Passing Variables ProgramA to ProgramB
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Passing Variables ProgramA to ProgramB

  1. #1
    Join Date
    Jan 2004
    Location
    TX, USA
    Posts
    347

    Passing Variables ProgramA to ProgramB

    I am using Shell to pass command line variable from program A to program B. In program B I am pasteing information I want to come back to program A via the CLIPBOARD.

    Is there a memory location or something else I can get a short string back from program B to program A without using the clipboard? I don't want to delete someones clipboard if they are using it at the time.

    Code:
    Program A
    
    Private Sub dbgrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
       If Button = 2 And DBGrid1.Col = 4 Then
          ckNull = IsNull(DBGrid1.Text)
          tmp = DBGrid1.Columns(4).Text
          If (ckNull = False) And tmp <> "" And tmp <> " ." Then
             DBGrid1.Col = 3
             CallWord = Shell(App.Path + "\texascnty.exe " + tmp, 1)
    
                     'In Program B :  Clipboard.settext county&radius
    
             msg = "Paste County and Radius from last" _
             & vbCrLf & "map session into County textbox?"
             Response = MsgBox(msg, 4, "Paste Clipboard")
             If Response = 6 Then
                tbCTY.Text = Clipboard.GetText
                If Clipboard.GetText <> "" Then NewSearch_Click
             End If
          End If
       End If
    End Sub
    Thanks
    Cathy
    Jan 2004 - NEWBIE to VB6
    Any and all help appreciated

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Passing Variables ProgramA to ProgramB

    Three ways immediately come to mind:

    1) Place a hidden textbox on the form, and place your string into it. Then obtain the handle to the textbox from the other program, and retrieve the text. I suppose you could use any control that has text obtainable via API, so you could use a control that is unique to the form, thereby easy to identify. Alternatively, set the window style to something unique, or include some special characters in the string as an identifier.

    2) Write the information to a temp file.

    3) Copy the clipboard contents into a buffer, use the clipboard for your data, then put the original contents back.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  3. #3
    Join Date
    Jan 2004
    Location
    TX, USA
    Posts
    347

    Re: Passing Variables ProgramA to ProgramB

    1) - Don't know how to get handles. Would appreciate an example.
    2) - I'll probably do this.
    3) - I know how to transfer text type clipboard data but don't know what to do if a picture file is in it.

    Thank you Wizbang
    Cathy
    Jan 2004 - NEWBIE to VB6
    Any and all help appreciated

  4. #4
    Join Date
    Jan 2004
    Location
    TX, USA
    Posts
    347

    Re: Passing Variables ProgramA to ProgramB

    Wizbang,
    I'm not a programmer but I see how files can build up in programs. I'm using your number 2 suggestion. Seems to work just fine but seems a little wasteful.
    I am interested in the 'handle' method if it is not difficult.
    thanks again
    Cathy
    Jan 2004 - NEWBIE to VB6
    Any and all help appreciated

  5. #5
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Passing Variables ProgramA to ProgramB

    Actually, now that I think about it a bit more, there is an easier way to do this. All you need to do, is pass the handle of the textbox to the second program. As long as the first program's window remain open, the handle of the textbox will not change. You can also pass the other information along with the handle, and use a delimiter such as "|" or some other character that will otherwise never be there. Alternatively, you can use the textbox to hold the data. Then the second program needs only the handle of the textbox to retrieve the information, and perform it's tasks. Once it has finished, it uses the handle to set the text of the box with the data which the first program is waiting for.

    Now for the really cool part. Once the second program changes the text, the Change event of the textbox will fire, thus your program will know that the second program has completed, and can do whatever it needs to do with the data in the box.

    For this to work, we only need to use the SetWindowText API. This works with a standard vb textbox, and the box does not have to be visible. If you pass only the handle of the box, then the second program can get the text with the GetWindowText API.

    Hope I didn't lose you in all this, but it really is simple. Let me know if you need more.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Passing Variables ProgramA to ProgramB

    Last edited by cjard; February 25th, 2005 at 06:40 AM.
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  7. #7
    Join Date
    Jan 2004
    Location
    TX, USA
    Posts
    347

    Re: Passing Variables ProgramA to ProgramB

    Wizbang, might you have a simple example?
    CJard, those two sites look like a college course. I'd rather just try and get some quick code and go to college later.
    Thanks
    Cathy
    Jan 2004 - NEWBIE to VB6
    Any and all help appreciated

  8. #8
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Passing Variables ProgramA to ProgramB

    OK. Here is an example of how it can be done. When you click the button, it will launch a second copy of itself, passing the hWnd of the textbox only. The second app instance then retrieves the text of the box from the first app's window, reverses it, and sends it back. Be sure to compile it first, so that the exe file exists.

    Hope you can make use of this!
    Attached Files Attached Files
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  9. #9
    Join Date
    Jan 2004
    Location
    TX, USA
    Posts
    347

    Re: Passing Variables ProgramA to ProgramB

    Well it seems to work but is a little confusing because of opening the same executable. I'll play with it but I think this is exactly a good way to transfer data.
    Thanks alot Wizbang.
    Cathy
    Jan 2004 - NEWBIE to VB6
    Any and all help appreciated

  10. #10
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Passing Variables ProgramA to ProgramB

    Quote Originally Posted by nbCathy
    Well it seems to work but is a little confusing because of opening the same executable. I'll play with it but I think this is exactly a good way to transfer data.
    Thanks alot Wizbang.
    Both sending and receiving the data are just included in the same example. Simply change the program name being called by ShellExecute, and put the code (and related functions) from the load event into the program you are calling. Incidently, you can click the button of the second window that opens, and it will launch another instance, reversing the text for that window, since the handle being passed is for that textbox. If you click the same button again after the text has been reversed, the new window that opens will reverse it back again.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  11. #11
    Join Date
    Jan 2004
    Location
    TX, USA
    Posts
    347

    Re: Passing Variables ProgramA to ProgramB

    Okay, I got it figured out now. I put a second button on it. Button1 sends and Button2 returns. Works just great. Only one button is visible at a time but same compiled program. Thanks alot for this. Exactly what I want.

    But, what happens when #1 is closed and button2 is clicked? It works but what is it doing?

    Also, what are the constants for?
    Private Const WM_SETTEXT = &HC
    Private Const WM_GETTEXT = &HD
    Private Const WM_GETTEXTLENGTH = &HE

    Thanks - Wizbang 1000 points!!!
    Cathy
    Jan 2004 - NEWBIE to VB6
    Any and all help appreciated

  12. #12
    Join Date
    Jan 2004
    Location
    TX, USA
    Posts
    347

    Re: Passing Variables ProgramA to ProgramB

    P.S.
    If the #1 (the send) is closed, it won't affect other open programs, will it?
    Cathy
    Jan 2004 - NEWBIE to VB6
    Any and all help appreciated

  13. #13
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Passing Variables ProgramA to ProgramB

    Quote Originally Posted by nbCathy
    P.S.
    If the #1 (the send) is closed, it won't affect other open programs, will it?
    Unlikely. This is one reason for using IsWindow. In addition to that, you could also check the window class to make sure it's a vb textbox. You could also use GetParent to make sure the window it's on is your program, in a variety of ways, such as checking for a known Caption, obtaining the name and location of the exe, etc. It just depends on how many checks and fail-safes you want to put into it.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  14. #14
    Join Date
    Jun 2005
    Posts
    1

    Re: Passing Variables ProgramA to ProgramB

    I am new to all of this, so I am still confused. Can someone show me the two forms(different applications) that would talk back and forth. One form would have a send and the other form would have a recieve.

  15. #15
    Join Date
    Mar 2005
    Posts
    226

    Re: Passing Variables ProgramA to ProgramB

    I would write a DLL and pass data beack and forth through the DLLs global memory. Although the code in a dll is unique the to it's instnace, the global memory is not.

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)