3

I am trying to execute a dos command using CreateProcess function :

 LPWSTR cmd=(LPWSTR)QString("C:\\windows\\system32\\cmd.exe  subst " + DLetter+"  \""+mountPath+"\"").utf16();



        STARTUPINFO si;
        PROCESS_INFORMATION pi;
        ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );

        if ( CreateProcessW(0,     // Application name
                           cmd,                 // Application arguments
                           NULL,
                           NULL,
                           TRUE,
                           0,
                           NULL,
                           L"C:\\windows\\system32",          // Working directory
                           &si,
                           &pi) == TRUE)
        { ...

it give as last error 3 = ERROR_PATH_NOT_FOUND, when I separate the application path "C:\\windows\\system32\\cmd.exe" from the command it shows the console without executing my subst command.

Any help will be appreciated.

Oumaya
  • 655
  • 4
  • 18
  • 43

2 Answers2

5

You need to include either /C or /K in the options to cmd.exe.

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains

Without one these options, the subst command that you pass is simply ignored.

Having said that, subst, at least on my Windows 7 box, is not implemented inside cmd.exe. It is a separate executable. So you can invoke it directly and bypass cmd.exe completely.

Regarding your call to CreateProcess I have the following comments:

  1. Don't include the path C:\\windows\\system32. Just invoke subst.exe and let the system locate the executable using the standard search path.
  2. Pass FALSE for bInheritHandles. You aren't passing any handles to the new process and so you don't need the new process to inherit your handles.
  3. Pass NULL as the working directory. There's just no need to specify it here.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • thank you David it works I invoked subst.exe with my arguments but I didn't find the mounted drive under Computer with C:/ and D:/ , did subst run with an admin rights so that's why I didn't find my new drive ?(I am a simple user) – Oumaya Dec 10 '12 at 13:42
  • I'm not sure. I think that's a different question. – David Heffernan Dec 10 '12 at 13:43
  • 3
    @DavidHeffernan: even better, dont use the `subst` executable to manipulate drive mappings. Use Win32 API functions directly, such as `DosDefineDevice()`, `WNetAddConnection...()`, `WNetCancelConnection...()`, etc. – Remy Lebeau Dec 10 '12 at 18:30
  • Er yes, that's a much better idea, @Remy – David Heffernan Dec 10 '12 at 18:31
1

Try using

"C:\\windows\\system32\\cmd.exe /c subst " + DLetter+"  \""+mountPath+"\""

instead. CMD won't accept arguments without either /c or /k, and unless you want to see the output in the console window, just use /c.

Bali C
  • 30,582
  • 35
  • 123
  • 152