Solved

Excution of External program

Posted on 1998-09-09
12
265 Views
Last Modified: 2011-09-20
I'm studying Excution of external program. I want a sample code which execute another. The main (Host) application is C++ window program and sub (client) program is based on C or C++. NOT DLL! Processing can be a simple code like 'C=A+B'. Please let me know how process it is. I want to sample! (I know it may bother you if making a sample. ^^, )
0
Comment
Question by:free096
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
12 Comments
 
LVL 7

Expert Comment

by:faster
ID: 1172406
Just use CreateProcess()
0
 

Author Comment

by:free096
ID: 1172407
I said I needed sample which can run. I can not do it though I know CreateProcess() function.
0
 
LVL 7

Expert Comment

by:faster
ID: 1172408
Sorry, you need a sample.

STARTUPINFO StartupInfo;
PROCESS_INFORMATION ProcessInformation;
char pathbuf[_MAX_PATH];

GetStartupInfo(&StartupInfo);
strcat(pathbuf, "\\path\\a.exe");
CreateProcess(NULL, pszHomeDir, NULL, NULL, FALSE, CREATE_NEW_PROCESS_GROUP | NORMAL_PRIORITY_CLASS, NULL, NULL, &StartupInfo, &ProcessInformation);

I assume that you are using windows, besides CreateProcess, you can also use WinExec().  Another alternative is to use system(), which is a C runtime function so it is also available on other platforms.  However, on windows, CreateProcess() is better.

On UNIX, you can use fork() and execle() (or execlp, execvp ...).  Well, you mentioned Windows, so forget about this.

 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:faster
ID: 1172409
Let me know if you have problem using it.
0
 

Author Comment

by:free096
ID: 1172410
Thanks, faster. But still I don't know How app. can exchange data with Client app. Yea, OS is windows, of course. Would you mind showing me both sample of Client & Host which can exchange data like 'C = A + B'. (input A & B into Host, Client calculate it, and  Host must show the result, C)
0
 
LVL 7

Expert Comment

by:faster
ID: 1172411
I see, you are actually talking about IPC.  There are a lot of ways to do it depending on what kind of messages you need to exchange between the parent and the child.  So could you let me know the type and size of the messages (both direction) and do they exchange information frequently or simply a one time process?
0
 

Author Comment

by:free096
ID: 1172412
Thanks, faster, again. Actually I have not heard about IPC. So I have no idea. Are there so many kinds of IPC? Hm... ^^, First of all I want to know just SIMPLE calculation, C=A+B. (It's the starting point. ^^;) I think it's one time process: Host send A&B to Client. and Client send the result, C(=A+B) to Host. And interger type is enough. Can you show me the samples of both Host and Client?
0
 
LVL 7

Accepted Solution

by:
faster earned 450 total points
ID: 1172413
This is just a simple demo, you need to refine your error checking etc.

Parent:

#include <windows.h>
#include <stdio.h>


void main()
{
       STARTUPINFO StartupInfo;
     PROCESS_INFORMATION ProcessInformation;
     char pathbuf[_MAX_PATH];
       BOOL bSuccess;
       DWORD r;

     GetStartupInfo(&StartupInfo);
     sprintf(pathbuf, "d:\\faster\\tt\\t2.exe %d %d", 10, 20);
     CreateProcess(NULL, pathbuf, NULL, NULL, FALSE,CREATE_NEW_PROCESS_GROUP | NORMAL_PRIORITY_CLASS, NULL, NULL, &StartupInfo, &ProcessInformation);
       bSuccess = GetExitCodeProcess(ProcessInformation.hProcess, &r);
}


Child:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
      int i,j;

      i = atoi(argv[1]);
      j = atoi(argv[2]);
      return i+j;
}

0
 
LVL 7

Expert Comment

by:faster
ID: 1172414
Note that the server->child communication is by command line, other simple choices include env variable.  But they can only be one time.  Better and more complex way include using a pipe, using windows message (create a window in child), socket, OLE etc.  Child->parent is done using an exit status, this can only work for integer.  A better and still easy way is to use windows message, I mean to create a window in parent with a fixed window name, then the child can find the window handle and then use WM_COPYDATA or other messages to pass data back.  Pipe, socket, OLE etc give you more control.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1172415
What does the ** in front of the argv means?? in
  int main(int argc, char **argv)

Thanks!

Regards,
Viktor Ivanov
0
 
LVL 7

Expert Comment

by:faster
ID: 1172416
** means a pointer to a list of pointers, that is the standard C stuff
0
 

Author Comment

by:free096
ID: 1172417
Thanks, faster. Your code has some error to debug. Anyway, it looks pretty good. I'll try it. After this, if I had more question, I would ask you. Could I know your Email address? (If question, I'll notify you.)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 149
Header of docx file 17 145
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 60
Outlook 13 100
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question