What is the difference between standard, int main(int nargs,....); and

int WINAPI WinMain(
  HINSTANCE hInstance, 
  HINSTANCE hPrevInstance, 
  LPWSTR lpCmdLine, 
  int nShowCmd 
);

I am making a cross-platform program, for windows and linux, and until this point, I used main, without WinMain, for windows and linux. My app calls windows and Linux-specific functions but I still use normal main(). Do I need WinMain() if I want to make windows GUI instead of command prompt? Sorry, I am not an expert in Windows programming.

Maybe someone will correct me, but I believe its just convention. But WinMain does have extra parameter options than int main() which might become useful. But a thing to note is that returning in WinMain really doesn't do much since its returned value is passed to ExitThread instead of ExitProcess.

It is the entry point for Win32 applications (not console). If you're doing cross platform, you will need to check the definitions to see which OS you're compiling for and use some #ifdef pre-processor statements to include the correct entry point.

I believe that the difference is that Windows actually makes a difference between programs that run as a console application (MSDOS) and programs that run in the Windows system (Win32). If you try to run a program that uses WinMain() from a console window, you will probably get the message "This program cannot run in console." (at least, on older versions of windows that still had a native MSDOS environment available, newer versions only emulate the MSDOS environment via the "command prompt"). In Linux, and other *nix environments, all programs run in the terminal (whether you see it or not), and the GUI environment is nothing more than an external library invoked by the program.

But I might be wrong, it has been a while since I implemented any GUI app for windows, but I don't remember ever doing it without a WinMain function as entry-point. Also note that many cross-platform GUI libraries will hide away the entry-point function and provide you with a secondary function that acts as the entry-point for your purposes (which is usually invoked after the basic setup of the GUI application have been done).

Ok, thanks guys. I decided to use winmain for my project, so I have something likes this

#define linux
//#define windows

int realmain() {
//real work for the program goes here
}

int main() {
return realmain();
}

int WINAPI WinMain(
  HINSTANCE hInstance, 
  HINSTANCE hPrevInstance, 
  LPWSTR lpCmdLine, 
  int nShowCmd 
) {
return realmain();
}

You don't need those #defines for linux and windows. There are a number of "standard" preprocessor defines which can tell you for what OS and compiler the code is being compiled for/with, in addition to a lot of other things (like C++0x support, multi-threading stuff, etc.). Here is a list for discriminating between the OS.

For example, you could just do:

int realmain() {
  //real work for the program goes here
}

#ifdef _WIN32
int WINAPI WinMain(
  HINSTANCE hInstance, 
  HINSTANCE hPrevInstance, 
  LPWSTR lpCmdLine, 
  int nShowCmd 
) {
#else
int main() {
#endif
  return realmain();
}
Member Avatar for jmichae3

What is the difference between standard, int main(int nargs,....); and

int WINAPI WinMain(
  HINSTANCE hInstance, 
  HINSTANCE hPrevInstance, 
  LPWSTR lpCmdLine, 
  int nShowCmd 
);

I am making a cross-platform program, for windows and linux, and until this point, I used main, without WinMain, for windows and linux. My app calls windows and Linux-specific functions but I still use normal main(). Do I need WinMain() if I want to make windows GUI instead of command prompt? Sorry, I am not an expert in Windows programming.

first, when you compile a commandline console app, you compile without anything special. with a windows GUI app which requires WinMain(), in mingw and mingw-w64 you must compile with the -windows switch. not sure, but I think this makes a different kind of exe,a Windows PE exe. main() will be totally ignored, at this point, you are using WinMain().

the lpCmdLine must be parsed using a win32 function call, which returns a UNICODE string. unfortunately, UNICODE is not really supported by C++ at this time, and it's not well supported by C either. windows uses 16-bit unicode.

oh, and the definition for WinMain() changes almost daily. I noticed your definition is different than the one I was using for my code (LPWSTR instead of LPSTR, probably a correction). you can try using ustring from sf.net.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.