Sign in to follow this  
boolean010

Unhandled exception when using TextOut

Recommended Posts

boolean010    134
#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
Crypter    748

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this