SendDatavbApp




Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long

Private Const WM_SETTEXT = &HC

Private Sub Text1_Change()
MsgBox "Change detected"
End Sub

'********* MESSAGE SENDING PROGRAM *****

'This program will send text messages to another vb program.

'The messages will be placed directly into the text boxes.

'Add 1 wide command button (Command1) to a blank form, double

'click on the form, then copy and pastethe following source code.

'(This will be a separate project called message sender)


Option Explicit

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long

Private Const WM_SETTEXT = &HC
'This program will send test messages to another vb program.

'The recipient must be running when the command button is pressed.


Private Sub Command1_Click()
Dim sAppName As String, sSection As String
' Here we must supply the name of the program which is to receive messages.

sAppName = "Receiving AppName"

If Not InterProcMsg(sAppName, "Text1", "Message To Text1") Then
' Notify if the message could not be sent.

MsgBox "Could Not send message sent To Text1"
End If

If Not InterProcMsg(sAppName, "Text2", "Message To Text2") Then
' Notify if the message could not be sent.

MsgBox "Could Not send message sent To Text2"
End If

If Not InterProcMsg(sAppName, "Text3", "Message To Text3") Then
' Notify if the message could not be sent.

MsgBox "Could Not send message sent To Text3"
End If
End Sub

Function InterProcMsg(sAppName As String, sKey As String, _
sValue As String) As Boolean
On Error Goto Err_InterProcMsg
' This routine will place a text message(sValue) into a control on a form

' running on another program.In order for this to work the recipient program

' must be running, and must have stored the required windows handles into the

' windows registry.


Dim sSection As String, lRequiredHandle As Long, SentOK As Boolean
sSection = "InterProcess Handles"
' First we obtain the required handle from the registry.

lRequiredHandle = GetSetting(sAppName, sSection, sKey)

' If a valid handle was found the send the message passed

' in the string 'sValue'

If lRequiredHandle = 0 Then
SentOK = False ' Message Not sent (handle not found)
Else
Call SendMessage(lRequiredHandle, WM_SETTEXT, ByVal 0&, ByVal sValue)
SentOK = True' Message sent
End If
Exit_InterProcMsg:
' Exit the function with InterProcMsg set to either

' TRUE if message sent to the other program without problems, or

' FALSE if the message could not be sent.


InterProcMsg = SentOK
Exit Function
Err_InterProcMsg:
' Error handler to catch and process anyunexpected errors.

MsgBox "Error" & Str$(Err) & _
" In routine InterProcMsg on sending form: " & Error$(Err)
SentOK = False ' Message Not sent (due to unexpected error)
Goto Exit_InterProcMsg
End Function

Private Sub Form_Load()
' Add a prompt to the command button.

Command1.Caption = "Send Messages To the other program"
End Sub

'********* MESSAGE RECEIVING PROGRAM ***

' This program will receive text messages from another vb program.

' The messages will be placed directly into the text boxes.

' Add 3 text boxes (text1, text2 and text3) to a blank form, double

' click on the form, then copy and paste the following source code.

' (This will be a separate project called message receiver)


Option Explicit

Private Sub Form_Load()
' To allow the sending program to write to our textboxes, we make a

' temporary saving of windows handles of the textboxes to the registry.

Dim sAppName As String
' Here we must supply the name of this program

' (the name must match that given in thesending program).


sAppName = "Receiving AppName"
' Now we store the windows handles for the forms textboxes.

SaveSetting sAppName, "InterProcess Handles", "Text1", Str$(Text1.hWnd)
SaveSetting sAppName, "InterProcess Handles", "Text2", Str$(Text2.hWnd)
SaveSetting sAppName, "InterProcess Handles", "Text3", Str$(Text3.hWnd)
End Sub

Private Sub Form_Unload(Cancel As Integer)
' The program has now finished, so we can now remove

' our InterProcess handle values from the registry.

DeleteSetting "Receiving AppName", "InterProcess Handles"
End Sub
Inputs:
In your own program substitue "Receiving AppName"
with the name of yourprogram (on both sending and
receiving ends). For every control that is to have
data passed to it just create an entry as per the
textbox

examples;
SaveSetting sAppName, "InterProcess Handles", "Text1", Str$(Text1.hWnd)

Returns:
When text data is sent to the receiving program it
is placed straight in to the control and are ready
for use.

Assumes:
The sample forms provided show text being passed to
3 textboxes, but you could easily modify to pass to
combo boxes etc. The solution involves teporary
storage in the windows registry of the necessary
windows handles. If you want the recipient program
To act upon the received text you could simply add a
change procedure such as;











( senddatavbapp.html )- by Paolo Puglisi - Modifica del 17/12/2023