1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/*****************************************************************************
* *
* PrimeSense PSCommon Library *
* Copyright (C) 2012 PrimeSense Ltd. *
* *
* This file is part of PSCommon. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
*****************************************************************************/
#include "XnLib.h"
#include <XnLog.h>
#ifdef __INTEL_COMPILER
#include <ia32intrin.h>
#else
#include <intrin.h>
#endif
//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
static const XnChar* GetOSName(OSVERSIONINFOEX& osVersionInfo)
{
if (osVersionInfo.dwMajorVersion == 4)
{
// Windows NT
switch (osVersionInfo.wProductType)
{
case VER_NT_WORKSTATION:
return "Windows NT 4.0 Workstation";
case VER_NT_SERVER:
case VER_NT_DOMAIN_CONTROLLER:
return "Windows NT 4.0 Server";
default:
return "Unknown Windows NT 4.0 version";
}
}
else if (osVersionInfo.dwMajorVersion == 5 && osVersionInfo.dwMinorVersion == 0)
{
// Windows 2000
switch (osVersionInfo.wProductType)
{
case VER_NT_WORKSTATION:
return "Windows 2000 Professional";
case VER_NT_SERVER:
case VER_NT_DOMAIN_CONTROLLER:
if ((osVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
return "Windows 2000 Datacenter Server";
else if ((osVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
return "Windows 2000 Advanced Server";
else
return "Windows 2000 Server";
default:
return "Unknown Windows 2000 version";
}
}
else if (osVersionInfo.dwMajorVersion == 5 && osVersionInfo.dwMinorVersion == 1)
{
// Windows XP.
if (0 != GetSystemMetrics(SM_TABLETPC))
return "Windows XP Tablet PC Edition";
else if (0 != GetSystemMetrics(SM_STARTER))
return "Windows XP Starter Edition";
else if (0 != GetSystemMetrics(SM_MEDIACENTER))
return "Windows XP Media Center Edition";
else if ((osVersionInfo.wSuiteMask & VER_SUITE_PERSONAL) == VER_SUITE_PERSONAL)
return "Windows XP Home Edition";
else
return "Windows XP Professional";
}
else if (osVersionInfo.dwMajorVersion == 5 && osVersionInfo.dwMinorVersion == 2)
{
// Windows Server 2003
if((osVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
return "Windows Server 2003 Datacenter Edition";
else if((osVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
return "Windows Server 2003 Enterprise Edition";
else if((osVersionInfo.wSuiteMask & VER_SUITE_BLADE) == VER_SUITE_BLADE)
return "Windows Server 2003 Web Edition";
else
return "Windows Server 2003 Standard Edition";
}
else if (osVersionInfo.dwMajorVersion == 6 && osVersionInfo.dwMinorVersion == 0)
{
// Windows Vista / Server 2008
switch (osVersionInfo.wProductType)
{
case VER_NT_WORKSTATION:
// Vista
return "Windows Vista";
case VER_NT_SERVER:
case VER_NT_DOMAIN_CONTROLLER:
return "Windows Server 2008";
default:
return "Unknown Windows 6.0 version";
}
}
else if (osVersionInfo.dwMajorVersion == 6 && osVersionInfo.dwMinorVersion == 1)
{
// Windows 7 / Server 2008 R2
if (osVersionInfo.wProductType != VER_NT_WORKSTATION)
{
return ("Windows Server 2008 R2");
}
else
{
return ("Windows 7");
}
}
else if (osVersionInfo.dwMajorVersion == 6 && osVersionInfo.dwMinorVersion == 2)
{
// Windows 8 / Server 2012
if (osVersionInfo.wProductType != VER_NT_WORKSTATION)
{
return ("Windows Server 2012");
}
else
{
return ("Windows 8");
}
}
return "Unknown Windows Version";
}
static void GetCPUName(XnChar* csName)
{
int CPUInfo[4] = {-1};
xnOSMemSet(csName, 0, XN_MAX_OS_NAME_LENGTH);
// calling cpuid with 0x80000000 retrives the number of extended properties
__cpuid(CPUInfo, 0x80000000);
unsigned int nExIds = CPUInfo[0];
if (nExIds >= 0x80000002)
{
__cpuid(CPUInfo, 0x80000002);
xnOSMemCopy(csName, CPUInfo, sizeof(CPUInfo));
}
if (nExIds >= 0x80000003)
{
__cpuid(CPUInfo, 0x80000003);
xnOSMemCopy(csName + 16, CPUInfo, sizeof(CPUInfo));
}
if (nExIds >= 0x80000004)
{
__cpuid(CPUInfo, 0x80000004);
xnOSMemCopy(csName + 32, CPUInfo, sizeof(CPUInfo));
}
if (nExIds > 0x80000002)
return;
// else, we need to build the name ourselves
xnOSMemSet(CPUInfo, 0xFF, sizeof(CPUInfo));
__cpuid(CPUInfo, 0);
int nIds = CPUInfo[0];
*((int*)csName) = CPUInfo[1];
*((int*)(csName+4)) = CPUInfo[3];
*((int*)(csName+8)) = CPUInfo[2];
if (nIds >= 1)
{
__cpuid(CPUInfo, 1);
int nSteppingID = CPUInfo[0] & 0xf;
int nModel = (CPUInfo[0] >> 4) & 0xf;
int nFamily = (CPUInfo[0] >> 8) & 0xf;
//int nProcessorType = (CPUInfo[0] >> 12) & 0x3;
//int nExtendedmodel = (CPUInfo[0] >> 16) & 0xf;
//int nExtendedfamily = (CPUInfo[0] >> 20) & 0xff;
//int nBrandIndex = CPUInfo[1] & 0xff;
sprintf(csName+strlen(csName), " Model: %d Family: %d Stepping: %d", nModel, nFamily, nSteppingID);
}
}
XN_C_API XnStatus xnOSGetInfo(xnOSInfo* pOSInfo)
{
// Validate input/output arguments
XN_VALIDATE_OUTPUT_PTR(pOSInfo);
// Get OS Info
OSVERSIONINFOEX osVersionInfo;
osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if (0 == GetVersionEx((LPOSVERSIONINFO)&osVersionInfo))
{
DWORD nErr = GetLastError();
xnLogWarning(XN_MASK_OS, "Failed getting OS version information. Error code: %d", nErr);
return XN_STATUS_ERROR;
}
sprintf(pOSInfo->csOSName, "%s", GetOSName(osVersionInfo));
if (osVersionInfo.szCSDVersion[0] != '\0')
{
strcat(pOSInfo->csOSName, " ");
strcat(pOSInfo->csOSName, osVersionInfo.szCSDVersion);
}
// Get CPU Info
GetCPUName(pOSInfo->csCPUName);
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
pOSInfo->nProcessorsCount = systemInfo.dwNumberOfProcessors;
// Get Memory Info
MEMORYSTATUSEX memoryStatus;
memoryStatus.dwLength = sizeof(memoryStatus);
if (0 == GlobalMemoryStatusEx(&memoryStatus))
{
DWORD nErr = GetLastError();
xnLogWarning(XN_MASK_OS, "Failed getting Memory information. Error code: %d", nErr);
return XN_STATUS_ERROR;
}
pOSInfo->nTotalMemory = memoryStatus.ullTotalPhys;
return (XN_STATUS_OK);
}
XnStatus xnWin32CreateKernelObjectName(XnChar* strDest, const XnUInt32 nDestLength, const XnChar* strSource, XnBool bAllowOtherUsers)
{
XnChar* pDest = strDest;
XnChar* pDestEnd = strDest + nDestLength;
if (bAllowOtherUsers)
{
static const XnChar strGlobal[] = "Global\\";
strcpy(strDest, strGlobal);
pDest = strDest + sizeof(strGlobal) - 1;
}
const XnChar* pSource = strSource;
while (pDest < pDestEnd && *pSource != '\0')
{
*pDest = *pSource == '\\' ? '_' : *pSource;
++pDest;
++pSource;
}
if (pDest == pDestEnd)
{
xnLogWarning(XN_MASK_OS, "Event name is too long!");
return XN_STATUS_ERROR;
}
*pDest = '\0';
return (XN_STATUS_OK);
}
XnStatus xnWin32GetSecurityAttributes(XnBool bAllowOtherUsers, SECURITY_ATTRIBUTES** ppSecurityAttributes)
{
*ppSecurityAttributes = NULL;
static SECURITY_DESCRIPTOR securityDesc;
static SECURITY_ATTRIBUTES securityAttributes;
static SECURITY_ATTRIBUTES* pSecurityAttributes = NULL;
if (pSecurityAttributes == NULL)
{
// initialize it now
if (FALSE == InitializeSecurityDescriptor(&securityDesc, SECURITY_DESCRIPTOR_REVISION))
{
xnLogWarning(XN_MASK_OS, "Failed to initialize security descriptor (%d)", GetLastError());
return XN_STATUS_ERROR;
}
if (FALSE == SetSecurityDescriptorDacl(&securityDesc, TRUE, NULL, FALSE))
{
xnLogWarning(XN_MASK_OS, "Failed to set security descriptor DACL (%d)", GetLastError());
return XN_STATUS_ERROR;
}
securityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
securityAttributes.lpSecurityDescriptor = &securityDesc;
securityAttributes.bInheritHandle = FALSE;
// initialization complete. Store a pointer
pSecurityAttributes = &securityAttributes;
}
*ppSecurityAttributes = bAllowOtherUsers ? pSecurityAttributes : NULL;
return (XN_STATUS_OK);
}
|