Sign in to follow this  
chrisparton1991

Win32 c++ TextOut(), What's happening???

Recommended Posts

Hi. This is my first ever post on GDForums, so i dont know how it's gonna turn out. Anyway, I am learning the WIN32 API with c++. When I write a TextOut() function, it basically outputs every bit of quoted code in my program.[crying] Here's an image of the program(obvioiusly :P) wtf? Here's the source:
#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT   Ps;
    HDC          hDC;
    HBRUSH  NewBrush;

    NewBrush = CreateSolidBrush(RGB(255, 0, 0));

    switch(msg)
    {
        case WM_PAINT:
        hDC = BeginPaint(hwnd, &Ps);
        SelectObject(hDC, NewBrush);
      
        TextOut(hDC, 10, 200, "SEE? ", 100);
        EndPaint(hwnd, &Ps);
        DeleteObject(NewBrush);
        break;
        case WM_KEYDOWN:
        switch(wParam)
        {
            case VK_ESCAPE:
            DestroyWindow(hwnd);
            break;
        }
        break;
        case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
        PostQuitMessage(0);
        break;
        default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
        break;
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    LPCTSTR className = "myClass";
    WNDCLASSEX wc;

    wc.cbSize        = sizeof(wc);
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.cbWndExtra    = 0;
    wc.cbClsExtra    = 0;
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "myClass";
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 0);
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "ERROR! FAILED TO REGISTER CLASS!", "FATAL ERROR!", MB_ICONERROR | MB_OK);
        return 1;
    }

    HWND hwnd = CreateWindowEx(0, "myClass", "WINDOW TITLE", WS_OVERLAPPEDWINDOW, 0, 0, 500 + 7, 500 + 33 , NULL, NULL, hInstance, NULL);

    if(!hwnd)
    {
        MessageBox(NULL, "ERROR! FAILED TO CREATE WINDOW!", "FATAL ERROR!", MB_ICONERROR | MB_OK);
        return 1;
    }

    ShowWindow(hwnd, nShowCmd);

    MSG msg;

    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}




I realise that it's probably an extremely stupid mistake, and I have most likely made a complete fool out of myself, but... whatever. +--------------------------------------------------------------------------+ Thanks, please help, Chris Parton the first [Edited by - chrisparton1991 on July 19, 2006 5:53:21 AM]

Share this post


Link to post
Share on other sites
I think I found the problem. The last number in the function should be the length of the string.
TextOut(hDC, 10, 200, "SEE? ", 100); <- Original
TextOut(hDC, 10, 200, "SEE? ", 5); <- Fixed

Share this post


Link to post
Share on other sites
TextOut(hDC, 10, 200, "SEE? ", 100);
The last parameter is the length of the string. You're telling it there's 100 characters there, when there's actually only 5. So you have a buffer overflow. Change the last parameter to 5, or better, change it to -1 (Means this is a null terminated string)

EDIT: Too late...

Share this post


Link to post
Share on other sites
Chris - welcome to GameDev, but when you post everything in bold, it sounds like you are shouting everything. It is still before 9am in the UK and my head hurts [smile].

Share this post


Link to post
Share on other sites
No problem. BTW, the ususal approach with TextOut would be something like:

const char *s="SEE? ";
TextOut(hDC, 10, 200, s, strlen(s));

or (better)

std::string s="SEE? ";
TextOut(hDC, 10, 200, s.c_str(), s.length());

HTH

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