Archived

This topic is now archived and is closed to further replies.

Another win32 error!

This topic is 5310 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

kay. my compiler says something is wrong with this code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT paintStruct;
HDC hDC;
char string[] = "Hello, World!";
switch(message)
{
case WM_CREATE:
return 0;
break;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
break;
case WM_PAINT:
hDC = BeginPaint(hwnd, &paintStruct);
SetTextColor(hDC, COLORREF(0x00FF0000));
TextOut(hDC, 150, 150, string, sizeof(string)-1);
EndPaint(hwnd, &paintStruct);
return 0;
break;
}
return (DefWindowProc(hwnd, message, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX windowClass;
HWND hwnd;
MSG msg;
bool done;
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon =LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "MyClass";
windowClass.hIconSm     = LoadIcon(NULL, IDI_WINLOGO);
if (!RegisterClassEx(&windowClass))
    return 0;
hwnd = CreateWindowEx(NULL,
                      "MyClass",
                      "A REAL Windows Application!",
                      WS_OVERLAPPEDWINDOW |
                      WS_VISIBLE |
                      WS_SYSMENU,
                      100, 100,
                      400, 400,
                      NULL,
                      NULL,
                      hInstance,
                      NULL);
if (!hwnd)
    return 0;
done = false;
while (!done)
{
PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);
if (msg.message == WM_QUIT)
{
done = true;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
I just get warnings, but at the bottom, it mentipns some linking errors, regarding SetTextColor, Textout, and GetStockObject. Whats going on? Scott Simontis Game Programmer in Training Have a nice day! Current Project: Learn DirectX

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can''t do sizeof on string. String''s a pointer to a space in memory so sizeof gets you the size of the pointer IE 32bit pointer = 4 bytes. change your char string[] = "Hello, World!"; to char string[] = "Hello, World!\0"; // Added a null terminator.

then do strlen(string); instead of sizeof(string)

Share this post


Link to post
Share on other sites
Excatly WHAT warnings/errors do you get? I pasted your code into MVC++ 6.0 and it compiled fine without any warnings/errors...
However, you should consider putting WM_PAINT: inside those nice {}. Not doing so may confuse some compilers..(Havn't tried but have read about it in more than one place).

About that gdi32.lib, you need to link to it. When i removed it from the link-list i got these errors:

main.obj : error LNK2001: unresolved external symbol __imp__TextOutA@20
main.obj : error LNK2001: unresolved external symbol __imp__SetTextColor@8
main.obj : error LNK2001: unresolved external symbol __imp__GetStockObject@4

...witch seems to correspond to what you were complaining about...

quote:

You can't do sizeof on string. String's a pointer to a space in memory so sizeof gets you the size of the pointer IE 32bit pointer = 4 bytes. change your char string[] = "Hello, World!"; to char string[] = "Hello, World!\0"; // Added a null terminator.

then do strlen(string); instead of sizeof(string)



This sizeof(string)-1 where string is a char worked for me and works for you...so aparently you CAN do it. The strlen seems to work too...
However, pay attention to what the AP say...i would think that i can agree that strlen() is a better way to do it...

Later

EDIT: Fixed some spelling...

"Compuer people tend to swear a LOT!...but only to lifeless things..."

http://www.gametutorials.com


[edited by - Android_s on June 4, 2003 3:30:32 PM]

Share this post


Link to post
Share on other sites