RSC  0.19.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, 2018 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 _WIN32_WINNT 0x0600 // For GetTickCount64 as per MSDN
30 #define SECURITY_WIN32
31 #include <windows.h>
32 #include <security.h>
33 
34 #include <stdexcept>
35 #include <sstream>
36 
37 #include <boost/format.hpp>
38 
39 #include <boost/date_time/posix_time/posix_time.hpp>
40 
41 #include "Win32Common.h"
42 
43 namespace rsc {
44 namespace os {
45 
46 // {Machine,Software} {Type,Version}
47 
48 std::string currentMachineType () {
49  throw std::runtime_error("not available");
50 }
51 
52 std::string currentMachineVersion () {
53  throw std::runtime_error("not available");
54 }
55 
56 std::string currentSoftwareType () {
57  return "win32";
58 }
59 
60 std::string currentSoftwareVersion () {
61  throw std::runtime_error("not available");
62 }
63 
64 // Hostname
65 
66 const unsigned int HOSTNAME_MAX_LENGTH = 1024;
67 
68 std::string currentHostname() {
69  char buffer[HOSTNAME_MAX_LENGTH];
70  long unsigned int length = HOSTNAME_MAX_LENGTH;
71  if (GetComputerName(buffer, &length) != 0) {
72  return std::string(buffer, length);
73  } else {
74  throw std::runtime_error(
75  boost::str(
76  boost::format("GetComputerName failed: %1%")
77  % GetLastErrorString()));
78  }
79 }
80 
81 // Host ID
82 
83 const unsigned int HOST_ID_MAX_LENGTH = 1024;
84 
85 std::string currentHostId() {
86  char buffer[HOST_ID_MAX_LENGTH];
87  long unsigned int length = HOST_ID_MAX_LENGTH;
88  if (GetComputerObjectName(NameUniqueId, buffer, &length) != 0) {
89  return std::string(buffer, length);
90  } else {
91  throw std::runtime_error(
92  boost::str(boost::format("GetComputerObjectName(NameUniqueId)"
93  " failed: %1%") % GetLastErrorString()));
94  }
95 }
96 
97 // Boot time
98 
99 boost::posix_time::ptime currentBootTime() {
100  // at least an approximation due to runtime differences in the two calls
101  boost::uint64_t millisSinceBoot = GetTickCount64();
102  return boost::posix_time::microsec_clock::local_time()
103  - boost::posix_time::millisec(millisSinceBoot);
104 }
105 
106 }
107 }
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.