Posted by Fox on 2000-05-24
> Hi,
>
> i'm learning win32 api programming and to do that i have been writing a
> simple program that get the input of the user in tree edit controls, convert
> that to hex, and show it in another control. but i'm having (very
> bad)problems becoze windows show me a kernel32.dll page fault when executes
> this code:
>
> /* (global variables) */
> struct rgb {
> int r, g, b;
> } fg, bg, *cg;
>
> cg = &fg;
>
>
> /* (this is from the WndProc function) */
>
> case WM_COMMAND:
> switch(HIWORD(wParam)) {
> case EN_CHANGE:
> memset(buffer, 0, MAX_BUF);
> switch(LOWORD(wParam)) {
> case IDE_RED:
> GetDlgItemText(hwnd, IDE_RED, buffer, 3);
> cg->r = atoi(buffer);
> break;
>
> case IDE_GREEN:
> GetDlgItemText(hwnd, IDE_GREEN, buffer, 3);
> cg->g = atoi(buffer);
> break;
>
> case IDE_BLUE:
> GetDlgItemText(hwnd, IDE_BLUE, buffer, 3);
> cg->b = atoi(buffer);
> break;
> }
> memset(buffer, 0, MAX_BUF);
> wsprintf(buffer, "#%02X%02X%02X", cg->r, cg->g, cg->b);
> SetDlgItemText(hwnd, IDE_RESULT, buffer);
> break;
> }
> break;
>
>
> cg is a pointer to a normal structure, and buffer i delcared like this:
> char buffer[100];
>
> i don't know what to do because the code seem to be fine.
>
> btw: i'm compiling this with gcc 2.95.2 - minwg32
/*********************************************/
One thing that might be an issue is: do you KNOW FOR SURE that the
strings in 'buffer' are null terminated. I assume they are, but check to make sure
anyway. atoi() does some wierd things when you pass a sting that's not null
terminated. I think they are, but check just to eliminate the possibility.
In 'memset()' why are you setting the array to 0? Try setting it to ' ' (a space)
in each element of the array.
Take a look at 'wsprintf()', the second parameter takes null terminated string.
Just to eliminate the possibility, add a null terminator to the end of that statement
just to see that it's not the problem.
Also, look at the final three parameters in wsprintf(). Through any given pass
of the message loop, ONLY one of those 'rgb' fields is filled with the 'atoi()' value.
The rest aren't initalized. It could be that wsprintf() is sometimes trying to convert
some sort of random garbage value(a value that wasn't set with atoi()) to
hexadecimal, while the value that was set with atoi() might be converting properly.
The other two, unintialized(unfilled) 'rgb' values could be causing an error. If the
uninitialized value there can't be changed to hexadecimal.........possible crash 'n'
burn.
Try initializing each 'rgb' field first. Set all of them to 0, then go about the
program normally.
Honestly though, I don't see anything wrong with the code, which means that the
problem is so subtle that it's easially overlooked. The only things that
POTENTIALLY might be wrong are above. That's all that seems out of place to
me. I hope this helps.
/*
Remember, complex problems almost
always have simple solutions!
Previous post | Next post | Timeline | Home