PostMessage(hWnd, WM_QUIT, 0, 0)

Hi,

I have a VB.NET project. At one line of code I am getting a messagebox automatically giving some error information. I tried whatever I could to get to the root cause of the problem but I couldn't. The messagebox is not hampering any functionality and my application works fine after I close the message box. So I decided to suppress this message box. And I did this successfully usingBackgroundworkerthread and Windows API calls likeFindWindow andPostMessage.

The only problem I am facing now is after the message box is closed my application gets hanged. It is not the problem of thread because I tried closing message box with external application also. But still same thing happens. Do I need to do something else so my original application from which message box pops up can receive the control once the message box is closed?

Below given is the code causing the problem:

Code Snippet

PrivateDeclareFunction FindWindowLib"user32"Alias"FindWindowA" (ByVal lpClassNameAsString,ByVal lpWindowNameAsString)As IntPtr

PrivateDeclareFunction PostMessageLib"user32"Alias"PostMessageA" (ByVal hwndAs IntPtr,ByVal wMsgAsUInteger,ByVal wParamAs IntPtr,ByVal lParamAs IntPtr)AsBoolean

PrivateConst WM_QUIT = &H12

Dim sTitleAsString

Dim iHwndAsLong

Dim ihTaskAsLong

Dim iReturnAsLong

sTitle ="MessageBox"

iHwnd = FindWindow(Nothing, sTitle)

iReturn = PostMessage(iHwnd, WM_QUIT, IntPtr.Zero, IntPtr.Zero)

[4952 byte] By [Mukund] at [2008-1-7]
# 1

Use error handling to properly catch and handle the original error.

Code Snippet

Public SomeSubWIthError()

Try

'your buggy code here

Catch Ex as Exception

'Do whatever you want to handle the error or just ignore it (bad idea)

End Try

End Sub

TaDa at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

I have tried everything from error handling to swallowing exception but nothing works, so I had to think of suppressing the message box.

Mukund at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

Perhaps I am missing something, but if this is your application, why would you need round about ways to stop a messagebox from showing? Why can't you just modify your code to NOT show the messagebox in the first place?

Also did you try sending a WM_CLOSE message instead of WM_QUIT to see if that has any different result?

kleinma at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

>> Perhaps I am missing something, but if this is your application,

This is just a guess, but I think the messagebox is coming from a COM or other component which isn't written properly. It should throw an error, not puke up a messagebox.
TaDa at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

Yes , you are right. Actually my application is macro based and I try to open project wizard using DTE.LaunchWizard()

method.

I used WM_CLOSE and now my application is not hanging. I am aware that this is not the solution. But atleast user will not get irritated with the message box.

Mukund at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...