如何在 MFC 应用程序中获取命令行参数?

2022-01-12 00:00:00 visual-c++ mfc

我希望有一个基于对话框的小应用程序,它通过命令行参数传递,因此,我使用 VC++6 运行应用程序向导并选择了一个 MFC 对话框应用程序.

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.

这不会自动配备命令行参数.所以我去了到 MSDN 来刷新我对这些的记忆.MSDN 声明所有 C++ 程序具有 main() 或 wmain() 函数以及 argc 等参数到这里.我刚刚创建的应用程序没有这些.

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.

显然有一个函数是应用程序的入口点,可以我在这里坚持论点?我确实尝试过,但我不相信我实际上是在编辑正确的功能.(我能找到那个函数吗?是作为项目设置中的 main() 函数还是类似函数?)

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.

也可以作为副业.对于一个简单的程序,这是,我真的不想使它成为一个 MFC 应用程序,因此大小超过 MB.是否有允许我制作非 MFC 对话框的应用程序向导模板库申请?

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?

推荐答案

使用 GetCommandLine(),返回正在执行的文件名,后跟参数.

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

应用程序成员 m_lpCmdLine(像 yourApp.m_lpCmdLine 一样使用)仅包含参数.

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

还有CWinApp::ParseCommandLine() 你可能会觉得有用.

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

还可以尝试使用 ATL COM 向导来创建非 MFC 对话框应用程序(选择 .exe 选项,而不是 .dll).

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

相关文章