CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2007
    Posts
    258

    Passing value from one exe to another exe

    Hi all,

    I am having two exe's, I am running one exe through another using API ShellExecute.... Now what i want is i want to pass a value from one exe to another and want to use that value in second exe. How is this possible???

    Thanks in advance

  2. #2
    Join Date
    Jun 2005
    Posts
    1,255

    Smile Re: Passing value from one exe to another exe

    For instance, using the clipboard.

  3. #3
    Join Date
    Feb 2007
    Posts
    258

    Re: Passing value from one exe to another exe

    can u tell me how

  4. #4
    Join Date
    Jun 2005
    Posts
    1,255

    Smile Re: Passing value from one exe to another exe

    Okay, but I don't pretend my solution is the best one.
    I use old C programming stuff. Please don't laugh at me.
    Code:
    static LPSTR to_clip_mem = NULL;
    int put_txt_in_clipboard(HWND hwnd, char *to_clip)
    {
       HANDLE hto_clip_mem;
    
       if (to_clip_mem != NULL)
          GlobalFree(to_clip_mem);
    
       to_clip_mem = (char *) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, to_clip_lg + 1);
       if (to_clip_mem == NULL)
          return FALSE;
    
       hto_clip_mem = GlobalLock(to_clip_mem);
       if (!(hto_clip_mem)) {
          GlobalFree(to_clip_mem);
          return FALSE;
       }
    
       lstrcpy((char *)hto_clip_mem, to_clip);
    
       GlobalUnlock(to_clip_mem);
    
       if (OpenClipboard(hwnd) == 0)
          return FALSE;
       if (EmptyClipboard() == 0) {
          CloseClipboard();
          return FALSE;
       }
       if (SetClipboardData(CF_TEXT, to_clip_mem) == NULL) {
          CloseClipboard();
          return FALSE;
       }
       if (CloseClipboard() == 0)
          return FALSE;
    
       return TRUE;
    }
    
    int get_txt_from_clipboard(HWND hwnd, char *from_clip, int from_clip_max)
    {
       HANDLE hclip; LPSTR clip_mem; int clip_mem_lg;
       int from_clip_lg;
    
       if (IsClipboardFormatAvailable(CF_TEXT) == 0)
          return FALSE;
       if (OpenClipboard(hwnd) == 0)
          return FALSE;
       hclip = GetClipboardData(CF_TEXT);
       if (hclip == 0) {
          CloseClipboard();
          return FALSE;
       }
       clip_mem_lg = GlobalSize(hclip);
       if (clip_mem_lg <= 0) {
          CloseClipboard();
          return FALSE;
       }
       clip_mem = (char *)GlobalLock(hclip);
       if (clip_mem == NULL) {
          CloseClipboard();
          return FALSE;
       }
       from_clip_lg = clip_mem_lg;
       if (from_clip_lg >= from_clip_max) from_clip_lg = from_clip_max - 1;
       for (i = 0; i < from_clip_lg; i++)
          from_clip[i] = clip_mem[i];
       from_clip[i] = '\0';
       GlobalUnlock(hclip);
       if (CloseClipboard() == 0)
          return FALSE;
    
       return TRUE;
    }

  5. #5
    Join Date
    Jun 2008
    Posts
    592

    Re: Passing value from one exe to another exe

    well there is a simpler way. It deals with sendmessage.. that is if both of your exes run through the message queue. You use RegisterWindowMessage to register a message in both apps and process the message in each app's message queue.....
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  6. #6
    Join Date
    May 2002
    Posts
    1,435

    Re: Passing value from one exe to another exe

    You could also pass the value on the command line by means of ShellExecute's lpParameters string. The called program would then use ::GetCommandLine() to retrieve the value.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Passing value from one exe to another exe

    Other possibilities: Interprocess Communications
    Victor Nijegorodov

  8. #8
    Join Date
    Jul 2001
    Location
    Netherlands
    Posts
    751

    Re: Passing value from one exe to another exe

    sockets,named pipes, windows events + (memorymapped) files

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Passing value from one exe to another exe

    Quote Originally Posted by VCProgrammer
    I am having two exe's, I am running one exe through another using API ShellExecute.... Now what i want is i want to pass a value from one exe to another and want to use that value in second exe. How is this possible???

    Thanks in advance
    What type of value is it? If it's a simple int or short string you can pass it as an argument in the command line when you fire the second process. If you to pass something else (like a structure or a large string), check out the Interprocess Communication link provided earlier.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured