Calling another executable
-
I have 1 exe which needs to call another exe, passing some data. How best to call another exe within my first exe?:confused:
PROCESS_INFORMATION pInfo; STARTUPINFO sInfo; DWORD exitCode; sInfo.cb = sizeof(STARTUPINFO); sInfo.lpReserved = NULL; sInfo.lpReserved2 = NULL; sInfo.cbReserved2 = 0; sInfo.lpDesktop = NULL; sInfo.lpTitle = NULL; sInfo.dwFlags = 0; sInfo.dwX = 0; sInfo.dwY = 0; sInfo.dwFillAttribute = 0; sInfo.wShowWindow = SW_SHOW; if (!CreateProcess(NULL, "path\\second.exe", NULL, NULL, FALSE, 0, NULL, NULL, &sInfo, &pInfo)) { printf("ERROR: Cannot launch child process\n"); exit(1); } :) Cheers
-
I have 1 exe which needs to call another exe, passing some data. How best to call another exe within my first exe?:confused:
I can think of three ways: 1) Use Shared memory 2) Passing a WM_DATA message to your rxing applications window (This also works for passing data between 16 & 32 bit apps). 3) Make the rxing exe a COM server. What's your level of expertise? Do you need more info or is this enough to get you started? "You can lead a horse to water but a pencil has to be lead"
-
I have 1 exe which needs to call another exe, passing some data. How best to call another exe within my first exe?:confused:
-
Oops, forgot to ask the obvious question (Just saw Prasanth's post :-O) Do you need to pass the data at application launch? (Prasanth's solution) or at run-time? (my suggestions)
I would like to see sample code on Prasanth's solution. What if I need to pass an object, from 1 process to another. Is my object Thread-Safe or do I need to sync data? I'm use to COM DLL's running under MTS & some connection points experience. Cheers for all the help! Gerry.
-
I would like to see sample code on Prasanth's solution. What if I need to pass an object, from 1 process to another. Is my object Thread-Safe or do I need to sync data? I'm use to COM DLL's running under MTS & some connection points experience. Cheers for all the help! Gerry.
Hi Gerry, I need to know a few more things to be able to help. Re: I would like to see sample code on Prasanth's solution. It's there in his post. Prasanth is spawning a process from another process. Is this what you want to do? Re: What if I need to pass an object, from 1 process to another. C++ object? COM object? OS handle object? Binary data 'object'? Other(?) Do you pass the object at from one process to the other when the second process is started? or.. Do you pass the object when the second process is already running? Re: Is my object Thread-Safe or do I need to sync data? Depends on the object (See last comment) and on what you intend to do with it. If you can describe what exactly you want to do, I will be hopefully be able to give you some better advice.
-
Hi Gerry, I need to know a few more things to be able to help. Re: I would like to see sample code on Prasanth's solution. It's there in his post. Prasanth is spawning a process from another process. Is this what you want to do? Re: What if I need to pass an object, from 1 process to another. C++ object? COM object? OS handle object? Binary data 'object'? Other(?) Do you pass the object at from one process to the other when the second process is started? or.. Do you pass the object when the second process is already running? Re: Is my object Thread-Safe or do I need to sync data? Depends on the object (See last comment) and on what you intend to do with it. If you can describe what exactly you want to do, I will be hopefully be able to give you some better advice.
It's a C++ object. I want to learn more about Calling another Exe from my main exe, then letting it carry out some task, & then returning. I would passbyref the object when the second process kicked off. Not sure to create a new process or spawn another Thread & sync the data. Multi-threaded. Cheers for everything. Gerry.
-
It's a C++ object. I want to learn more about Calling another Exe from my main exe, then letting it carry out some task, & then returning. I would passbyref the object when the second process kicked off. Not sure to create a new process or spawn another Thread & sync the data. Multi-threaded. Cheers for everything. Gerry.
OK, that's clearer. If you call into another process from another process, you have two address spaces. What this means in reality is this. You have a pointer to an area of memory (a C++ object for example). This pointer has a physical address, say 0x1000000 which is meaningful *only* in the address space it is created in. So if you have a separate process to which you pass this C++ object pointer to and try to de-reference it, then it will not point to the object you want. This is why you need the shared-memory approach. Of course, you have to come up with a way of calling a function in another process first using some form of IPC. (Into the realms of COM here). In fact, that's what COM out-of-process servers are designed for. NOTE: You can expose the C++ object as shared memory between two processes, but you still have to thread synch because two threads are (potentially) using the same memory concurrently. If you want to create a separate thread in the same process then pass a C++ object by reference (pointer) then that's the simpler solution. You still have to synch access on the data (the C++ object's methods and attributes) though. My advice, do the in-process (two threads in same address space) approach first, with thread synchronisation, then migrate the code to calling cross-process later.
-
OK, that's clearer. If you call into another process from another process, you have two address spaces. What this means in reality is this. You have a pointer to an area of memory (a C++ object for example). This pointer has a physical address, say 0x1000000 which is meaningful *only* in the address space it is created in. So if you have a separate process to which you pass this C++ object pointer to and try to de-reference it, then it will not point to the object you want. This is why you need the shared-memory approach. Of course, you have to come up with a way of calling a function in another process first using some form of IPC. (Into the realms of COM here). In fact, that's what COM out-of-process servers are designed for. NOTE: You can expose the C++ object as shared memory between two processes, but you still have to thread synch because two threads are (potentially) using the same memory concurrently. If you want to create a separate thread in the same process then pass a C++ object by reference (pointer) then that's the simpler solution. You still have to synch access on the data (the C++ object's methods and attributes) though. My advice, do the in-process (two threads in same address space) approach first, with thread synchronisation, then migrate the code to calling cross-process later.