当我将ShellExecuteEx与这样的命令"-unused parameter -action capturescreenshot -filename C:\\ATDK\\Screenshots\\abcd.jbg"一起使用时,一切都很好,Executor.exe以char* argv[]开始,拥有所有的权限。9个参数。但是,当命令有更多的符号,例如文件名为"abc...xyz.jpg“时,该进程就有argc == 1,并且命令是空的。所以在将命令发送到ShellExecute之前,在我将其更改为ShellExecute (而不是Ex )之前,这个命令是可以的,它是有效的!命令可以很长,并且已经成功地传递了。谁能解释一下有什么区别吗?下面是我填充的SHELLEXECUTEINFO代码。
std::wstringstream wss;
wss << L"-unused" << " " // added because we need to pass some info as 0 parameter
<< L"parameter" << " " // added because EU parser sucks
<< L"-action" << " "
<< L"capturescreenshot" << " "
<< L"-filename" << " "
<< L"C:\\ATDK\\Screenshots\\abc.jpg";
SHELLEXECUTEINFO shell_info;
ZeroMemory(&shell_info, sizeof(shell_info));
shell_info.cbSize = sizeof(SHELLEXECUTEINFO);
shell_info.fMask = SEE_MASK_ASYNCOK | SEE_MASK_NO_CONSOLE;
shell_info.hwnd = NULL;
shell_info.lpVerb = NULL;
shell_info.lpFile = L"C:/ATDK/Executor";
shell_info.lpParameters = (LPCWSTR)wss.str().c_str();
shell_info.lpDirectory = NULL;
shell_info.nShow = SW_MINIMIZE;
shell_info.hInstApp = NULL;
// 1
ShellExecuteEx(&shell_info);
// this sucks,
// GetLastError returns err code 2147483658,
//FormatMessage returns The data necessary to complete this operation is not yet available
// 2
ShellExecute(NULL, NULL, L"C:/ATDK/Executor", (LPCWSTR)wss.str().c_str(), NULL, NULL);
// OK!发布于 2017-02-10 11:19:12
你的窃听器在这里:
shell_info.lpParameters = (LPCWSTR)wss.str().c_str();wss.str()返回一个临时对象,该对象在创建该对象的完整表达式结束后不再存在。在这一点之后使用它是未定义的行为。
要解决这个问题,您必须构造一个std::wstring对象,该对象的生存期足够长,以便调用ShellExecuteEx返回。
std::wstringstream wss;
wss << L"-unused" << " "
<< L"parameter" << " "
// ...
SHELLEXECUTEINFO shell_info;
ZeroMemory(&shell_info, sizeof(shell_info));
// Construct string object from string stream
std::wstring params{ wss.str() };
shell_info.cbSize = sizeof(SHELLEXECUTEINFO);
shell_info.fMask = SEE_MASK_ASYNCOK | SEE_MASK_NO_CONSOLE;
shell_info.hwnd = NULL;
shell_info.lpVerb = NULL;
shell_info.lpFile = L"C:\\ATDK\\Executor"; // Path separator on Windows is \
shell_info.lpParameters = params.c_str(); // Use string object that survives the call
shell_info.lpDirectory = NULL;
shell_info.nShow = SW_MINIMIZE;
shell_info.hInstApp = NULL;
ShellExecuteEx(&shell_info); 注意你的第二个电话
ShellExecute(NULL, NULL, L"C:\\ATDK\\Executor", wss.str().c_str(), NULL, NULL);可靠的工作。即使wss.str()仍然返回一个临时表达式,它在完整表达式结束之前仍然有效(即在整个函数调用过程中)。
https://stackoverflow.com/questions/42155265
复制相似问题