How to detect OS version?

Started by
4 comments, last by KaneBlackflame 22 years, 2 months ago
I have noticed that different Windows versions handle thread timing in a special way. Win XP and 98 take the thread priority literally, running the thread for the whole alotment of time, which can overuse code or waste precious cycles. Win2K handles the threads real time, allowing the thread to run up to the suggested thread priorities time allotment. This runs much better. I have found that my AI thread gets tons of extra time to think or wastes a lot of time I need for display when running on XP/98 machines. This causes the AI to learn much faster than I need it to, or the display to lag. Is there a function I can call or a registry key I can look at to get a machines Windows version so I can more approriatly set my thread priorities? "Victims...aren''t we all?" -Brandon Lee, the Crow
"Victims...aren't we all?" -Brandon Lee, the Crow
Advertisement
Check the registry

Windows NT
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\CurrentVersion
Yes, I know VB code, but this was the first code snippet I could find. If you understand this, it shouldn''t be very hard to port to C++. Hope this helps.


Public Const HKEY_LOCAL_MACHINE = &H80000002

Public Const REG_SZ = 1 ''Unicode nul terminated string
Public Const REG_BINARY = 3 ''Free form binary
Public Const REG_DWORD = 4 ''32-bit number
Public Const ERROR_SUCCESS = 0&
Public Const ERROR_FILE_NOT_FOUND = 2

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long


Public Function GetRegKeyString(hKey As Long, _
strPath As String, strValue As String, Optional _
Default As String) As String
Dim hCurKey As Long
Dim lResult As Long
Dim lValueType As Long
Dim strBuffer As String
Dim lDataBufferSize As Long
Dim intZeroPos As Integer
Dim lRegResult As Long

''Set up default value
If Not IsEmpty(Default) Then
GetRegKeyString = Default
Else
GetRegKeyString = ""
End If

lRegResult = RegOpenKey(hKey, strPath, hCurKey)

If (lRegResult = ERROR_FILE_NOT_FOUND) Then
GetRegKeyString = vbNullString
lRegResult = RegCloseKey(hCurKey)
Exit Function
End If

lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, ByVal 0&, lDataBufferSize)

If lRegResult = ERROR_SUCCESS Then
If lValueType = REG_SZ Then
strBuffer = String(lDataBufferSize, " ")
lResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, ByVal strBuffer, lDataBufferSize)
intZeroPos = InStr(strBuffer, Chr$(0))

If intZeroPos > 0 Then
GetRegKeyString = Left$(strBuffer, intZeroPos - 1)
Else
GetRegKeyString = strBuffer
End If
End If
ElseIf (lRegResult = ERROR_FILE_NOT_FOUND) Then
GetRegKeyString = vbNullString
lRegResult = RegCloseKey(hCurKey)
Exit Function
Else
VBA.Err.Raise ERR_FATAL
End If

lRegResult = RegCloseKey(hCurKey)
End Function
Here''s som more or less pseudo-code that should work.
Note that you''ll have to define the os-name constants yourself before trying it.

int get_os(){	OSVERSIONINFO osinf; 	osinf.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);	GetVersionEx(&osinf); 	switch(osinf.dwPlatformId)	{	case VER_PLATFORM_WIN32_NT: 		if (osinf.dwMajorVersion == 3)			return OS_WINDOWS_NT3;		if (osinf.dwMajorVersion == 4)			return OS_WINDOWS_NT4;		if (osinf.dwMajorVersion == 5 && osinf.dwMinorVersion == 0)			return OS_WINDOWS_2000; 		if (osinf.dwMajorVersion >= 5 || (osinf.dwMajorVersion == 5 && osinf.dwMinorVersion > 0))			return OS_WINDOWS_XP;	//Windows XP, and it''s followers! (if any) 		break;	case VER_PLATFORM_WIN32_WINDOWS: 		if (osinf.dwMinorVersion == 0)			return OS_WINDOWS_95; 		if (osinf.dwMinorVersion == 10)			return OS_WINDOWS_98;		if (osinf.dwMinorVersion == 90) 			return OS_WINDOWS_ME;		break;	case VER_PLATFORM_WIN32s: 		return OS_WINDOWS_32S;	} 	return (-1);	//Unknown}  
Thanks! That is exactly what I was looking for. I can always count on the GameDev.net forums to answer anything!

"Victims...aren''t we all?"
-Brandon Lee, the Crow
"Victims...aren't we all?" -Brandon Lee, the Crow
*cough* libSysInfo

Excuse the shameless promotion
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers

This topic is closed to new replies.

Advertisement