VB.net SendMessage waiting problem

findwindowsendmessagevb.netvisual-studio-2010

I am using the following code to click a button for displaying another form to click on:

Dim hwnd As Integer = FindWindow(vbNullString, "Virtual CDRom Control Panel")
Dim x As Integer = FindWindowEx(hwnd, 0, vbNullString, "Driver Control ...")

SendMessage(x, BM_CLICK, 0&, 0&)
Thread.Sleep(200)
hwnd = FindWindow(vbNullString, "Virtual CD-ROM Driver Control")
Debug.Print(hwnd)

Problems is that when it gets to

SendMessage(x, BM_CLICK, 0&, 0&)

to click the button, it stops the code there until i exit out of the box that pops up. I want to be able to continue without having to exit the box since the next line

hwnd = FindWindow(vbNullString, "Virtual CD-ROM Driver Control")

finds the pop up window and will click on a button inside that box.

Any help would be great! :o)

David

SOLVED

Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWndParent As IntPtr, ByVal hWndChildAfter As Integer, ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As IntPtr
Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As IntPtr

Dim hwnd As IntPtr = FindWindow(vbNullString, "Virtual CDRom Control Panel")
Dim x As IntPtr = FindWindowEx(hwnd, 0, vbNullString, "Driver Control ...")

PostMessage(x, BM_CLICK, 0&, 0&)
Thread.Sleep(200)
hwnd = FindWindow(vbNullString, "Virtual CD-ROM Driver Control")
Debug.Print(hwnd)

Best Solution

Try changing SendMessage to PostMessage.

Also please note all HWNDs must be declared as IntPtr.
Also wParam and lParam for SendMessage and PostMessage are IntPtrs.
This will make you code compatible with x64 environment.

Related Question