Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/65863430/c-command-line-arguments-with-windows-h

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...