EnCoCo Services

Tech Tips

CreateProcess

The Task is to start a batch file (from inside a program) and supply some paths for further handling.

It is suggested, you use the function CreateProcess and that you quote the paths, which include spaces.

Following is the call syntax of this function. The (in)complete description can be found here and a supplement in Microsoft Knowledge Base Article - 175986

BOOL CreateProcess(

    LPCTSTR  lpApplicationName,	// pointer to name of executable module 
    LPTSTR  lpCommandLine,	// pointer to command line string
    LPSECURITY_ATTRIBUTES  lpProcessAttributes,	// pointer to process security attributes 
    LPSECURITY_ATTRIBUTES  lpThreadAttributes,	// pointer to thread security attributes 
    BOOL  bInheritHandles,	// handle inheritance flag 
    DWORD  dwCreationFlags,	// creation flags 
    LPVOID  lpEnvironment,	// pointer to new environment block 
    LPCTSTR  lpCurrentDirectory,	// pointer to current directory name 
    LPSTARTUPINFO  lpStartupInfo,	// pointer to STARTUPINFO 
    LPPROCESS_INFORMATION  lpProcessInformation 	// pointer to PROCESS_INFORMATION  
   );	

Tests

The test focuses on the batch file "path0\Prog.bat", where path0 is e.g. c:\Programs\Company with Programs and Company substituted accordingly.

Test results:

No lpCommandLine 1) no special character spaces in all paths & in path0
A: path0\Prog.bat works doesn't work 3) 5) doesn't work 3)
B: "path0\Prog.bat" works works doesn't work 3)
C: "path0\Prog.bat" path1 path2 works works 6) doesn't work 3)
D: path0\Prog.bat "path1" "path2" works doesn't work 3) 5) doesn't work 3)
E: "path0\Prog.bat" "path1" "path2" doesn't work 3) doesn't work 3) doesn't work 3)
F: c:\"Program Files"\Company\Prog.bat 2) doesn't work 3) doesn't work 3) doesn't work 4)

1) If you think in C, you must escape sepcial characters to generate these strings!
2) Suggested by MS
3) Works for EXE files (Prog.exe instead of Prog.bat).
4) & in Company.
5) This example is dangerous, because someone could place an EXE file (c:\Program.exe with path c:\Program Files\....) to be executed by our program.
6) Has more then 2 parameters.

Workaround:

  • Set the Current Directory to the path of Prog.bat.
  • Set lpCommandLine to the file name Prog.bat (no Path).
  • Set lpCurrentDirectory to the Current Directory (path of Prog.bat).
  • Append extra parameters to lpEnvironment.
    (use GetEnvironmentStrings, append your parameters and sort the block! This also avoids problems with quoted paths inside Prog.bat)
  • Now call CreateProcess(0, lpCommandLine.....lpEnvironment, lpCurrentDirectory...)
  • If you added your working directory to the environment (WorkDir=....), you can change the current directory to that inside Prog.bat (cd %WorkDir%). Be sure to handle the drive separately.

Tests were conducted on MS Windows NT 4 SR6a and MS Windows XP Home SR1 on 2003-10-17. Behavier and embedded links are subject to changes by MS (Microsoft)!



Image Investigator

Organize your photos with the help of our Image Investigator. Search your image library for similar images or retrieve images by painting a sample. Look if you have duplicates in your image library.

Copyright 2008 © EnCoCo - All Rights Reserved