getmessage

Hi I'm trying to fetch the messages an application sends.
like what microsoft spy++ are able to do.. here is a log from that apllication->
<00001> 000100DE P message:0x052E [User-defined:WM_USER+302] wParam:00000901 lParam:00006486
<00002> 000100DE P message:0x052E [User-defined:WM_USER+302] wParam:00000901 lParam:00006486
<00003> 000100DE P message:0x052E [User-defined:WM_USER+302] wParam:00000901 lParam:00006486
<00004> 000100DE P message:0x052E [User-defined:WM_USER+302] wParam:00000901 lParam:00006486
<00005> 000100DE P message:0x052E [User-defined:WM_USER+302] wParam:00000901 lParam:00006486
<00006> 000100DE P message:0x052E [User-defined:WM_USER+302] wParam:00000901 lParam:00006486
<00007> 000100DE P message:0x052E [User-defined:WM_USER+302] wParam:00000901 lParam:00006486
<00008> 000100DE P message:0x0530 [User-defined:WM_USER+304] wParam:00000901 lParam:00006486
<00009> 000100DE P message:0x052E [User-defined:WM_USER+302] wParam:00000901 lParam:00006486
<00010> 000100DE P message:0x052E [User-defined:WM_USER+302] wParam:00000901 lParam:00006486
<00011> 000100DE P message:0x052E [User-defined:WM_USER+302] wParam:00000901 lParam:00006486
<00012> 000100DE P message:0x052E [User-defined:WM_USER+302] wParam:00000901 lParam:00006486


Here is how I have tried to fetch the messages from an application.
module.bas
'Option Explicit

'All the Declarations
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public 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 Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As Msg, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
Public Declare Function TranslateMessage Lib "user32" (lpMsg As Msg) As Long
Public Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As Msg) As Long

Public Const WM_USER = &H400
Public Const WM_WA_IPC = WM_USER
Public Const WM_COPYDATA = &H4A
Public Const WM_COMMAND = &H111

Public Type POINTAPI
    x As Long
    y As Long
End Type

Public Type Msg
    hwnd As Long
    message As Long
    wParam As Long
    lParam As Long
    time As Long
    pt As POINTAPI
End Type

In form->
Private Sub Command1_Click()
Dim wMsg As Msg
hwnd_tablet = FindWindow("TBLMOUSE", vbNullString)

         Do While GetMessage(wMsg, hwnd_tablet, 0, 0)
            Call TranslateMessage(wMsg)
            Call DispatchMessage(wMsg)
         Loop
Label2.Caption = wMsg.message
End Sub

This hangs the visual basic...
I'm not even close to know how to do this, so please help
LVL 2
thor918Asked:
Who is Participating?
 
rpaiConnect With a Mentor Commented:
Surprising enough, it works for me without hanging. Mentioned below is a little variation of the code and the values displayed by debug.print.

-----------------Start Code--------------------------------
Private Sub Command1_Click()

Dim wMsg As Msg
Dim hwnd_tablet As Long
Dim lParam As Long
Dim wParam As Long
Dim i As Integer

hwnd_tablet = FindWindow("TBLMOUSE", vbNullString)
Do While (GetMessage(wMsg, hwnd_tablet, 0, 0)) <> 0 And i <= 9
        i = i + 1
        TranslateMessage wMsg
        DispatchMessage wMsg
        Debug.Print i & vbTab & wMsg.message  
Loop

End Sub
-----------------------End Code----------------------------
----------------------Results displayed--------------------
1   4186
2   512
3   15
4   15
5   15
6   512
7   512
8   257
9   512
10  512
-----------------------End of Results----------------------
0
 
rpaiCommented:
Why do you have a Loop?
Do While GetMessage(wMsg, hwnd_tablet, 0, 0)
.
.
Loop
This goes in an infinite loop causing the application to hang.
0
 
thor918Author Commented:
yes I know.. but It hangs without the loop too
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
rpaiCommented:
0
 
thor918Author Commented:
hmm I see.. The code examples are unfourtantly in c++
So I don't know how to fix it in visual basic..

But after I looked in api manual, I found one api called "peekmessage"
I tried to use this command.. and it didn't hang,, but it didn't return anything either.

Public Declare Function PeekMessage Lib "user32" Alias _
        "PeekMessageA" (lpMsg As Msg, ByVal hwnd As Long, _
        ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax _
        As Long, ByVal wRemoveMsg As Long) As Long

Public Const PM_NOREMOVE = &H0
Public Const VK_RETURN = &HD
 

