如果是在SDK下,只需要象 mscf 所说的"判断winmain的命令行参数是否为空或符合你要的条件"就行了的,即 int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state ); 检查lpCmdLine即可.如果是在MFC下的,情况就复杂了.首先在你的CWinApp的继承类中,通过其 ParseCommandLine()方法得知是否有命令行参数,然后override它的Run()方法,使之在无命令行参数的情况下直接返回0,有命令行参数时调用CWinApp::Run().
在任何地方都可以这样得到参数信息 for (int i = 1; i < __argc; i++) { LPCTSTR pszParam = __targv[i]; }
The GetCommandLine function returns a pointer to the command-line string for the current process. LPTSTR GetCommandLine(VOID) ParametersThis function has no parameters. Return ValuesThe return value is a pointer to the command-line string for the current process. ResNon-Unicode console processes written in C can use the argc and argv arguments to access the command-line arguments. The parameters of the command-line string, excluding the program name, are also available to such non-Unicode applications as a parameter of the WinMain function. The reason for the Unicode exclusion from these options is that WinMain, argc, and argv use the LPSTR data type for parameters, not the LPTSTR datatype. See Also
两种方式:
1 进入命令行方式,和dos一样的操作;
2 建立一个对应的应用程序的快捷方式,在快捷方式上右键-〉属性-〉目标 后加上你的参数。运行快捷方式。
你解释一下commandLine,有参数就继续,没有就退出.
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
);
检查lpCmdLine即可.如果是在MFC下的,情况就复杂了.首先在你的CWinApp的继承类中,通过其
ParseCommandLine()方法得知是否有命令行参数,然后override它的Run()方法,使之在无命令行参数的情况下直接返回0,有命令行参数时调用CWinApp::Run().
WinExec();
CreateProcess()
这三个函数都可以启动一个.exe文件
具体参数请参考MSDN
for (int i = 1; i < __argc; i++)
{
LPCTSTR pszParam = __targv[i];
}
ParametersThis function has no parameters. Return ValuesThe return value is a pointer to the command-line string for the current process. ResNon-Unicode console processes written in C can use the argc and argv arguments to access the command-line arguments. The parameters of the command-line string, excluding the program name, are also available to such non-Unicode applications as a parameter of the WinMain function. The reason for the Unicode exclusion from these options is that WinMain, argc, and argv use the LPSTR data type for parameters, not the LPTSTR datatype. See Also
{
...
InitCommonControls(); CWinApp::InitInstance(); if(m_lpCmdLine[0] == _T('\0')) {
::MessageBox(NULL,"无参数","错误",MB_OK|MB_ICONSTOP);
return FALSE;
}
...
}