Library tutorials & articles
Enum Windows & SendMessage API
- Introduction
- EnumWindows
- SendMessage
- SendMessage vs PostMessage
- Conclusion
SendMessage vs PostMessage
There’s another similar API which works exactly like SendMessage and that is PostMessage API. Both require same parameters but there’s a slight difference. When a message is sent to a window with SendMessage, the window procedure is called and the calling program (or thread) waits for the message to be processed and replied back, and until then the calling program does not resume its processing. One thing is wrong with this approach however, that is if the program that is busy carrying out long instructions or a program that has been hung and hence no time to respond to the message will in turn hang your program too because your program will be waiting for a reply that may never arrive. The solution to this is to use PostMessage instead of SendMessage. PostMessage on the otherhand returns to the calling program immediately without waiting for the thread to process the message, hence saving your program from hanging. Which of them you have to use depends on your requirement.
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
I wanna ask about the equivalent of SendKey if I will use SendMessage:
Ex.
If ctr2 <= length Then
myKeys(ctr2) = Mid$(myString, ctr2, 1)
SendKeys myKeys(ctr2)
ctr2 = ctr2 + 1
Else
I tried converting SendKey with SendMessage but nothing happens:
myHWnd is the handle I got from Finding the window. What I want to do is to send specific keys to the specified application (myKeys(ctr2)) as if I am using sendKeys.
SendMessage myHWnd, WM_KEYDOWN, 0, myKeys(ctr2)
It didn't send keys to notepad.
I really cannot understand SendMessage and I really need your help guys... I am only a newbie in VB.
Thank you,
Gwapo
Hi,
About this subject, I´m trying to define the size of an image captured from a generic camera.
I'm using the SendMessage and SetWindowPos functions with the following instructions:
To initialize the camera:
Private Sub PreviewVideo(ByVal pbCtrl As PictureBox)
On Error GoTo treaterror:
mHwnd = capCreateCaptureWindowA("Teste", WS_VISIBLE Or WS_CHILD, 0, 0, 100, 100, pbCtrl.hwnd, 0)
If SendMessage(mHwnd, WM_CAP_DRIVER_CONNECT, 0, 0) Then
SendMessage mHwnd, WM_CAP_SET_SCALE, False, 0
'---set the preview rate (ms)---
SendMessage mHwnd, WM_CAP_SET_PREVIEWRATE, 30, 0
'---start previewing the image---
SendMessage mHwnd, WM_CAP_SET_PREVIEW, True, 0
'---resize window to fit in PictureBox control---
SetWindowPos mHwnd, HWND_BOTTOM, 0, 0, pbCtrl.ScaleWidth, pbCtrl.ScaleHeight, SWP_NOMOVE Or SWP_NOZORDER
Else
'--error connecting to video source---
MsgBox "Problema na conexão com a câmera. Contate o suporte!! - " & Err.Description
DestroyWindow mHwnd
End If
Exit Sub
treaterror:
MsgBox "Problema na conexão com a câmera. Contate suporte!!!" & Err.Description
End Sub
To capture the image:
Private Sub cmdCapturar_Click()
SendMessage mHwnd, GET_FRAME, 0, 0
SendMessage mHwnd, COPY, 0, 0
Picture1.Picture = Clipboard.GetData
Clipboard.Clear
'Timer1.Enabled = False
End Sub
Please note that I don't want just want to fit the image to the window. I need its real size to be the window's size. (What is happenning is that I see one image on the PictureBox passed as a parameter the sub PreviewVideo but the captured image is much larger than it.)
Thank you in advance,
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
prevWindow = 0
Call GetCursorPos(mousePT)
X = mousePT.X
Y = mousePT.Y
Label2.Text = "(" & X & "," & Y & ")"
hWnd = WindowFromPoint(X, Y)
className = Space(255) ' Make room in the string to receive the information.
retValue = GetClassName(hWnd, className, 255)
windowText = Space(255)
GetWindowText(hWnd, windowText, 255)
Label1.Text = hWnd
End Sub
this is a function which will constantly called by timer, and i have 2 labels on my form which is label 1 and and label2
you can see that i print the handle number (hWnd) using label1, but when i run the program no matter where i point the mouse to but the hwnd value is still the same, why?
Hi Riaz,
Could you plz explain me how did u fetch data from MS-Word.
I can able to get typed text from notepad using sendmessage API.
Same API Could not work for MS-word.
Thanks,
Arshad.
Hi,
Yes I could fetch data from MS- word, Notepad etc. But I am not able to fetch from HTML file. Any idea for getting from this.
Anyway thanks for your valuable help.
Regards
Riaz
I used SendMessage(hwnd, WM_COPY, 0, 0). And I meet the above problem, too!
I cann't get selected text for HTML file/VC/PDF file/SourceInsight File......
i m also looking for the same...
Can you help me some how more ?
I have developed a dictionary & now I want let user use it with a very simple way.
I wish to have a word under mouse cursor from any other windows application. my program will search definition for that word & will show at mouse cursor when user press Ctrl key or in any other special window automatically.
For the same I was searching for, I come across you. I want just any children of any window ( like..notepad, wordpad, word, or explorer or just any edit control.)
Hopes u got my requirement. Simply I have a definition dictionary & want to synchronize it with text under mouse cursor.
althogh u have given a nice idea that how to go for it but can u pls show it with a code pls.
I 'll. be greatly thankful to you, if you can illustrate it with few sample source code (Visual Basic only).
if u got it pls help me.
Thanks & have a nice time.
Sunil.
how are u?
Can you help me some how more ?
I have developed a dictionary & now I want let user use it with a very simple way.
I wish to have a word under mouse cursor from any other windows application. my program will search definition for that word & will show at mouse cursor when user press Ctrl key or in any other special window automatically.
For the same I was searching for, I come across you. I want just any children of any window ( like..notepad, wordpad, word, or explorer or just any edit control.)
Hopes u got my requirement. Simply I have a definition dictionary & want to synchronize it with text under mouse cursor.
althogh u have given a nice idea that how to go for it but can u pls show it with a code pls.
I 'll. be greatly thankful to you, if you can illustrate it with few sample source code (Visual Basic only).
Thanks & have a nice time.
Sunil.
Hi Riaz,
Could you plz explain me how did u fetch data from MS-Word.
I can able to get typed text from notepad using sendmessage API.
Same API Could not work for MS-word.
Thanks,
Arshad.
Hi,
Yes I could fetch data from MS- word, Notepad etc. But I am not able to fetch from HTML file. Any idea for getting from this.
Anyway thanks for your valuable help.
Regards
Riaz
Hi,
Yes I could fetch data from MS- word, Notepad etc. But I am not able to fetch from HTML file. Any idea for getting from this.
Anyway thanks for your valuable help.
Regards
Riaz
like the Gator client, it allow user the hold down a key e.g. ctrl and click on any text in any windows, it will pop up a definition for the clicked text, how do they do it? does it use the WM_GETTEXT as well?
I was able to do that using the SENDMESSAGE, GETCURSORPOS and WINDOWFROMPOINT functions.
Basicly what you need to do is first get the cursor's position using GETCURSORPOS which will return the x and y location. Pass those coordibates to WINDOWFROMPOINT and it will return the window handle in which the cursor is currently highlighting the text. Since you now have the correct handle of the window (this window can be anything...notepad...explorer...etc...) you then use the SENDMESSAGE function as such:
SENDMESSAGE FoundWindow.hWnd, WM_COPY, 0, 0 where 'FoundWindow' is the handle returned by WINDOWFROMPOINT.
NOTE: This method needs a constant loop to keep checking the mouse cursor position (using a timer control).
This works when a text is highlighted and the mouse cursor is still above it. If you want to do this using the CTRL key you might have to resort to GETASYNCKEYSTATE also.
If you succeed in developing a better code i'd be glad to see it
like the Gator client, it allow user the hold down a key e.g. ctrl and click on any text in any windows, it will pop up a definition for the clicked text, how do they do it? does it use the WM_GETTEXT as well?
I'm using this api to send a custom message between 2 VB exe's. The called exe is subclassed to listen to the custom message. In the calling exe, I'm trying to pass a string in the 4th parameter. At this stage the 2nd exe is already running. The api call keeps crashing the 2nd exe. Can it be done across exe's or only within the same exe, thanks.
The PostMessage function works, while the SendMessage function does not..WHY???
//*********************************
PROCESS_INFORMATION* pi;
pi = (PROCESS_INFORMATION*)malloc(sizeof(PROCESS_INFORMATION));
STARTUPINFO si;
si.cb=sizeof(si);
si.lpReserved=NULL;
si.lpDesktop=NULL;
si.lpTitle=NULL,
si.dwFlags=STARTF_FORCEONFEEDBACK;
si.cbReserved2=0;
si.lpReserved2=NULL;
BOOL bres = CreateProcess(NULL, "calc.exe", NULL,NULL,NULL,
CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS,
NULL,NULL,&si,pi);
if(!bres)
{
return;
}
HWND hWnd;
EnumThreadWindows(pi->dwThreadId, EnumProc, (LPARAM)&hWnd);
SetFocus(hWnd);
//LRESULT lr1 = SendMessage(hWnd, WM_KEYDOWN, 38, 0);
//LRESULT lr2 = SendMessage(hWnd, WM_KEYUP, 38, 1);
BOOL bp = PostMessage(hWnd, 256, 53, 393217);
LRESULT lr1 = SendMessage(hWnd, 256, 53, 393217);
//*************************************************
This thread is for discussions of Enum Windows & SendMessage API.