Click to See Complete Forum and Search --> : Problems with window creation, window messages and a DLL.


Rancorb
March 25th, 2008, 10:53 AM
Hello,

I did have a search but couldn't see anything that matched my problem. If this has already been addressed then I'd appreciate it if you could point me at the right thread. I'm a Java programmer by profession and I haven't done much C++ or Windows programming in the last 10 years, so am slightly stumped by my current problem.

I'm writing a C++ DLL that's being loaded in a Java application. The DLL needs to use the windows messaging structure (i.e. WndProc callback method) to receive data from a third party application. I'm having some strange problems and I have a few questions I would like help with. Answers to any of the questions I have will be gratefully received.

When the DLL is loaded I launch a new thread which runs the message processing loop, using _beginthreadex( NULL, 0, &WindowThread, &hModule, 0, NULL ). I am doing this to prevent the message loop from blocking the DLLMain from returning, hence possible blocking the return of the loadLibrary method in java.

Q1: Do I need to have a thread for the message loop or am I just complicating the code?

After the DLL is loaded, Java calls the interface to trigger connection to the third party DLL. The Window is created at this point. I've followed what I believe is the usual procedure for creating a window:
- Create a WNDCLASSEX structure and register it using RegisterClassEx.
- Call CreateWindowEx to get a hWnd object.
- ShowWindow(hWnd, SW_SHOWNORMAL)
- UpdateWindow(hWnd)

The ShowWindow returns false at this stage, but the window is still displayed on screen. The WndProc method WM_CREATE case is reached.

Q2: Is the ShowWindow returning false because I'm calling it from a DLL, or is it because I'm doing something wrong?
Q3: Is false returned from ShowWindow actually a problem in this case or is it a red herring?

I think that the failure of ShowWindow is somehow causing problems with my access to windows messages.

In my WndProc method, the WM_CREATE case calls a 3rd party DLL to initiate communication - otherInterface.ContactLauncher(...);
I should receive a user defined windows message in my WndProc method (OTHERINTERFACE_LAUNCHER_CONTACTED) in response.

I only receive this message if I have used MessageBox(NULL, "Debug", "Output", MB_OK) in key places in the code. The most obvious example of this so far is that if I used MessageBox to report the failure of ShowWindow, and I received the response message. When I commented out the MessageBox at that point, then I didn't receive the response message.

Q4: Is MessageBox doing something behind the scenes (windows creation, loading objects, threading) which has the side effect of allowing me to receive the third party message?
Q5: Is a problem with ShowWindow returning false going to cause me to fail to receive messages?

and just thought of this last one too...

Q6: Is separating the message processing loop from the creation of the window likely to cause problems?

If you need more information, specifics or code fragments (once I figure out how to do them properly) then let me know and I'll be happy to provide what I can.

Thanks,

Tim

kirants
March 25th, 2008, 04:29 PM
I'm a Java programmer by profession and I haven't done much C++ or Windows programming in the last 10 years
Welcome to C++ world :wave:
When the DLL is loaded I launch a new thread which runs the message processing loop, using _beginthreadex( NULL, 0, &WindowThread, &hModule, 0, NULL ). I am doing this to prevent the message loop from blocking the DLLMain from returning, hence possible blocking the return of the loadLibrary method in java.
Your intentions are good, but spawning a thread in DllMain , if that is where you are doing, is recipe for disaster. I'd strongly recommend you move this code to the connection triggering API that is called later after Dll initialization.

Q1: Do I need to have a thread for the message loop or am I just complicating the code?
A window needs messages to be "pumped" into it. This is performed by a traditional message loop. If a message loop is not gauranteed, you are better off implementing one yourself. In that respect, you are headed the right direction.


After the DLL is loaded, Java calls the interface to trigger connection to the third party DLL. The Window is created at this point. I've followed what I believe is the usual procedure for creating a window:
- Create a WNDCLASSEX structure and register it using RegisterClassEx.
- Call CreateWindowEx to get a hWnd object.
- ShowWindow(hWnd, SW_SHOWNORMAL)
- UpdateWindow(hWnd)

Fair enough, but remember , the window is now created in a different thread than the you spawned earlier. So, that thread is essentially of no use. A window has to have a message pump in the thread it was created in. More later..

The ShowWindow returns false at this stage, but the window is still displayed on screen. The WndProc method WM_CREATE case is reached.

Q2: Is the ShowWindow returning false because I'm calling it from a DLL, or is it because I'm doing something wrong?
Q3: Is false returned from ShowWindow actually a problem in this case or is it a red herring?

Read up on documentation of ShowWIndow API. The return values you see are harmless.

I think that the failure of ShowWindow is somehow causing problems with my access to windows messages.

Don't think so
In my WndProc method, the WM_CREATE case calls a 3rd party DLL to initiate communication - otherInterface.ContactLauncher(...);
I should receive a user defined windows message in my WndProc method (OTHERINTERFACE_LAUNCHER_CONTACTED) in response.

I only receive this message if I have used MessageBox(NULL, "Debug", "Output", MB_OK) in key places in the code. The most obvious example of this so far is that if I used MessageBox to report the failure of ShowWindow, and I received the response message. When I commented out the MessageBox at that point, then I didn't receive the response message.

Q4: Is MessageBox doing something behind the scenes (windows creation, loading objects, threading) which has the side effect of allowing me to receive the third party message?

You are right. MessageBox implements a message loop. So, if this MessageBox happens in the same thread as the thread from which CreateWindow happened, you will see those messages being pumped into your WndProc.


Q5: Is a problem with ShowWindow returning false going to cause me to fail to receive messages?

No.

Q6: Is separating the message processing loop from the creation of the window likely to cause problems?

That IS the problem in your case. Ideally this should be the sequence:


java loads dll. Dll does most probably nothing in your case.
Jave calls some initialization method of dll. Dll first lauches a thread if not present already.
Thread procedure first creates a window, and then implements the message loop.

Rancorb
March 26th, 2008, 12:05 PM
Hi Kirants, thanks for the reply.

I was beginning to suspect it was some form of threading issue by the time I'd finished writing everything out. I'll move the code round a bit as you suggest and see if that solves the problem.

It's good to know that it wasn't ShowWindow. It'll be interesting to see if that starts working properly too once everything is in the right thread.

I'll let you know if it works once I've had a chance to work on it again.

Tim