Advertisement

opening an EXE from a program

Started by February 01, 2005 08:32 PM
14 comments, last by unfinished 19 years, 9 months ago
I've seen it done before, I just don't know how to do it? Simple situation, say I have a program that generates random numbers and writes them to a file. Say I have another program that reads numbers from a file. Is there any way that I can have the generator open the reader program when it's done? If not, can I write a program that opens the generator, and then the reader when the generator is done? speedy reply's appreciated! thnx!
What are you developing in? Windows? Unix?

If Windows, look at CreateProcess

If *nix, look at the exec* (or here) family of functions.


Regards,
jflanglois
Advertisement
winexec always worked for me

(and he wants to run a .exe so I'm pretty sure he's after windows functions not *nix)
Take a look at CreateProcess
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocess.asp

[edited] Omg, I'm too slow, 2 guys posted before me :/ [edited]
[edited (second time)]
"The WinExec function runs the specified application.

Note This function is provided only for compatibility with 16-bit Windows. Applications should use the CreateProcess function."

link > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/winexec.asp [edited]
error C2065: 'signature' : undeclared identifier
Quote: Original post by kaysik
winexec always worked for me

(and he wants to run a .exe so I'm pretty sure he's after windows functions not *nix)

You're absolutely right. Thanks for pointing it out.
ok, you guys are awesome - createprocess works like a charm.

I'm going to push my luck here and ask a follow up question:

If my generator program requires the user input a file name and I store it as char filename[20], how do I send it as a parameter to the reader program? More simply put, how do I make sure my reader will recognise it as argv[1]?
Advertisement
If you called CreateProcess( "prog.exe", filename, etc... ) then filename would be argv[1] without a doubt. argv[0] is always the process name, so the first command line arg passed is argv[1], the next is argv[2] and so on.


-SirKnight
Concatenate the executable name with that to the lpCommandLine parameter.

char filename[20] = "inputfile";char *cmd = new char[80];cmd[0] = 0;strcat( cmd, "test.exe " );strcat( cmd, filename );

Or somesuch...

Regards,
jflanglois
Quote: Original post by SirKnight
If you called CreateProcess( "prog.exe", filename, etc... ) then filename would be argv[1] without a doubt. argv[0] is always the process name, so the first command line arg passed is argv[1], the next is argv[2] and so on.


-SirKnight

Quote: MSDN
If both lpApplicationName and lpCommandLine are non-NULL, the null-terminated string pointed to by lpApplicationName specifies the module to execute, and the null-terminated string pointed to by lpCommandLine specifies the command line. The new process can use GetCommandLine to retrieve the entire command line. Console processes written in C can use the argc and argv arguments to parse the command line. Because argv[0] is the module name, C programmers generally repeat the module name as the first token in the command line.


As a test I have two terribly written applications:
test30:
#include <iostream>#include <windows.h>using namespace std;int main() {	char filename[20] = "inputfile";	char *cmd = new char[80];	cmd[0] = 0;	strcat( cmd, "shouldbeprocessnameright? " );	strcat( cmd, filename );	STARTUPINFO si = { 0, };	si.cb = sizeof( si );	PROCESS_INFORMATION pi = { 0, };	CreateProcess( "..\\test31\\Debug\\test31.exe", cmd, NULL, NULL, FALSE,		NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi );	delete [] cmd; // ZOMG! ZOMG! I almost forgot...	cin.get();	return 0;}


test31:
#include "stdio.h"#include <process.h>int main( int argc, char *argv[] ) {	if ( argc == 2 ) {		puts( argv[0] );		puts( argv[1] );	}	system( "pause" );	return 0;}


Output:
Quote: shouldbeprocessnameright?
inputfile
Press any key to continue . . .
If you're in for a learning experience:

1. Create a named/anonymous pipe (CreatePipe)
2. Duplicate the readhandle to make a noninheriting handle) (DuplicateHandle)
3. close the original readhandle (CloseHandle)
4. pass the duplicate readhandle in the processinfo struct (e.g. in the stdin member) of the CreateProcess
5. Start writing to to writehandle. (WriteFile)
6. Read from the readhandle in the client.

Voila, you just created a server without using a file on disk...

Cheers

This topic is closed to new replies.

Advertisement