在MFC程序中,可以用以下几种方法来获取命令行参数。
为方便说明,我们假设执行了命令:C:\test\app.exe -1 -2
方法一 : 使用API ::GetCommandLine()获取应用程序名称及参数列表
在OninitDialog()中添加代码
CString sCmdline = ::GetCommandLine();
AfxMessageBox(sCmdline);将获取到 "C:\test\app.exe -1 -2 "方法二 :
在OnInitDialog()中添加代码
for (int i = 0; i < __argc; i++)
{
__argv[i];
AfxMessageBox(__argv[i]);
}将依次得到"C:\test\app.exe","-1", "-2"
CString sCmdline = AfxGetApp()->m_lpCmdLine;
将获取到 "-1 -2 ",AfxGetApp()->m_lpCmdLine 只包含参数。
本文介绍在MFC程序中通过三种不同方法获取命令行参数的过程,包括使用API::GetCommandLine()、__argc和__argv以及AfxGetApp()->m_lpCmdLine。
1820

被折叠的 条评论
为什么被折叠?



