当前位置:   article > 正文

VC中处理命令行参数:GetCommandLine()与m_lpCmdLine

m_lpcmdline

對於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 
 

 

  1. BOOL CMyApp::InitInstance()
  2. {
  3. // ...
  4. //通过判断第一个字符是不是字符串结尾标志来判断是否有命令行参数的输入
  5. if (m_lpCmdLine[0] == _T('\0')) {
  6. // Create a new (empty) document.
  7. OnFileNew();
  8. }
  9. else
  10. {
  11. // Open a file passed as the first command line parameter.
  12. OpenDocumentFile(m_lpCmdLine);
  13. }
  14. // ...
  15. }

  

而對於::GetCommandLine():

主进程中函数CreateProcess和ShellExcute传给子进程的命令行参数,在子进程中均可以::GetCommandLine()获取,但CWinApp::m_lpCmdLine仅可以获取ShellExcute传命令行参数.

 

 

 

 

參考與:

http://www.cnblogs.com/super119/archive/2011/04/10/2011331.html

http://blog.csdn.net/lala_achun/article/details/6119572

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/467156
推荐阅读
相关标签
  

闽ICP备14008679号