如何将CreateProcess转换为ShellExecuteEx?

发布于 2024-09-19 06:49:24 字数 1486 浏览 7 评论 0原文

我使用 MS detour 库来挂钩 CreateProcess,它在 Win7 上运行良好。然后我想用 ShellExecuteEx 替换迂回的 CreateProcess,以便我可以使用“runas”以管理员权限静默运行该程序。不幸的是,参数类型不一样。

这是函数签名:

CreateProcess( 
         LPCWSTR lpszImageName, 
         LPCWSTR lpszCmdLine, 
         LPSECURITY_ATTRIBUTES lpsaProcess, 
         LPSECURITY_ATTRIBUTES lpsaThread, 
         BOOL fInheritHandles, 
         DWORD fdwCreate, 
         LPVOID lpvEnvironment, 
         LPWSTR lpszCurDir, 
         LPSTARTUPINFOW lpsiStartInfo, 
         LPPROCESS_INFORMATION lppiProcInfo 
      ); 

这里我调用 ShellExecuteEx:

SHELLEXECUTEINFO ShExecInfo;
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = 0 ;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = lpszImageName; // this is obatined within deboured CreateProcess.
ShExecInfo.lpParameters = ""; 
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL; 
ShellExecuteEx(&ShExecInfo);

但是,这个赋值有错误: ShExecInfo.lpFile = lpszImageName; VC2005 抱怨数据类型不一样:“无法从 const unsigned char * 转换为 const char *”

但CreateProcess 中参数lpszImageName 的数据类型为LPCWSTR,ShExecInfo.lpFile 为LPCTSTR

如果我使用 (const unsigned char*) 转换 lpszImageName,则 lpFile 的值只是 lpszImageName 值的第一个字母。

例如,如果 lpszImageName 是“C:\windows\system32\cmd.exe”。赋值后,lpFile 值仅为“C”。

如何从 LPCWSTR 转换为 LPCTSTR?或者我怎样才能进行转换?

谢谢!!!

I use MS detour library to hook CreateProcess and it works fine on Win7. Then I want to replace the detoured CreateProcess with ShellExecuteEx so that I can use 'runas' to silently run the program with administrator priviledge. Unfortunately, the parameter type is not the same.

This is the function signature:

CreateProcess( 
         LPCWSTR lpszImageName, 
         LPCWSTR lpszCmdLine, 
         LPSECURITY_ATTRIBUTES lpsaProcess, 
         LPSECURITY_ATTRIBUTES lpsaThread, 
         BOOL fInheritHandles, 
         DWORD fdwCreate, 
         LPVOID lpvEnvironment, 
         LPWSTR lpszCurDir, 
         LPSTARTUPINFOW lpsiStartInfo, 
         LPPROCESS_INFORMATION lppiProcInfo 
      ); 

Here I call ShellExecuteEx:

SHELLEXECUTEINFO ShExecInfo;
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = 0 ;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = lpszImageName; // this is obatined within deboured CreateProcess.
ShExecInfo.lpParameters = ""; 
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL; 
ShellExecuteEx(&ShExecInfo);

However, this assignment has error: ShExecInfo.lpFile = lpszImageName;
VC2005 complaints the data type is not the same: "cannot convert from const unsigned char * to const char *".

But the data type is LPCWSTR for parameter lpszImageName in CreateProcess and ShExecInfo.lpFile is LPCTSTR.

If I use (const unsigned char*) to convert lpszImageName, the value of lpFile is only the first letter of lpszImageName value.

For example, if lpszImageName is "C:\windows\system32\cmd.exe". After assignment, lpFile value is only 'C'.

How can I convert from LPCWSTR to LPCTSTR? Or How could I do the convertion?

Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摘星┃星的人 2024-09-26 06:49:24

看起来您已经挂接了 CreateProcessW() 并尝试将其映射到 ShellExecuteExA()。相反,映射到 ShellExecuteExW() 并使用 SHELLEXECUTEINFOW 结构,该结构使用宽 (LPCWSTR) 字符串。

It looks like you've hooked CreateProcessW() and are attempting to map it to ShellExecuteExA(). Instead, map to ShellExecuteExW() and use the SHELLEXECUTEINFOW structure which uses wide (LPCWSTR) strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文