Public Type Msg
    hwnd As Long
    message As Long
    wParam As Long
    lParam As Long
    time As Long
    pt As POINTAPI
End Type

Private Sub Command2_Click()
  Dim Result&, pMsg As Msg
 hwnd_tablet = FindWindow("TBLMOUSE", vbNullString)
    Result = PeekMessage(pMsg, hwnd_tablet, 0, 0, PM_NOREMOVE)
    If Result <> 0 And pMsg.wParam = VK_RETURN Then
      If pMsg.lParam And &H1000000 Then
        Me.Caption = "Entertaste des Numpads"
      Else
        Me.Caption = "Normale Entertaste"
      End If
    Else
      Me.Caption = ""
    End If
End Sub


result is always 0.. I'm stuck again ;(
0
 
thor918Author Commented:
and when I lookup the peekcommand it says ->
If no messages are available, the return value is zero
0
 
rpaiCommented:
thor918,

The URL I mentioned in my previous post uses PeekMessage.

The major difference between the GetMessage and PeekMessage is in the case where there are no messages to be returned to the application. GetMessage puts the application to sleep while PeekMessage returns to the application with a NULL value.

The 'sleep mode' of the GetMessage API is exactly the reason why your application used to hang when using GetMessage API and it does not with PeekMessage.

To test PeekMessage, try the following lines of code in a VB form.
--------------------Start Code----------------------------
Option Explicit
Const PM_NOREMOVE = &H0

Private Sub Command1_Click()
Dim wMsg As Msg
Dim hwnd_tablet As Long
hwnd_tablet = FindWindow("notepad", "untitled")
Do While (PeekMessage(wMsg, hwnd_tablet, 0, 0, PM_NOREMOVE)) <> 0
        Call TranslateMessage(wMsg)
        Call DispatchMessage(wMsg)
        Debug.Print wMsg.message  
Loop
End Sub
------------------End Code---------------------------------

The application will go in an infinite loop. To stop the application, hit [ctrl + Pause/Break] key together.
Debug.print would have logged several non-zero values in the Immediate Window of your VB application.

Hope this helps.
0
 
thor918Author Commented:
thanks.. I see that there are severeal values fetched.. Now I just have to figure out witch of them is the value I'm looking for.
0
 
thor918Author Commented:
hmm I can't seem to find similar values fetched through the peekmessage..

What I need is the values that got the wm_user+304
there are no wparam with value 201 or 401

<00006> 000100E2 P message:0x0530 [User-defined:WM_USER+304] wParam:00000201 lParam:00006486
<00018> 000100E2 P message:0x0530 [User-defined:WM_USER+304] wParam:00000401 lParam:00006486

and this is what I have logged out of the peek message ->
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10804996 Message=512 Lparm=786462 Wparm=0
Time=10805307 Message=512 Lparm=655390 Wparm=0
Time=10805357 Message=512 Lparm=524318 Wparm=0
Time=10805377 Message=512 Lparm=458782 Wparm=0
Time=10805417 Message=512 Lparm=327710 Wparm=0
Time=10805417 Message=512 Lparm=327710 Wparm=0
Time=10805487 Message=512 Lparm=262174 Wparm=0
Time=10805527 Message=512 Lparm=131102 Wparm=0
Time=10805567 Message=160 Lparm=1114143 Wparm=2
Time=10805597 Message=160 Lparm=983071 Wparm=2
Time=10805647 Message=160 Lparm=589856 Wparm=2
Time=10805677 Message=160 Lparm=327713 Wparm=2
Time=10805727 Message=160 Lparm=65569 Wparm=2
Time=10805737 Message=160 Lparm=33 Wparm=2
Time=10805737 Message=160 Lparm=33 Wparm=2
Time=10805737 Message=160 Lparm=33 Wparm=2
Time=10805737 Message=160 Lparm=33 Wparm=2
Time=10805737 Message=160 Lparm=33 Wparm=2
Time=10805737 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10806018 Message=160 Lparm=33 Wparm=2
Time=10808311 Message=160 Lparm=5 Wparm=3
Time=10808381 Message=160 Lparm=0 Wparm=3
Time=10808531 Message=160 Lparm=0 Wparm=3
Time=10808531 Message=160 Lparm=0 Wparm=3
Time=10808531 Message=160 Lparm=0 Wparm=3
Time=10808531 Message=160 Lparm=0 Wparm=3
Time=10808531 Message=160 Lparm=0 Wparm=3
Time=10808531 Message=160 Lparm=0 Wparm=3
Time=10808531 Message=160 Lparm=0 Wparm=3
Time=10808531 Message=160 Lparm=0 Wparm=3
Time=10808531 Message=160 Lparm=0 Wparm=3
Time=10809523 Message=160 Lparm=118 Wparm=2
Time=10809663 Message=160 Lparm=125 Wparm=2
Time=10809753 Message=512 Lparm=4718780 Wparm=0
Time=10809793 Message=512 Lparm=524320 Wparm=0
Time=10809793 Message=512 Lparm=524320 Wparm=0
Time=10809793 Message=512 Lparm=524320 Wparm=0
Time=10809793 Message=512 Lparm=524320 Wparm=0
Time=10810274 Message=512 Lparm=65796 Wparm=0
Time=10810364 Message=512 Lparm=196941 Wparm=0
Time=10810454 Message=512 Lparm=1311070 Wparm=0
Time=10810544 Message=512 Lparm=196951 Wparm=0
Time=10810684 Message=512 Lparm=655717 Wparm=0
Time=10810725 Message=512 Lparm=786792 Wparm=0
Time=10810805 Message=512 Lparm=852328 Wparm=0
Time=10810835 Message=512 Lparm=983400 Wparm=0
Time=10810875 Message=512 Lparm=1048938 Wparm=0
Time=10810895 Message=512 Lparm=1048939 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0
Time=10810905 Message=512 Lparm=1114476 Wparm=0

