Skip to content

Instantly share code, notes, and snippets.

@pwlin
Created April 18, 2010 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwlin/370193 to your computer and use it in GitHub Desktop.
Save pwlin/370193 to your computer and use it in GitHub Desktop.
2 exe communicating vb6
project MainApp:
on the form place controls as follows:
place a frame on form, set the caption to frame1 = Communicate to Receiver
insert in frame1 a Text box, leave default name(Text1), set index to 0
place another frame on form, set the caption to frame2 = Send This Message
insert in frame2 a Text Box, Change default name to Text1, set index to 1
insert a Label under Text Box, leave default name(Label1), set Alignment to center
place a Command button on form, leave default name, set command1 caption = Send Message
'MainApp Form1 code ########################################
Option Explicit
DefLng A-Z
Private Sub Command1_Click()
On Error GoTo EHmess
If SendMessageToAppReceiver(Me.Text1(0).Text, Me.Text1(1).Text) Then
Me.Label1.Caption = "Message has been sent."
Else
Me.Label1.Caption = "Error sending Message."
End If
Exit Sub
EHmess:
MsgBox Err.Description, vbExclamation, "Error: &H" & Hex(Err.Number)
End Sub
Private Sub Form_Load()
Me.Text1(0).Text = GUIDappreceiver
Me.Label1.Caption = "Please enter Message to send!"
End Sub
Private Sub Text1_GotFocus(Index As Integer)
With Me.Text1(Index)
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
' End MainApp Form1 code ####################################
add a Module to project, name the Module = modSendMessage
' modSendMessage code #####################################
Option Explicit
DefLng A-Z
Private Declare Function FindWindowEx _
Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Private Declare Function SendMessageTimeout _
Lib "user32" Alias "SendMessageTimeoutA" (ByVal hwnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
lParam As Any, _
ByVal fuFlags As Long, _
ByVal uTimeout As Long, _
lpdwResult As Long) As Long
Public Const GUIDappreceiver = "{7DE2A166-C107-4FC6-9A09-67672C3D6AAB}"
Private Const WM_SETTEXT = &HC
Private Function TBHandle(sWindowTitle As String) As Long
Dim lWndHandle As Long
'Make sure we are working with a VB Form hWnd
'use WinClass
'This only works on VB RunTime 6 Forms "ThunderFormRT6DC"
' look for form
lWndHandle = FindWindowEx(0, 0, "ThunderRT6FormDC", sWindowTitle)
If lWndHandle Then
'This only works on VB RunTime 6 Forms "ThunderRT6TextBox"
' once found, look for its TextBox
TBHandle = FindWindowEx(lWndHandle, 0, "ThunderRT6TextBox", vbNullString)
Else
'This only works on VB RunTime Form "ThunderFormDC"
' if form not found, look for interpreted form
lWndHandle = FindWindowEx(0, 0, "ThunderFormDC", sWindowTitle)
If lWndHandle Then
'This only works on VB RunTime Form "ThunderTextBox"
' if found, search for its child TextBox
TBHandle = FindWindowEx(lWndHandle, 0, "ThunderTextBox", vbNullString)
End If
End If
' raise error if its not found
If lWndHandle = 0 Then
Err.Raise vbObjectError + 1, "modSendMessage:TBHandle", sWindowTitle & " App Receiver not found."
ElseIf TBHandle = 0 Then
Err.Raise vbObjectError + 2, "modSendMessage:TBHandle", sWindowTitle & " App Receiver Text1 not found."
End If
End Function
Public Function SendMessageToAppReceiver(sWindowTitle As String, sText As String) As Boolean
Dim lhWndTextBox As Long
Dim lRtn As Long
lhWndTextBox = TBHandle(sWindowTitle)
If lhWndTextBox > 0 Then
If SendMessageTimeout(lhWndTextBox, WM_SETTEXT, 0, ByVal sText, 0, 1000, lRtn) Then
If lRtn <> 0 Then
SendMessageToAppReceiver = True
End If
End If
End If
End Function
' End modSendMessage code ##################################
run MainApp, then build MainApp.exe
create a new seperate exe project, name it AppReceiver
on the form place controls as follows:
place a Label on form, Remove the caption to Label1
insert in a Text box under Label1, leave default name(Text1)
place a Timer on the form, leave default(Timer1)
set the name to Form1 = AppReceiver
set the caption to Form1 = {7DE2A166-C107-4FC6-9A09-67672C3D6AAB}
' AppReceiver Form1 code ###################################
Option Explicit
DefLng A-Z
Private Sub MessageReceived(sText As String)
Select Case sText
Case "abc"
Case "xyz"
End Select
End Sub
Private Sub Form_Load()
Me.Label1.Caption = "Listening for Messages..."
End Sub
Private Sub Timer1_Timer()
Me.Timer1.Interval = 0
Me.Text1.Text = ""
End Sub
Private Sub Text1_Change()
Me.Timer1.Interval = 10000
MessageReceived Me.Text1.Text
Me.Label1.Caption = "Message Received"
If Me.Text1.Text = "" Then
Me.Label1.Caption = "Listening for Messages..."
End If
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Then
Unload frmReceiver
End If
End Sub
' End AppReceiver Form1 code ################################
add a Module to project, name the Module = modAppReceiver
' modAppReceiver code ####################################
Option Explicit
DefLng A-Z
Public Sub Main()
If App.PrevInstance Then Exit Sub
Load frmReceiver
frmReceiver.Show
End Sub
' End modAppReceiver code #################################
right click AppReceiver Project, select AppReceiver Properties,
select Startup Object = Sub Main
run AppReceiver, then build AppReceiver.exe
How to use:
start AppMain.exe & AppReceiver.exe
type a message in AppMain window "Send This Message" Text Box
then press Send Message Button, this will send the message to and display in
AppReceiver Window.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment