赞
踩
對於VC程序无论是SDI、MDI还是基于Dialog的程序,主类都是继承自CWinApp,在CWinApp中,有命令行参数的成员变量 - m_lpCmdLine
m_lpCmdLine 是一个LPTSTR,也就是一个32位的字符串,也就是整个命令行参数(不带应用程序可执行文件的名字)。举例来说,如果应用程序是Hello.exe,那么运行用命令運行Hello world,此时的m_lpCmdLine就是world,得到了这个命令行参数。
MSDN:
CWinApp::m_lpCmdLine
Remarks
Corresponds to the lpCmdLine parameter passed by Windows to WinMain. Points to a null-terminated string that specifies the command line for the application. Use m_lpCmdLine to access any command-line arguments the user entered when the application was started. m_lpCmdLine is a public variable of type LPTSTR.
说明:
对应于Windows传递给WinMain的lpCmdLine参数。指向一个以null结尾的字符串,指定了应用程序的命令行。用m_lpCmdLine可以访问当应用程序启动时用户输入的命令行参数。m_lpCmdLine是LPSTR类型的公有变量。
Example
- BOOL CMyApp::InitInstance()
- {
- // ...
-
-
- //通过判断第一个字符是不是字符串结尾标志来判断是否有命令行参数的输入
- if (m_lpCmdLine[0] == _T('\0')) {
- // Create a new (empty) document.
- OnFileNew();
- }
- else
- {
- // Open a file passed as the first command line parameter.
- OpenDocumentFile(m_lpCmdLine);
- }
-
- // ...
- }

而對於::GetCommandLine():
主进程中函数CreateProcess和ShellExcute传给子进程的命令行参数,在子进程中均可以::GetCommandLine()获取,但CWinApp::m_lpCmdLine仅可以获取ShellExcute传命令行参数.
參考與:
http://www.cnblogs.com/super119/archive/2011/04/10/2011331.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。