15.7 使应用程序支持启动参数

2013-10-07 00:34:43 · 作者: · 浏览: 64

15.7  使应用程序支持启动参数

在不同类型的应用程序下,对启动参数的支持方式也不同。

(1)Win32 Console类型应用程序如下:

#include <iostream>
using namespace std;
void main(int argc,char * argv[])
{
for (int i=0;i<argc;i++)
{
cout<<argv[i]<<endl;
}
}
在命令行下进入程序所在目录,输入如下参数:
J:\BookCode\Debug>cpp1.exe hello everyone~
程序输出结果如下所示。
cpp1.exe
hello
everyone~
(2)Win32 Application类型应用程序如下:
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR     lpCmdLine,
int       nCmdShow)
{
MessageBox(0,lpCmdLine,"参数",MB_OK);
return 0;
}

(3)MFC Application类型应用程序如下:
void CMFCDlg::OnOK() 
{
// TODO: Add extra validation here

AfxMessageBox(AfxGetApp()->m_lpCmdLine);
}
注意:假设有上述3种类型的Windows应用程序,名称为1.exe,那么按下面命令行参数执行程序:目录>1.exe hello everyone~。对于(2)、(3)获得的命令行字符串为"hello everyone~",与(1)不同,请读者注意。
【责任编辑:夏书 TEL:(010)68476606】

回书目   上一节   下一节