Results 1 to 1 of 1

Thread: VB - Send a string between VB-created apps without using subclassing

Threaded View

  1. #1

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Arrow VB - Send a string between VB-created apps without using subclassing

    What this example lets you do is send any string you like, instantaneously, to another app you have made in Visual Basic. Because this example doesn't use subclassing, it is perfectly stable in the IDE and doesn't require VB 6. All you need is a couple of API declarations.

    Before we start, let me emphasise that this is NOT a code-only solution; it is sort of a workaround or 'hack' requiring a textbox. If you are after a code-only solution (well, requires a form only), then have a look at this thread where moeur posted an example using a COPYDATASTRUCT and subclassing to receive the message.


    Brief description of how it works

    This example relies on the fact that sending a WM_SETTEXT command to a textbox triggers its Change() event. This way, the receiving application is instantly notified when a string message is received.

    I used the API calls FindWindow and FindWindowEx in order to retrieve the window handle (hWnd) of the text box on the destination window. Note that this does require that the destination window only has one textbox present (otherwise it can get confusing) and that the sending application knows the destination window's caption.

    Once the text box hWnd is known, it is a matter of simply using the SendMessage API call to send a WM_SETTEXT (&HC) command to the text box, passing zero (0&) in wParam, and a pointer to the string contents in lParam using ByVal CStr([string]).

    As I said at the beginning of the post this is a bit of a hack method, but it is very simple to implement and works well. There is only one caveat, which is noticeable in the example I attached, and that is the fact that SendMessage waits for the message to be received before it returns execution to the sending app. This in itself isn't a bad thing at all (in fact it's the only way to do it), but it does have the effect of 'hanging' the sending application until the receiving app has finished processing the message. In my example, the sending app is hung until you dismiss the message box showing the received string.


    Using the example attached

    Just unzip the file to a directory, using a program such as WinZip or 7zip, and then run both the StringSend.vbp and StringReceive.vbp projects simultaneously. If you like you can compile them first, to get the IDE out of the way so it is easier to see the effect of sending the string messages. But make sure you read the next point.


    Compiled vs. in the IDE

    When your application is running in the VB IDE, the text box class name is just "ThunderTextBox". But when you compile it, it will have a different name depending on your version of VB. For VB6 it is "ThunderRT6TextBox" and for VB5 it is "ThunderRT5TextBox". Make sure you change the class name appropriately in the code before you compile the application.


    Have fun!
    Attached Files Attached Files
    Last edited by penagate; Jun 10th, 2005 at 11:24 AM. Reason: Correction: Added "a pointer to" (the string contents).

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