0
 
thor918Author Commented:
in what I logged from the peekmessage it should find wparam->201
0
 
rpaiCommented:
hmm..No clue on why PeekMessage does not return the expected values.

I thought of giving GetMessage a try again.
Mentioned below is the code.
------------------------Start Code-------------------------
Dim wMsg As Msg
Dim hwnd_tablet As Long
Dim lParam As Long
Dim wParam As Long

hwnd_tablet = FindWindow("TBLMOUSE", vbNullString)

Do While (GetMessage(wMsg, hwnd_tablet, 0, 0)) <> 0
        TranslateMessage wMsg
        DispatchMessage wMsg
        Debug.Print wMsg.message
Loop
End Sub
-----------------------End Code----------------------------
The application will go in an infinite loop. To stop the application, hit [ctrl + Pause/Break] key together.

Now that we are trying to revert back to GetMessage, hope you are issues with  wParam are resolved.

Good Luck!
0
 
thor918Author Commented:
hmm it still hangs.. I can't press the ctrl+pause/breake
0
 
thor918Author Commented:
Hmm strange.. when I use vbNullString, it hangs.
But if I change it to a zero, it works.
But I still can't get the same values as I get in spy++ (Microsoft Visual studio tool).

Is the values in desimal, hex or something?
0
 
thor918Author Commented:
okey.. If I write an 0.. it doesn't get the handle
0
 
thor918Author Commented:
I mean, it dosent find the window
0
 
thor918Author Commented:
So this means that when I put an zero in,it will fetch all messages in windows.. but when it has the window handel it hangs..strange
0
 
rpaiCommented:
Do you mean replace vbnullstring with a zero in the FindWindow API? I do not think FindWindow would return a successful handle when zero is used. Does it?
Try using vbNull instead, if that helps.
0
 
thor918Author Commented:
yes I meant that..and you are correct it will not return an handle,here is an example->
hwnd_tablet = FindWindow("TBLMOUSE", 0)
GetMessage(wMsg, hwnd_tablet, 0, 0)

The hwnd_tablet returns 0, then the getmessage will be like this->
GetMessage(wMsg, 0, 0, 0)
and recives all windows messages.. But it doesn't hang then.
0
 
rpaiCommented:
Thats great news!! So your problem resolved?
0
 
thor918Author Commented:
I'm not so sure.. it wont give the same values I get in the spy++.. I will have to play with it before I can say anything.
0
 
rpaiCommented:
Good Luck!
0
 
thor918Author Commented:
I found out that this don't work with peekmessage either.
I had forgotten to remove "untitled", so now I figured out that the peekmessage and getmessage don't work with a spesified handle. Pretty strange huh?

But both of the commands return messages with a non spesified handle.. But still it returns values I'm not looking for.. The part I don't understand is why the values are different than the ones in spy++.
0
 
thor918Author Commented:
I haven't gotten this to work yet.. but you have helped me alot.. You got the points and grade a.. Have a nice summer.. ;) best regards
Thor
0
 
rpaiCommented:
Thor,

Thank you for the points!
Please let me know if you get this working.

Cheers!
RP.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.