Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does this crash in XP but not in 98?

Status
Not open for further replies.

kangolian

Programmer
Jan 26, 2002
4
GB
Hi

My program needs to run on all versions of Windows from 95 to XP. It also needs to detect what type of OS it is running on, so I am using GetVersionEx for this.
Trouble is, my app runs fine on 98 but when I test it on XP it crashes with some kind of Access Violation.
Anyone got any ideas on how to get round this on XP?
Please help, I can't go any further with my app until I have solved this problem!
Here's my code:

#include <windows.h>
#include <iostream.h>

bool IsNTBased();

void main()
{
if ( IsNTBased() )
cout << &quot;Is NT based\n&quot;;
else
cout << &quot;Is not NT based\n&quot;;
}


bool IsNTBased()
// IsNTBased returns true if OS is NT based, e.g NT, 2000 or XP
// Processes: gathers OS info and checks platform id
// Returns: true if platform id indicates an NT based OS, false otherwise
{

OSVERSIONINFO * osinfo; // os version info structure pointer
GetVersionEx( osinfo ); // gather os info

// if platform identifier is NT, return true
if ( osinfo->dwPlatformId == VER_PLATFORM_WIN32_NT )
return true;

return false;
}
 
Answered my own question by reading the Help!You have to first specify the size in bytes of the structure you are passing to the function.
I wonder why it still worked in 98 though? Weird.


bool IsNTBased()
{

OSVERSIONINFO osinfo; // os version info structure
osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

GetVersionEx( &osinfo ); // gather os info
cout << &quot;Platform ID = &quot; << osinfo.dwPlatformId << &quot;\n&quot;;

if ( osinfo.dwPlatformId == VER_PLATFORM_WIN32_NT )
return true;

return false;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top