如何检测传递到创建进程的无效命令
我目前正在开发一个程序,该程序启动用户指定的程序。 传入的是用户输入的 wstring。我的问题是如何让它抛出异常或检查“passedIn”是否有效。目前,如果用户输入“notepad.exe”,它会正确启动它,但如果他们输入像“asdf”这样的虚假内容或类似的内容,它仍然会创建该进程。
try {
wchar_t* commandLine = new wchar_t [CP_MAX_COMMANDLINE];
wcsncpy_s(commandLine, CP_MAX_COMMANDLINE, passedIn.c_str(), passedIn.size() +1);
CreateProcess(NULL,
commandLine,
NULL, NULL,
false,CREATE_NEW_CONSOLE,NULL,
NULL,
&sinfo,
&pi);
delete [] commandLine;
}
catch (int e) {
cout << "An exception occurred. Exception Nr. " << e << endl;
}
我希望我的捕获物能抓住它,但它没有。我可以做些什么来检查它是否有效?
谢谢!
I am currently working on a program that starts a program that is specified by the user.
passed in is as a wstring entered in by the user. My question is how do i make it throw an exception or check if "passedIn" is vaild. Currently, if the user enters in "notepad.exe" it launches it properly but if they enter somthing bogus like "asdf" or something along those lines it still creates the process.
try {
wchar_t* commandLine = new wchar_t [CP_MAX_COMMANDLINE];
wcsncpy_s(commandLine, CP_MAX_COMMANDLINE, passedIn.c_str(), passedIn.size() +1);
CreateProcess(NULL,
commandLine,
NULL, NULL,
false,CREATE_NEW_CONSOLE,NULL,
NULL,
&sinfo,
&pi);
delete [] commandLine;
}
catch (int e) {
cout << "An exception occurred. Exception Nr. " << e << endl;
}
I was hopeing my catch would grab it but it doesnt. Is there anything i can do to check if its vaild?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引用函数的文档(应该有是您检查的第一件事):
因此,检查函数的返回值。
Quoting the function's documentation (which should have been the first thing you checked):
So, check the function's return value.