I m trying to send some userdefined messege to previous application, if its running.. if i try that to call from same app then its work properly otherwice not.. please help me regarding it as soon as possible.. my code is like that...
bas file..
-----------
Option Explicit
Public OldProc As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public Const MmM_MSG = 101
Public Sub Main()
If App.PrevInstance Then
Dim AppName As String, hWindow As Long
AppName = App.Title
App.Title = "Nothing"
hWindow = FindWindow("ThunderRTMain", AppName)
If hWindow = 0 Then
hWindow = FindWindow("ThunderRT5Main", AppName)
End If
If hWindow = 0 Then
hWindow = FindWindow("ThunderRT6Main", AppName)
End If
If hWindow = 0 Then
hWindow = FindWindow(vbNullString, AppName)
End If
If hWindow <> 0 Then
' MsgBox hWindow
PostMessage hWindow, MmM_MSG, vbNull, vbNull
End If
End
Else
Form1.Show
End If
End Sub
Public Function WndProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case wMsg
Case MmM_MSG
MsgBox "MmM_MSG"
WndProc = 1
Case Else
WndProc = CallWindowProc(OldProc, hwnd, wMsg, wParam, lParam)
End Select
End Function
.frm file
---------
Option Explicit
Private Sub Command1_Click()
PostMessage Me.hwnd, MmM_MSG, 0, 0
End Sub
Private Sub Form_Load()
Label1.Caption = Me.hwnd
OldProc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WndProc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
If OldProc Then
SetWindowLong Me.hwnd, GWL_WNDPROC, OldProc
OldProc = 0
End If
End Sub
If i press command button then its work fine.. but if i start another instence of this program, then its not work.. i put a label on form and print the current hWnd of the form there.. and put a MsgBox over Main method and print exist windows handle, and i got different values... but if i put WM_CLOSE message inside of MmM_MSG, then its work perfectely !! i also including that test project as attechment, please check it and let me know the proper way to handle this...
You have to make sure that 101 is not already a message in use. Use RegisterWindowMessage API function instead. This is register a unique message globally.
I got the solution i just change folloing in form and its work
Private Sub Form_Load()
OldProc = SetWindowLong(FindWindow(vbNullString, AppName), GWL_WNDPROC, AddressOf WndProc)
End Sub