首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ShellExecuteEx不传递long命令

ShellExecuteEx不传递long命令
EN

Stack Overflow用户
提问于 2017-02-10 09:00:34
回答 1查看 347关注 0票数 0

当我将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代码。

代码语言:javascript
复制
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!
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-10 11:19:12

你的窃听器在这里:

代码语言:javascript
复制
shell_info.lpParameters = (LPCWSTR)wss.str().c_str();

wss.str()返回一个临时对象,该对象在创建该对象的完整表达式结束后不再存在。在这一点之后使用它是未定义的行为。

要解决这个问题,您必须构造一个std::wstring对象,该对象的生存期足够长,以便调用ShellExecuteEx返回。

代码语言:javascript
复制
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); 

注意你的第二个电话

代码语言:javascript
复制
ShellExecute(NULL, NULL, L"C:\\ATDK\\Executor", wss.str().c_str(), NULL, NULL);

可靠的工作。即使wss.str()仍然返回一个临时表达式,它在完整表达式结束之前仍然有效(即在整个函数调用过程中)。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42155265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档