Code Snippets

  

C Source Code


Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,372 Programmers for FREE! Ask your question and get quick answers from experts. There are 962 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Pass Return Value

Pass the return value from one exe to another program.

Submitted By: no2pencil
Actions:
Rating:
Views: 1,200

Language: C

Last Modified: May 22, 2007
Instructions: Compile both sets of code, & have them in the same directory.

Snippet


  1. // Program 1.
  2. // This guy will do the passing...
  3. //
  4. #include <stdio.h>
  5. #include <windows.h>
  6.  
  7. int main(void) {
  8.      int i=0,lc=0; // Line count
  9.      char str[132];
  10.      char ch='a';
  11.      FILE *fp;
  12.      
  13.      fp=fopen("C:\\test.txt","r+");
  14.      if(!fp)exit(1);     
  15.      else {
  16.           while(ch!=EOF) {
  17.                ch=fgetc(fp);
  18.                if(ch=='\n'){
  19.                     i=0;
  20.                     lc++;
  21.                }
  22.                if(ch!='\n'){
  23.                     str[i]=ch;
  24.                     if(i==0){
  25.                          //if(str[0]=='#')lc--;
  26.                          if(str[0]!='1')lc--;
  27.                     }
  28.                     i++;
  29.                }
  30.           }
  31.      }
  32.      lc--;
  33.      fclose(fp);
  34.      return(lc); // This value will be passed to the 2nd program.
  35. }
  36.  
  37. // Program #2
  38. // This guy recieves the value from the return() of program #1.
  39. //
  40. #include <stdio.h>
  41. #include <windows.h>
  42.  
  43. void main(void) {
  44.      DWORD     exit_status;
  45.      char line_count[14]="LineCount.exe";
  46.  
  47.   SECURITY_ATTRIBUTES      sattr;
  48.   STARTUPINFO           sinfo;
  49.   PROCESS_INFORMATION      pinfo;
  50.  
  51.   ZeroMemory(&sinfo,sizeof(sinfo));
  52.  sattr.nLength=sizeof(SECURITY_ATTRIBUTES);
  53.  sattr.lpSecurityDescriptor=NULL;
  54.  sattr.bInheritHandle=FALSE;
  55.  
  56.  if(!CreateProcess(NULL,line_count,NULL,NULL,FALSE,0,NULL,NULL,&sinfo,&pinfo)) {
  57.       printf("ERROR: Cannot launch child process\n");
  58.     exit(1);
  59.  }
  60.  WaitForSingleObject(&pinfo.hProcess,INFINITE);
  61.  TerminateProcess(&pinfo,0);
  62.  GetExitCodeProcess(pinfo.hProcess,&exit_status);
  63.       while(exit_status==STILL_ACTIVE){
  64.             if(GetExitCodeProcess(pinfo.hProcess,&exit_status)) {
  65.                if(exit_status!=STILL_ACTIVE)printf("%d",exit_status);
  66.             }
  67.             else printf("GetExitCodeProcess() failed\n");
  68.     }
  69. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month