如果我們想要在調試 VC++ 程序的時候帶上命令行參數,可以這樣做:
在項目屬性裏面的 Configuration Properties | Debugging | Command Arguments 中,輸入命令行參數即可:
例如,在以下示範程序中:
int _tmain(int argc, TCHAR *argv[])
{
if ((argc > 1) && ((*argv[1] == _T('-') || (*argv[1] == _T('/')))))
{
if (_tcsicmp(_T("install"), argv[1] + 1) == 0)
{
// Install the service when the command is
// "-install" or "/install".
// TODO
}
else if (_tcsicmp(_T("remove"), argv[1] + 1) == 0)
{
// Uninstall the service when the command is
// "-remove" or "/remove".
// TODO
}
else if (_tcsicmp(_T("console"), argv[1] + 1) == 0)
{
AfdxEsService service(SERVICE_NAME);
service.ServiceWorkerThread();
}
}
else
{
// TODO
}
return 0;
}
在上述代碼中,如果命令行參數爲 -console 則進入“service.ServiceWorkerThread();”這個分支運行。我們在 Command Arguments 中,輸入命令行參數 -console,那麼在Debug的時候就會進入該分支。