Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...