Discussion:
Please help with CreateProcess and Executing VBS Script
(too old to reply)
James
2009-02-07 19:35:37 UTC
Permalink
Hi there,

I'm having problems executing a VBS Script through CreateProcess. Below is the sample code that I am using. Please note that the application is built with UNICODE enabled. The main problem is that, the script is never executed and "WaitForSingleObject" nevers completes execution. I'm not getting any error messages, but rather my application just hangs. Can someone please help me with correcting this - as all I want is to execute the vbs script. Many thanks for your help

STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof STARTUPINFO;
PROCESS_INFORMATION pi;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;



CreateProcess(_T("C:\\Windows\\system32\\cmd.exe"), _T("c:/myfile.vbs"), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exit_code;
GetExitCodeProcess(pi.hProcess, &exit_code);
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );



Thanks,
James
Scott Seligman
2009-02-07 19:42:47 UTC
Permalink
Post by James
CreateProcess(_T("C:\\Windows\\system32\\cmd.exe"),
_T("c:/myfile.vbs"), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
You should use either cscript.exe or wscript.exe to run a script file.
Also, instead of the string you're passing, you'll need to pass a real
path (i.e., "c:\\myfile.vbs")
--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
Music hath charms to soothe the savage breast, to soften rocks, or
bend a knotted oak.
-- The Mourning Bride by William Congreve
James
2009-02-07 22:27:00 UTC
Permalink
Hi Scott,

I've tried what you suggested and it still doesn't work. I changed it to
the below line:
CreateProcess(_T("cscript //NOLOGO c:/myfile.vbs"), NULL, NULL, NULL, FALSE,
0, NULL, NULL, &si, &pi);
Post by Scott Seligman
Post by James
CreateProcess(_T("C:\\Windows\\system32\\cmd.exe"),
_T("c:/myfile.vbs"), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
You should use either cscript.exe or wscript.exe to run a script file.
Also, instead of the string you're passing, you'll need to pass a real
path (i.e., "c:\\myfile.vbs")
--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
Music hath charms to soothe the savage breast, to soften rocks, or
bend a knotted oak.
-- The Mourning Bride by William Congreve
James
2009-02-07 23:06:14 UTC
Permalink
I solved it:-)
Basically, internally CreateProcess modifies the second parameter passed to it. The solution was to simply store the second parameter into a Variable and then pass the variable to
CreateProcess like so:
TCHAR appCmdLine[] = TEXT("cscript.exe //NOLOGO c:/myfile.vbs");
CreateProcess(NULL, appCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

Thanks,
Jim
"James" <***@yahoo.co.uk> wrote in message news:Obz%23$***@TK2MSFTNGP05.phx.gbl...
Hi there,

I'm having problems executing a VBS Script through CreateProcess. Below is the sample code that I am using. Please note that the application is built with UNICODE enabled. The main problem is that, the script is never executed and "WaitForSingleObject" nevers completes execution. I'm not getting any error messages, but rather my application just hangs. Can someone please help me with correcting this - as all I want is to execute the vbs script. Many thanks for your help

STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof STARTUPINFO;
PROCESS_INFORMATION pi;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;



CreateProcess(_T("C:\\Windows\\system32\\cmd.exe"), _T("c:/myfile.vbs"), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exit_code;
GetExitCodeProcess(pi.hProcess, &exit_code);
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );



Thanks,
James
d***@gmail.com
2017-02-21 22:35:51 UTC
Permalink
Post by James
I solved it:-)
Basically, internally CreateProcess modifies the
second parameter passed to it.  The solution was to simply store the second
parameter into a Variable and then pass the variable to
TCHAR appCmdLine[] = TEXT("cscript.exe //NOLOGO
c:/myfile.vbs");
CreateProcess(NULL, appCmdLine, NULL, NULL, FALSE, 0, NULL,
NULL, &si, &pi);
 
Thanks,
Jim
Hi there,
 
I'm having problems executing a VBS Script through
CreateProcess.  Below is the sample code that I am using.  Please
note that the application is built with UNICODE enabled.  The main
problem is that, the script is never executed and "WaitForSingleObject" nevers
completes execution.  I'm not getting any error messages, but rather my
application just hangs.  Can someone please help me with correcting this
- as all I want is to execute the vbs script.  Many thanks for your
help
 
STARTUPINFO si;
ZeroMemory(&si,
sizeof(si));
si.cb = sizeof STARTUPINFO;
PROCESS_INFORMATION
pi;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow =
SW_HIDE;
 
 
 
CreateProcess(_T("C:\\Windows\\system32\\cmd.exe"),
_T("c:/myfile.vbs"), NULL, NULL, FALSE, 0, NULL, NULL, &si,
&pi);
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD
exit_code;
GetExitCodeProcess(pi.hProcess,
&exit_code);
    CloseHandle( pi.hProcess
);
    CloseHandle( pi.hThread );
 
 
Thanks,
James
Thanks James, I was facing the same issue. Your post helped me a lot.
Cheers :)

Loading...