Create a window inside of a DLL

Im writing a DLL and trying to create a window.  For some reason when I try the RegisterClass and CreateWindow calls CreateWindow fails with a "The system cannot find the file specified." error.  I can't seem to figure out why?  RegisterClassEx does pass, however.


Here is a code snippet of what Im doing.

// Needed because I can't access WinMain.
hInstance = (HINSTANCE)GetModuleHandle(NULL);

WNDCLASSEX wcex;

     wcex.cbSize = sizeof(WNDCLASSEX);
     wcex.style               = CS_HREDRAW | CS_VREDRAW;
     wcex.lpfnWndProc     = (WNDPROC)WndProc;
     wcex.cbClsExtra          = 0;
     wcex.cbWndExtra          = 0;
     wcex.hInstance          = hInstance;
     wcex.hIcon          = NULL;
     wcex.hCursor          = LoadCursor(NULL, IDC_ARROW);
     wcex.hbrBackground     = (HBRUSH)(COLOR_WINDOW+1);
     wcex.lpszMenuName     = NULL;
     wcex.lpszClassName     = "Testing";
     wcex.hIconSm          = NULL;

if(!RegisterClassEx(&wcex))
    MessageBox(NULL, "RegClass Failed", "Error", MB_OK);

hWnd = CreateWindow("Testing", "This is a test", WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
dmaroffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MadshiCommented:
Hmmm, on a first glance it seems to look right. Please try to put the class name into a constant. I had a case in NT4, where I had to give in the same pointer as the class name. Giving in the same string (but a different pointer) failed.

Regards, Madshi.
jkrCommented:
The "hIcon" entry in your WNDCLASSEX struct is missing. The docs say:

hIcon

Handle to the class icon. This member must be a handle of an icon resource. If this member is NULL, an application must draw an icon whenever the user minimizes the application's window.

You could just load any std. icon, e.g.

wcex.hIcon = LoadIcon(0,IDI_EXCLAMATION);
dmaroffAuthor Commented:
Well the both of you have merit to your comments.

Madshi:  I was thinking about that.  I tried it now but still have same problem.

jkr:  With your solution I now get a different error message which is somewhat good.  I get a error #6
"The handle is invalid".

Now understand.  The main program is a Console app.  Im actually writing a module for the Python language.  Im not sure if this has any bearing however.  Im creating this window inside of a thread spawned in the DLL.

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

dmaroffAuthor Commented:
Nevermind I created a test program (console) and it will create a window.  You can create Windows from as console app.
jkrCommented:
>>The main program is a Console app

This should make no difference, acutally.

>>Im creating this window inside of a thread spawned in the DLL.

This, however, would, as message processing is thread-based.

A quick shot in the dark - what happens if you add "CS_GLOBALCLASS" to the styles you use in "RegisterClassEx()"?
dmaroffAuthor Commented:
Nevermind I created a test program (console) and it will create a window.  You can create Windows from as console app.
dmaroffAuthor Commented:
Nope, same thing happens.

Here is birds eye view of my code...

1. Python calls func1
2. func1 spawns thread ThreadFunc so func1 can return
3. ThreadFunc calls RegWindowEx and CreateWindow
dmaroffAuthor Commented:
Nope, same thing happens.

Here is birds eye view of my code...

1. Python calls func1
2. func1 spawns thread ThreadFunc so func1 can return
3. ThreadFunc calls RegWindowEx and CreateWindow
dmaroffAuthor Commented:
I think I found my answer to my dismay.  Tell me if you think this is related.


http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q97758&
dmaroffAuthor Commented:
I think I found my answer to my dismay.  Tell me if you think this is related.


http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q97758&
jkrCommented:
Hmm, the article mentions "CS_GLOBALCLASS" - and if the DLL wasn't loaded, the function couldn't fail...
dmaroffAuthor Commented:
CreateWindow still fails though.  Very weird.
dpearsonCommented:
Given the two errors you've managed to get
"File not found" and "The handle is invalid"
I wonder if the problem is that Windows is
attempting to access the resources in your main
executable...and failing because it's a console app
and therefore presumably has no resources.

So to test that either add some resources to the parent
or try passing the hInstance of the DLL rather than
the console app.

My other thought is that the problem is in the pumping
of the messages to the WndProc for the new window.
CreateWindow sends a WM_CREATE message to the new window
before returning, so if that message is not being
processed correctly perhaps you'd get your error?

Doug
byte4byteCommented:
You must return 0 for WM_CREATE and TRUE for WM_NCCREATE in the wndproc. Also try setting the window to a nonzero size for the width and height.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.