ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > Drag&Drop Robot

Suggestion: Use Comspec

<< < (3/5) > >>

mouser:
right ok, well i'm quite happy to check for the launching of .bat files and use %comspec% when launching them.. the only real question is whether i should just do that always, or do i need an option to let the user say they do NOT want %comspec% being used, and would prefer default of cmd.exe.

second question, assuming i want to just let the OS do it's default when an alternate command shell isn't specified, can i check %comspec% and tell if a replacement shell has been configured.  i.e. if i see cmd.exe file in %comspec% shall i just assume its default and let CreateProcess handle the .bat file normally.

third and final question -- is there any value in letting the user manually specify the command shell to use when launching .bat files instead of checking %comspec%.  this would be useful for people who want to use an alternate shell not configured through %comspec%

myarmor:
Quoted from the Win32 SDK documentation, CreateProcess:
To run a batch file, you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the name of the batch file.

It is the cmd.exe part in the text above that is changed to the contents of the comspec environment variable.

// This simply expands the comspec environment variable, then tries to execute a batch file using the expanded contents
// as the command processor.
// Note, this tidbit looks like h..., has no errorchecking, no redirection, and probably has lots of issues,
// but its just a demonstration.

#include <windows.h>
int main(void){
   STARTUPINFO inf;
   char cmdspecstr[MAX_PATH];
   PROCESS_INFORMATION pinf;
   ZeroMemory(cmdspecstr,sizeof(cmdspecstr));
   if (GetEnvironmentVariable("COMSPEC",(LPTSTR)&cmdspecstr,MAX_PATH)==0){ //In case we have an error of some kind while getting the string.          
          strcpy(cmdspecstr,"c:\\windows\\system32\\cmd.exe"); //here we could've retrieved the systemroot environment variable instead of pointing to a hardcoded path.
        }
   ZeroMemory(&inf,sizeof(inf));
   inf.cb=sizeof(inf);
   CreateProcess(cmdspecstr," /c c:\\tmp\\test.bat",NULL,NULL,true,
       NORMAL_PRIORITY_CLASS,NULL,NULL,&inf,&pinf);
       return 0;
}

myarmor:
right ok, well i'm quite happy to check for the launching of .bat files and use %comspec% when launching them.. the only real question is whether i should just do that always, or do i need an option to let the user say they do NOT want %comspec% being used, and would prefer default of cmd.exe.
-mouser (June 06, 2008, 06:47 PM)
--- End quote ---

The default value of %comspec% IS:
%systemroot%\system32\cmd.exe

try to type SET on the commandline.

mouser:
To run a batch file, you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the name of the batch file.
--- End quote ---

ok well that explains some of our crosstalk.. you probably wont be surprised to hear that i do not do that.  i just set applicationname to the batch file name, and the OS is figuring out to launch cmd.exe.

i'll try to get this comspec support in next version, once we decide the answers to my previous questions.

thanks for the sample code, i appreciate it.  :up:

myarmor:
So really, this is no fault of mouser's :) - pretty annoying anyway, as it sounds like .cmd and .bat need special manual handling in order to use %ComSpec%...
-f0dder (June 06, 2008, 06:30 PM)
--- End quote ---
No more than the code above (and that one includes an execute example, not just the code to extract the
comspec, then refer to it in the createprocess statement).

You would use comspec for batch files, i.e .bat and .cmd as you guessed.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version