Solved

getmessage

Posted on 2002-06-10
24
770 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:thor918
  • 15
  • 9
24 Comments
 
LVL 5

Expert Comment

by:rpai
ID: 7068008
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
 
LVL 2

Author Comment

by:thor918
ID: 7068126
yes I know.. but It hangs without the loop too
0
 
LVL 5

Expert Comment

by:rpai
ID: 7068253
0
 
LVL 2

Author Comment

by:thor918
ID: 7069647
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
 
LVL 2

Author Comment

by:thor918
ID: 7069655
and when I lookup the peekcommand it says ->
If no messages are available, the return value is zero
0
 
LVL 5

Expert Comment

by:rpai
ID: 7071080
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
 
LVL 2

Author Comment

by:thor918
ID: 7072819
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
 
LVL 2

Author Comment

by:thor918
ID: 7072996
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
 
LVL 2

Author Comment

by:thor918
ID: 7073000
in what I logged from the peekmessage it should find wparam->201
0
 
LVL 5

Expert Comment

by:rpai
ID: 7073134
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
 
LVL 2

Author Comment

by:thor918
ID: 7073203
hmm it still hangs.. I can't press the ctrl+pause/breake
0
 
LVL 5

Accepted Solution

by:
rpai earned 89 total points
ID: 7073347
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:thor918
ID: 7073751
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
 
LVL 2

Author Comment

by:thor918
ID: 7073766
okey.. If I write an 0.. it doesn't get the handle
0
 
LVL 2

Author Comment

by:thor918
ID: 7073770
I mean, it dosent find the window
0
 
LVL 2

Author Comment

by:thor918
ID: 7073775
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
 
LVL 5

Expert Comment

by:rpai
ID: 7073859
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
 
LVL 2

Author Comment

by:thor918
ID: 7073878
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
 
LVL 5

Expert Comment

by:rpai
ID: 7073928
Thats great news!! So your problem resolved?
0
 
LVL 2

Author Comment

by:thor918
ID: 7073956
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
 
LVL 5

Expert Comment

by:rpai
ID: 7073974
Good Luck!
0
 
LVL 2

Author Comment

by:thor918
ID: 7078369
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
 
LVL 2

Author Comment

by:thor918
ID: 7150620
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
 
LVL 5

Expert Comment

by:rpai
ID: 7165300
Thor,

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

Cheers!
RP.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now