![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
Q: Determine which version of Windows the program is running on.AnswerUse the GetVersionEx API function. GetVersionEx returns the OS version and a platform identifier that indicates whether the OS is Windows 95/98 or Windows NT. Here is a code example that extracts version information and dumps it into a TMemo control. The first three lines do most of the work. Figure 1 shows the output from this code. void __fastcall TForm1::Button1Click(TObject *Sender) { // GetVersionEx takes a pointer to an OSVERSIONINFO struct as // its only argument. It will fill in the struct with version // info. The struct has a size member that must be initialized // to the size of the structure. OSVERSIONINFO info; info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&info); // add version info to a TMemo control Memo1->Lines->Clear(); Memo1->Lines->Add("Major Version:" + IntToStr(info.dwMajorVersion)); Memo1->Lines->Add("Minor Version:" + IntToStr(info.dwMinorVersion)); Memo1->Lines->Add("Build Number :" + IntToStr(info.dwBuildNumber)); Memo1->Lines->Add("Platform ID :" + IntToStr(info.dwPlatformId)); Memo1->Lines->Add("CSDVersion :" + AnsiString(info.szCSDVersion)); Memo1->Lines->Add(""); // if the major version is less than 4, then you know that the OS // is windows NT 3.X, since BCB can't create 16 bit programs if(info.dwMajorVersion < 4) Memo1->Lines->Add("Windows NT 3.X detected"); switch (info.dwPlatformId) { case VER_PLATFORM_WIN32s: Memo1->Lines->Add("Win32s detected"); break; case VER_PLATFORM_WIN32_WINDOWS: Memo1->Lines->Add("Win95 or Win 98 detected"); break; case VER_PLATFORM_WIN32_NT: Memo1->Lines->Add("Windows NT detected"); break; } } ![]() Figure 1: GetVersionEx Example. Note: On Windows 95/98, GetVersionEx fills in the dwBuildNumber of the OSVERSIONINFO with both the build number and the major and minor version numbers. The build number resides in the low word, and the major and minor version numbers reside in the high word. Since the structure already contains separate members for the major an minor versions, you can mask off the high word of dwBuildNumber to obtain the correct value for the build number. OSVERSIONINFO info; info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&info); // If Windows 95/98 is detected, mask off the junk in the build number if (info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) info.dwBuildNumber &= 0x0000FFFF; | ||||||
All rights reserved. |