• Advertisement
Sign in to follow this  

Unhandled exception when using TextOut

This topic is 3963 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

#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <windowsx.h> LRESULT CALLBACK WindowProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam ) { switch( msg ) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CREATE: return 0; case WM_PAINT: PAINTSTRUCT ps; HDC hdc; hdc = BeginPaint( hwnd, &ps ); TextOut( hdc, 100, 100, "Hello World", RGB(255,255,0) ); EndPaint( hwnd, &ps ); return 0; default: return DefWindowProc( hwnd, msg, wparam, lparam ); } } int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow ) { WNDCLASS wndclass; HWND hwnd; wndclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WindowProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hinstance; wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION ); wndclass.hCursor = LoadCursor( NULL, IDC_CROSS ); wndclass.hbrBackground = ( HBRUSH ) GetStockObject( BLACK_BRUSH ); wndclass.lpszClassName = "WINCLASS1"; wndclass.lpszMenuName = NULL; RegisterClass( &wndclass ); hwnd = CreateWindow( "WINCLASS1", "Real Windows Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hinstance, NULL ); MSG msg; ShowWindow( hwnd, ncmdshow ); UpdateWindow( hwnd ); while ( 1 ) { if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { if( msg.wParam == WM_QUIT ) break; TranslateMessage( &msg ); DispatchMessage( &msg ); } } return msg.wParam; }

Share this post


Link to post
Share on other sites
Advertisement

TextOut( hdc, 100, 100, "Hello World", RGB(255,255,0) );

The last parameter is the number of characters in the string to print,
not the color. You are recieving an access violation here because
RGB(255,255,0) represents some length of characters. Windows will use
this number to print the characters in the string. Since the number of chars
is much larger then the actual string length, a buffer overrun is encountered.

Try this instead:

TextOut( hdc, 100, 100, "Hello World", 10 );


Also, change

if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ))
{
if( msg.wParam == WM_QUIT )
break;
TranslateMessage( &msg );
DispatchMessage( &msg );
}

To the following:

if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ))
{
if( msg.message == WM_QUIT )
break;
TranslateMessage( &msg );
DispatchMessage( &msg );
}

This will fix your programs quitting code, so that it will terminate when
WM_DESTROY is sent.

Just a few minor errors[smile]

After these changes, everything should work fine.

Share this post


Link to post
Share on other sites
Yep, thanks for the reply. I change the last argument for the TextOut function and the problem went away.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement