Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CreateProcessW not honoring commandline [duplicate]

I am trying to implement CreateProcessW within a dll so that the user can launch an application in a separate process.

For starters I am hardcoding the commands in the code until I figure it out..

I've got

STARTUPINFO si = {sizeof(STARTUPINFO), 0};
si.cb = sizeof(si);
PROCESS_INFORMATION pi = {0};
LPCTSTR AppName=L"c:\\utilities\\depends.exe";
LPTSTR Command = L"c:\\utilities\\tee.exe";
if (CreateProcessW(AppName, Command, 0, 0, 0, CREATE_DEFAULT_ERROR_MODE, 0, 0, &si, &pi)) {
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
        return GX_RESULT_OK;
    } else {
        .. show error msg
    }

This will launch Depends but won't open Tee.exe. There's no error, it just ignores the command line parameter. The parameters are correct and I can run it at the run prompt and it works fine. If I leave AppName blank and specify Depends.exe as the Command parameter it also works, but if I specify

LPTSTR Command = L"c:\\utilities\\depends.exe c:\\utilities\\tee.exe";

I get Error 3: "The system cannot find the path specified".

Also, by specifying the lpCurrentDirectory parameter it is likewise ignored.

like image 863
marcp Avatar asked Oct 19 '22 18:10

marcp


1 Answers

You must to supply the executable in the command

  • Appname should contain the full path to the executable
  • Command should contain also the argv[0]

if you want to open file t.txt with notepad than you can give as follows:

  • Appname = "c:/windows/notepad.exe";
  • command = "notepad c:/temp/t.txt";

You doesn't even must to supply the real program name, even fake name will do the job, since it is only a place holder.

like this: command = "fake c:/temp/t.txt";

now in notepad.exe:

  • argv[0] = "notepad";
  • argv[1] = "c:/temp/t.txt";

See this full example:

#include <Windows.h>
#include <iostream>

using namespace std;

int main(){
    STARTUPINFO si = {sizeof(STARTUPINFO), 0};
    si.cb = sizeof(si);
    PROCESS_INFORMATION pi = {0};
    LPTSTR AppName=L"C:/Windows/notepad.exe";
    wchar_t Command[] = L"notepad C:/Temp/t.txt"; 
    DWORD res = CreateProcess(AppName, Command, 0, 0, 0, CREATE_DEFAULT_ERROR_MODE, 0, 0, &si, &pi);
    if (res) {
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
        return 0;
    } else {
        cerr<<"error..."<<GetLastError()<<endl;
    }; 
    return 0;
}
like image 192
SHR Avatar answered Oct 22 '22 13:10

SHR