Kevin Hou
Kevin Hou

Reputation: 1

How to convert CreateProcess to ShellExecuteEx?

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!!!

Upvotes: 0

Views: 1393

Answers (1)

NuSkooler
NuSkooler

Reputation: 5525

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.

Upvotes: 1

Related Questions