This topic is now archived and is closed to further replies.

How to detect OS version?

This topic is 5796 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post

Link to post
Share on other sites
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&

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
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)
GetRegKeyString = strBuffer
End If
End If
ElseIf (lRegResult = ERROR_FILE_NOT_FOUND) Then
GetRegKeyString = vbNullString
lRegResult = RegCloseKey(hCurKey)
Exit Function
End If

lRegResult = RegCloseKey(hCurKey)
End Function

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
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()
osinf.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

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)
if (osinf.dwMinorVersion == 0)
return OS_WINDOWS_95;
if (osinf.dwMinorVersion == 10)
return OS_WINDOWS_98;
if (osinf.dwMinorVersion == 90)
return OS_WINDOWS_32S;

return (-1); //Unknown

Share this post

Link to post
Share on other sites