Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WIN32 Messages Handle Doubt

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,

I am using WIN32 Messages in order to be able to communicate with a Visual C++ application, actually I am able to capture the messages using the API getmessage(..), but I am using a timer in order to post a message ( because I don't know exactly when I am going to get a message) and get the desire response, this action is making my application to work very slow because it is using a lot CPU resources, Could anyone advise me another way to get messages from windows without using timers

I would really appreciate your help

Thanks
 
Hi,

You could do this with subclassing. If you have the hWnd of the window you want to check the messages from, you can either hook with the SetWindowsHookEx function to it or subclass it with the SetWindowLong function.

[tt]
'place this in a form
Private Sub Form_Load()
Call Subclass(me.hwnd)
End Sub

Private Sub Form_Unload(Cancel as Integer]
Call Unsubclass
End Sub

'place this in a module
Public 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
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Const GWL_WNDPROC = -4

Private lpPrevProc As Long
Private m_hWnd As Long

Public Sub Subclass(hWnd as Long)
m_hWnd = hWnd

lpPrevProc = SetWindowLong(m_hWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub

Public Sub UnSubClass()
Call SetWindowLong(m_hWnd, GWL_WNDPROC, lpPrevProc)
End Sub

Private Function WindowProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'handle the messages you want to capture here
WindowProc = CallWindowProc(lpPrevProc, hWnd, wMsg, wParam, lParam)
End Function

[/tt]

LuCkY
 
As LuckyLuke said, you can do this with subclassing by hooking into a process' message queue using the serWindowsHookEx. This can be done in VB if you are monitoring your own local process, but if you want to monitor other process or if you want to monitor all process, the callback MUST reside in a C++ DLL since DLL code is loaded into common memory shared (actually its physical memory mapped into each virtual process space) it can monitor all Processes. - Jeff Marler B-)
 
Hi,
Thanks for your fast response

I still have some doubts

I already get the desire messages that I need, I am using the next Sub into a Timer which interval parameter is set to 1 millisecond.

Private Sub QueryStatus()
Dim Interrupt As Long

If FindWindow(vbNullString, "CNCSim") = 0 Then
lblStatus.Caption = "Error Posting Message"
Else
'I already know the range of messages
'MinMsg and MaxMsg

dummy = PostMessage(x, nQueryMsg, 0, y)

If GetMessage(xMsg, Me.hWnd, MinMsg, MaxMsg) Then
lblMsgID.Caption = xMsg.message
lblLParam.Caption = xMsg.lParam
lblWParam.Caption = xMsg.wParam

Select Case xMsg.message
Case nStatusMsg
Select Case xMsg.wParam
Case 0
lblStatus.Caption = "No Program Loaded"
Case 1
dummy = GlobalGetAtomName(xMsg.lParam, aString, 256 )
lblStatus.Caption = "Program Loaded"
Case 2
dummy = GlobalGetAtomName(xMsg.lParam, aString, 256)
lblstatus.caption="Program _ running"
Case 3
dummy = GlobalGetAtomName(xMsg.lParam, aString, 256)
lblStatus.Caption = "Program Stopped

End Select
End If

End If

End Sub

As I mentioned before this timer is using a lot of CPU resources, but I dont know when I am going to get these messages that's why I use this sub (Query) into the timer.

Does the WindowCall function or the SetWindowsHookeX help me to avoid the use of the timer, how can I implementate them?, I think they can be useful but if I have to use a timer once again to call these functions I will still have the same problem.

Maybe I am not understanding the use of the WindowCall function and the SetWindowsHookeX, could you explain me how to use it without need of timers

Thanks a lot I really appreciate your help
 
you have tried increasing the timer interval right? if it's set to 1, that's sort of like taching out your engine in neutral. Does it have to be that low to catch messages?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top