简体   繁体   中英

Tell difference between Vista and XP [C]

Is their a way in C to diferentiate between Vista and XP. Reason being is the the path I use is different in both.

You can get the version of your Windows OS by calling GetVersionEx .

OSVERSIONINFO vi;
vi.dwOSVersionInfoSize = sizeof vi;
GetVersionEx(&vi);

if (vi.dwMajorVersion >= 6)
    // Windows Vista or newer
else
    // Windows XP or older

You shouldn't have platform specific paths hard-coded into your application. There are environment variables for these things.

Open up a command prompt and type "set" to view a list of them. Several of these have been standard since Windows 95. Important environment variables to note are...

  • HOME
  • APPDATA
  • ProgramFiles
  • SystemRoot
  • ALLUSERSPROFILE

So for example...

char * path;
    path = getenv("HOME");
    printf(path);

Have a poke around your target versions of windows to see what variables are common between the two.

edit: python has made me lazy with string manipulation, fixed example code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM