How to get the command line arguments in MFC appli

2020-03-08 12:36发布

I wish to have a small dialog based application which is passed command line parameters, so, using VC++6 I ran the application wizard and chose an MFC dialog application.

This is not automatically equipped with command-line parameters. So I went to MSDN to refresh my memory on these. MSDN states that all C++ programs have either a main() or a wmain() function and that the argc, etc. arguments go here. The application I just created does not have these.

As there is obviously a function which is the entry point to the application, can I stick the arguments here? I did try this, but I am not convinced that I was actually editing the correct function. (Can I find the function which is acting as the main() function from the project settings or similar?)

Basically, how do I get my program to read command line parameters.

Also as a sideline. For a simple program, which this is, I really do not want to make it an MFC application, and thereby over a MB in size. Are there application wizard template libraries that will allow me to make a non-MFC dialog application?

4条回答
smile是对你的礼貌
2楼-- · 2020-03-08 13:11

Yes, see CWinApp:ParseCommandLine. Also take a look at the CCommandLineInfo class.

查看更多
Bombasti
3楼-- · 2020-03-08 13:13

In MFC applications, the entry point function is 'initInstance()', like main() or wmain(). Use CWinApp::m_lpCmdLine in initInstance() to access the command line.

查看更多
劫难
4楼-- · 2020-03-08 13:34

Use GetCommandLine(), which returns the name of the file being executed, followed by the arguments.

The application member m_lpCmdLine (used like yourApp.m_lpCmdLine) contains only the arguments.

There is also CWinApp::ParseCommandLine() that you may find useful.

Also try the ATL COM wizard to create a non-MFC dialog application (chose the .exe option, not .dll).

查看更多
爷的心禁止访问
5楼-- · 2020-03-08 13:38

To get the raw command line use the following code (will work on any Win32 / MFC application):

TCHAR *pCommandLine = ::[GetCommandLine()][1];
int nArgc = 0;
LPWSTR *pArgv = ::CommandLineToArgvW(pCommandLine, &nArgc);

nArgc should be 1 when no arguments given and larger than 1 when there are. Then, pArgv1 will be the first argument, and so on...

查看更多
登录 后发表回答