tonyaim83
12-11-2007, 05:44 AM
Hi
How should i identify the operating system on which my program is running in c++.
Kindly help
How should i identify the operating system on which my program is running in c++.
Kindly help
|
Click to See Complete Forum and Search --> : How to identify the operating system tonyaim83 12-11-2007, 05:44 AM Hi How should i identify the operating system on which my program is running in c++. Kindly help Hack 12-11-2007, 08:23 AM Using WMI, try thisusing namespace System::Management; String^ Result = nullptr; ManagementObjectSearcher^ ObjSearcher; ObjSearcher = gcnew ManagementObjectSearcher( "SELECT Caption FROM Win32_OperatingSystem" ); for each( ManagementObject^ MgmtObj in ObjSearcher->Get() ) { Result = MgmtObj["Caption"]->ToString(); } return Result; vijayan 12-11-2007, 08:26 AM unless you have done something extraordinary, this will return true on unix and unix-like (linux,cygwin) systems; false on windows systems. inline bool this_is_unix() { return system( "uname >> /dev/null" ) == 0 ; } Viorel 12-11-2007, 08:32 AM Hi How should i identify the operating system on which my program is running in c++. Kindly help I think your executable file cannot run on more Operating Systems and you have to compile the program for each case. You can detect the Operating System on compilation time using pre-processor directives. Like this: #ifdef WIN32 . . . // Compiling for Windows #elif defined(linux) . . . // Compiling for Linux #else # error Unknown target Operating System #endif Example: const char end_of_line[] = #ifdef WIN32 "\r\n"; #elif defined(linux) "\r"; #else # error Unknown target Operating System #endif I hope this works. Danny 12-12-2007, 08:44 PM Added to what others have said, you can also use environment variables to detect the current OS at runtime. This is useful when you run the program in a emulated environment and want to switch to a different emulator dynamically. devx.com
Copyright Internet.com Inc. All Rights Reserved |