RSC  0.16.0
Win32HostInfo.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSC project
4  *
5  * Copyright (C) 2014 Jan Moringen
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "HostInfo.h"
28 
29 #define SECURITY_WIN32
30 #include <windows.h>
31 #include <security.h>
32 
33 #include <stdexcept>
34 #include <sstream>
35 
36 #include <boost/format.hpp>
37 
38 #include <boost/date_time/posix_time/posix_time.hpp>
39 
40 #include "Win32Common.h"
41 
42 namespace rsc {
43 namespace os {
44 
45 // {Machine,Software} {Type,Version}
46 
47 std::string currentMachineType () {
48  throw std::runtime_error("not available");
49 }
50 
51 std::string currentMachineVersion () {
52  throw std::runtime_error("not available");
53 }
54 
55 std::string currentSoftwareType () {
56  return "win32";
57 }
58 
59 std::string currentSoftwareVersion () {
60  throw std::runtime_error("not available");
61 }
62 
63 // Hostname
64 
65 const unsigned int HOSTNAME_MAX_LENGTH = 1024;
66 
67 std::string currentHostname() {
68  char buffer[HOSTNAME_MAX_LENGTH];
69  long unsigned int length = HOSTNAME_MAX_LENGTH;
70  if (GetComputerName(buffer, &length) != 0) {
71  return std::string(buffer, length);
72  } else {
73  throw std::runtime_error(
74  boost::str(
75  boost::format("GetComputerName failed: %1%")
76  % GetLastErrorString()));
77  }
78 }
79 
80 // Host ID
81 
82 const unsigned int HOST_ID_MAX_LENGTH = 1024;
83 
84 std::string currentHostId() {
85  char buffer[HOST_ID_MAX_LENGTH];
86  long unsigned int length = HOST_ID_MAX_LENGTH;
87  if (GetComputerObjectName(NameUniqueId, buffer, &length) != 0) {
88  return std::string(buffer, length);
89  } else {
90  throw std::runtime_error(
91  boost::str(boost::format("GetComputerObjectName(NameUniqueId)"
92  " failed: %1%") % GetLastErrorString()));
93  }
94 }
95 
96 // Boot time
97 
98 boost::posix_time::ptime currentBootTime() {
99  // at least an approximation due to runtime differences in the two calls
100  boost::uint64_t millisSinceBoot = GetTickCount64();
101  return boost::posix_time::microsec_clock::local_time()
102  - boost::posix_time::millisec(millisSinceBoot);
103 }
104 
105 }
106 }
RSC_EXPORT std::string currentHostId()
Determine and return a unique id string of the local machine.
const unsigned int HOST_ID_MAX_LENGTH
RSC_EXPORT boost::posix_time::ptime currentBootTime()
Return the boot time of the local machine.
RSC_EXPORT std::string currentMachineVersion()
Determine and return the version within its type, usually the CPU identification string, of the local machine.
RSC_EXPORT std::string currentHostname()
Determine and return the hostname of the local machine.
std::string GetLastErrorString()
Definition: Win32Common.cpp:36
RSC_EXPORT std::string currentSoftwareVersion()
Determine and return the version of the operating system within its type, usually the kernel version ...
RSC_EXPORT std::string currentSoftwareType()
Determine and return the type of the operating system, usually the kernel name, running on the local ...
const unsigned int HOSTNAME_MAX_LENGTH
RSC_EXPORT std::string currentMachineType()
Determine and return the machine type, usually CPU architecture, of the local machine.