Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if Windows operating system in C++ [duplicate]

Tags:

c++

windows

How can I detect if the operating system is Windows in C++/C?

like image 646
Daniel Avatar asked Dec 06 '10 05:12

Daniel


2 Answers

#ifdef _WIN32
cout << "This is Windows." << endl;
#endif

This will allow you define blocks for windows only. As long as the preprocessor macro is defined.

This is a compile time thing, not a runtime thing.

like image 192
Paul Avatar answered Oct 07 '22 21:10

Paul


You can use getenv() from cstdlib like so:

#include <cstdlib>

getenv("windir");

If you get NULL then it's not windows.

This works since %windir% should only be defined on windows systems. This is a cheap and dirty hack of course.

http://www.cplusplus.com/reference/clibrary/cstdlib/getenv/

like image 33
Jeremy Edwards Avatar answered Oct 07 '22 21:10

Jeremy Edwards