VC中如何处理命令行参数

无论是SDI、MDI还是基于Dialog的程序,主类都是继承自CWinApp的。在CWinApp中,有命令行参数的成员变量 - m_lpCmdLine

m_lpCmdLine 是一个LPTSTR,也就是一个32位的字符串,也就是整个命令行参数(不带应用程序可执行文件的名字)。举例来说,如果应用程序是Hello,那么运行 Hello I am John,此时的m_lpCmdLine就是I am John,得到了这个命令行参数之后,应用程序就可以自己再展开分析了。

最后附上一段MSDN中有关LPTSTR的解说:

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.

Example

Code: Select all
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);
   }
 
   // ...
}
原文地址:https://www.cnblogs.com/super119/p/2011331.html