---
title: "VC中处理命令行参数:GetCommandLine()与m_lpCmdLine"
author: "hanyezhiyu8"
date: "2012-08-28T14:08:49Z"
tags: []
categories: ["编程语言"]
slug: "vc-getcommandline-m-lpcmdline"
draft: false
lastmod: "2012-08-28T14:14:26Z"
id: 1665087
url: "https://inuts.iteye.com/blog/1665087"
---

對於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](http://www.cnblogs.com/super119/archive/2011/04/10/2011331.html)

[http://blog.csdn.net/lala\_achun/article/details/6119572](http://blog.csdn.net/lala_achun/article/details/6119572)