Somebody
Somebody

Reputation: 175

C command line arguments with Windows.h

I'm making a simple window program in C with windows.h (I'm quite new to this). As I understand, a start of execution is function int WINAPI WinMain. If I make int main function, the window does not show. So,

  1. Is there a way to have int main and int WINAPI WinMain at the same time?
  2. If not, how do I get command-line arguments?

P.S. I'm using Windows.

Upvotes: 0

Views: 362

Answers (1)

Martin Rosenau
Martin Rosenau

Reputation: 18521

Some background

The actual entry for the program is the start-up code.

The start-up code does not have any arguments.

The start-up code then calls main or WinMain.

... so the start-up code must "calculate" the arguments of main or WinMain.

About your question

You cannot use main and WinMain at the same time.

However, you can "calculate" the arguments of main or WinMain the same way the start-up code does it:

  • GetModuleHandle(NULL) is used to get the program instance (first argument of the WinMain function)
  • The "command line" argument of WinMain is calculated from the value returned by GetCommandLine()
  • This value is also used to calculate argv and argc of main
  • The window show mode argument of WinMain is calculated from the STARTUPINFO which is read using GetStartupInfo()
  • The environment variables passed to main are returned by GetEnvironmentStrings()

Upvotes: 4

Related Questions