 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
E B Guest
|
Posted: Wed May 09, 2007 8:10 am Post subject: How to run a script from cpp code |
|
|
Hi,
I'm looking for a way to run either a .js or a .vbs from my app code
(which is written in cpp).
I have tried ShellExecute(Ex) and CreateProcess (with various
parameters) but none of them seems to work (at least I did not find a
way to make them worked).
Does anybody have an example to help ?
thank you |
|
| Back to top |
|
 |
Alan Bellingham Guest
|
Posted: Wed May 09, 2007 3:06 pm Post subject: Re: How to run a script from cpp code |
|
|
E B <eric.britz (AT) libertysurf (DOT) fr> wrote:
| Quote: | I'm looking for a way to run either a .js or a .vbs from my app code
(which is written in cpp).
I have tried ShellExecute(Ex) and CreateProcess (with various
parameters) but none of them seems to work (at least I did not find a
way to make them worked).
|
First question - can you get them to work from the command line? If you
can't, then CreateProcess is going to have difficulties.
Alan Bellingham
--
ACCU Conference 2008: 2-5 April 2008 - Oxford (probably), UK |
|
| Back to top |
|
 |
dhoke Guest
|
Posted: Wed May 09, 2007 6:41 pm Post subject: Re: How to run a script from cpp code |
|
|
Haven't tried this - but I believe the script commands for windows script
host are cscript and wscript.
Even if you can execute them from the command line, I think its the command
window that is looking up the type (by file extension) and finding the
default execution environment, if one is assigned to the file type.
You probably have to execute either cscript or wscript with CreateProcess,
passing your script name with its arguments, as the arguments to one of
those, via the CreateProcess API.
"Alan Bellingham" <alan (AT) lspace (DOT) org> wrote in message
news:o7734353ubp2ob7jscv5fa1q5rbehd5h5o (AT) 4ax (DOT) com...
| Quote: | E B <eric.britz (AT) libertysurf (DOT) fr> wrote:
I'm looking for a way to run either a .js or a .vbs from my app code
(which is written in cpp).
I have tried ShellExecute(Ex) and CreateProcess (with various
parameters) but none of them seems to work (at least I did not find a
way to make them worked).
First question - can you get them to work from the command line? If you
can't, then CreateProcess is going to have difficulties.
Alan Bellingham
--
ACCU Conference 2008: 2-5 April 2008 - Oxford (probably), UK |
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed May 09, 2007 9:34 pm Post subject: Re: How to run a script from cpp code |
|
|
"E B" <eric.britz (AT) libertysurf (DOT) fr> wrote in message
news:46418155 (AT) newsgroups (DOT) borland.com...
| Quote: | I'm looking for a way to run either a .js or a .vbs from my app
code (which is written in cpp).
|
If you want to run the script inside your application instead of as an
external process, then have a look at IActiveScript and its related
interfaces:
Microsoft Windows Script Interfaces-Introduction
http://msdn2.microsoft.com/en-us/library/t9d4xf28.aspx
| Quote: | I have tried ShellExecute(Ex) and CreateProcess (with various
parameters) but none of them seems to work (at least I did not find
a
way to make them worked).
|
That approach would only ork if you have Windows Script Host
installed.
Gambit |
|
| Back to top |
|
 |
