dmaroff
asked on
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);
Here is a code snippet of what Im doing.
// Needed because I can't access WinMain.
hInstance = (HINSTANCE)GetModuleHandle
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);
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 );
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
ASKER
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.
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.
ASKER
Nevermind I created a test program (console) and it will create a window. You can create Windows from as console app.
>>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()"?
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()"?
ASKER
Nevermind I created a test program (console) and it will create a window. You can create Windows from as console app.
ASKER
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
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
ASKER
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
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
ASKER
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&
http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q97758&
ASKER
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&
http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q97758&
Hmm, the article mentions "CS_GLOBALCLASS" - and if the DLL wasn't loaded, the function couldn't fail...
ASKER
CreateWindow still fails though. Very weird.
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
"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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Regards, Madshi.