PDA

View Full Version : A Win32 API question, relating to WindowsXP.


Stragen
11-02-2002, 04:21 AM
Before today, the last time I did any coding was two months ago. At the time I had Windows 2000 installed and my program operated exactly as it was intended. Since then I have installed Windows XP. This morning to my dismay I discovered that my program no longer operates, causing an exception in user32.dll upon execution.

I found that the exception was occuring upon calling the RegisterClass function, as demonstrated in the code below:

bool Win32_Window::Register(WNDPROC proc)
{
hInstance = GetModuleHandle(NULL);

WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = proc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszClassName = WIN_CLASSNAME;

if(!RegisterClass(&wc)) return false;
return true;
}

Due to my total lack of skill I am unable to resolve this problem and are forced to ask the question: If this code works for Win2K, why not for XP? Am I missing something obvious? Do I suck?

Are there changes in the structs or functions relating to this proccess in the Win32 API in WindowsXP? If so, where is documentation available? (as I was unable to find any)

bwkaz
11-02-2002, 11:43 AM
Well, since I don't have source code for XP, I can't tell what the problem is for sure. But there are a couple of things you might want to look at:

(1) Is hInstance valid? I don't know if GetModuleHandle can fail, but if it does, you're not checking for that.

(2) Could proc possibly be a null pointer? It's the address of the window procedure, I know, but where's that function at? If it's a class member function, that can sometimes screw things up, though not often.

(3) Are LoadIcon and/or LoadCursor failing? If these handles were invalid, that could possibly cause problems...

Is RegisterClass a Win32 API function? It doesn't sound familiar (not that that means anything; it's been forever and a day since I've touched win32 C code)...

You can do most of these failure checks just before you call RegisterClass. If something's NULL or invalid or whatever, it should show up there...

Stragen
11-03-2002, 01:44 AM
Strangely, the program began to function today without changing any code. I find this very odd, perhaps there was some problem with the windows system during yesterdays instance of its operation. Nonetheless I have taken the precautions you mentioned. I did not think GetModulehandle could fail but upon reading the specifications I saw that it infact can, and have taken neccessary measures to check this and the other things you mentioned. Thanks for your input - and reminding me not to assume that a functions inputs / returns will always be valid (something I seem to easily forget).