E B Guest
|
Posted: Fri May 11, 2007 6:15 pm Post subject: Re: How to run a script from cpp code |
|
|
I tried the following code which works fine for running an external .bat
or .exe, but it doe snot work for a .js
Can you tell me why ?
std::string tmp = "\"" + procfile + "\" " + param; //procfile is the
full path of the .js file I intend to run
if( !CreateProcess( NULL, // No module name (use command line)
(char*)tmp.c_str(), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
output_dir.c_str(), // Don not Use parent's starting
directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
ERROR_STACK
<< MsgFormat("Can not create process.).")
;
}
else
{
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
} |
|
| Back to top |
|
 |
dhoke Guest
|
Posted: Fri May 11, 2007 9:25 pm Post subject: Re: How to run a script from cpp code |
|
|
If you haven't (the snippet you gave doesn't), try checking the value of
GetLastError() on failure, and find its associated text.
Error codes are often a clue as to why the call failed. :)
I also sometimes find it useful to get sysinternals (now microsoft's)
filemon file monitoring program, set the filters to the program using
CreateProcess(), and see what file/directory reference attempts are made. I
often find clues that way as well.
I'm a little surprised that your snippet works for ".bat"...
"E B" <eric.britz (AT) libertysurf (DOT) fr> wrote in message
news:46446d47 (AT) newsgroups (DOT) borland.com...
| Quote: | I tried the following code which works fine for running an external .bat
or .exe, but it doe snot work for a .js
Can you tell me why ?
std::string tmp = "\"" + procfile + "\" " + param; //procfile is the
full path of the .js file I intend to run
if( !CreateProcess( NULL, // No module name (use command line)
(char*)tmp.c_str(), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
output_dir.c_str(), // Don not Use parent's starting
directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
ERROR_STACK
MsgFormat("Can not create process.).")
;
}
else
{
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
} |
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Fri May 11, 2007 9:49 pm Post subject: Re: How to run a script from cpp code |
|
|
"E B" <eric.britz (AT) libertysurf (DOT) fr> wrote in message
news:46446d47 (AT) newsgroups (DOT) borland.com...
| Quote: | I tried the following code which works fine for running an
external .bat or .exe, but it doe snot work for a .js
|
It is not supposed to. CreateProcess() only works on actual
executable files. A .js file is not executable by itself. It is just
a text file that you must load into a separate script interpreter,
such as wscript.exe or wscript.exe. You can use ShellExecute() to
load any file, .js or otherwise, into its default program, though. Of
course, if the user's default program edits the file instead of
executing it, that won't help you any.
Did you try my earlier suggestion yet about using the IAtiveScript
interface to run your scripts?
Gambit |
|
| Back to top |
|
 |
E B Guest
|
Posted: Mon May 14, 2007 10:43 pm Post subject: Re: How to run a script from cpp code |
|
|
I'm trying your solution from now, but this may need some
restructuration of my application code.
in the meanwhile I found a solution to my problem using shellexecuteex
(which works fine with .bat, .js and .exe)
here is the code:
{
SHELLEXECUTEINFO ExecInfo;
ExecInfo.cbSize=sizeof(ExecInfo);
ExecInfo.fMask=SEE_MASK_NOCLOSEPROCESS;
ExecInfo.lpVerb = 0;//"Open" if not specified;
ExecInfo.lpFile = procfile.c_str();
ExecInfo.lpParameters = param.c_str();
ExecInfo.lpDirectory = working_dir.c_str();
#ifdef _DEBUG
ExecInfo.nShow = SW_SHOW;
#endif
#ifndef _DEBUG
ExecInfo.nShow = SW_HIDE;
#endif
if(!ShellExecuteEx(&ExecInfo))
{
ERROR_STACK
<< MsgFormat("Can not create process to run product line
step:\n\t%s\n\t%s\n\terror code %d.)."_locate_
, tmp.c_str()
, param.c_str()
, ExecInfo.hInstApp
)
;
}
else
{
WaitForSingleObject( ExecInfo.hProcess, INFINITE);
CloseHandle(ExecInfo.hProcess);
}
} |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue May 15, 2007 4:03 am Post subject: Re: How to run a script from cpp code |
|
|
"E B" <eric.britz (AT) libertysurf (DOT) fr> wrote in message
news:4648a06d$1 (AT) newsgroups (DOT) borland.com...
| Quote: | I'm trying your solution from now, but this may need
some restructuration of my application code.
|
Not really. You have the filename for a script you want to run.
Simply load the script text into memory, instantiate the IActiveScript
object, pass it the memory block, and let it run. At least this way,
you get notifications of the script's progress as well as its result,
if any. You don't get any of that with ShellExecute/Ex() or
CreateProcess().
Gambit |
|
| Back to top |
|
 |
Ron Sawyer Guest
|
Posted: Tue May 15, 2007 4:10 am Post subject: Re: How to run a script from cpp code |
|
|
Thank you for this post. It solved a major problem I have been having.
"dhoke" <dhoke.nojunk@east-shore.com> wrote in message
news:4641cffc$1 (AT) newsgroups (DOT) borland.com...
| Quote: | Haven't tried this - but I believe the script commands for windows script
host are cscript and wscript.
Even if you can execute them from the command line, I think its the
command
window that is looking up the type (by file extension) and finding the
default execution environment, if one is assigned to the file type.
You probably have to execute either cscript or wscript with CreateProcess,
passing your script name with its arguments, as the arguments to one of
those, via the CreateProcess API. |
|
|
| Back to top |
|
 |
E B Guest
|
Posted: Wed May 16, 2007 1:27 am Post subject: Re: How to run a script from cpp code |
|
|
Remy Lebeau (TeamB) wrote:
thank you for the information.
Will I need to link my app to a specific DLL or an active X ? At the
moment my app is stand alone, it does not depend on any dlls or anything
else. THis really eases the distribution / installation process. I'm
wondering if this will still be the case if I use the IActiveScript ? |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed May 16, 2007 1:39 am Post subject: Re: How to run a script from cpp code |
|
|
"E B" <eric.britz (AT) libertysurf (DOT) fr> wrote in message
news:464a1894 (AT) newsgroups (DOT) borland.com...
| Quote: | Will I need to link my app to a specific DLL or an active X ?
|
IActiveScript is an ActiveX control, but you don't need a type library
to use it. Just include the appropriate header files into your code
to get the interface declarations, and then call
CoInitialize/Uninitialize() and CoCreateInstance() at runtime when
needed.
| Quote: | At the moment my app is stand alone, it does not depend on
any dlls or anything else.
|
The IActiveScript object and Windows Script Host are built into the
OS, so they should always be available for use without you needing to
distribute anything yourself.
Gambit |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|