Results 1 to 2 of 2

Thread: Notepad

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    Notepad

    I am just learning about win32 and am trying to create a simple notepad app, anyone know why the strcpy is causing this to crash?

    #include <windows.h>
    #include <stdlib.h>
    #include <string.h>

    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR szCmdLine, int iCmdShow)
    {
    static TCHAR szAppName[] = TEXT ("Typer") ;
    HWND hwnd ;
    MSG msg ;
    WNDCLASS wndclass ;

    wndclass.style = CS_HREDRAW | CS_VREDRAW ;
    wndclass.lpfnWndProc = WndProc ;
    wndclass.cbClsExtra = 0 ;
    wndclass.cbWndExtra = 0 ;
    wndclass.hInstance = hInstance ;
    wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
    wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndclass.lpszMenuName = NULL ;
    wndclass.lpszClassName = szAppName ;

    if (!RegisterClass (&wndclass))
    {
    MessageBox (NULL, TEXT ("This program requires Windows NT!"),
    szAppName, MB_ICONERROR) ;
    return 0 ;
    }

    hwnd = CreateWindow (szAppName, TEXT ("Typing Program"),
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT,
    CW_USEDEFAULT, CW_USEDEFAULT,
    NULL, NULL, hInstance, NULL) ;

    ShowWindow (hwnd, iCmdShow) ;
    UpdateWindow (hwnd) ;

    while (GetMessage (&msg, NULL, 0, 0))
    {
    TranslateMessage (&msg) ;
    DispatchMessage (&msg) ;
    }
    return msg.wParam ;
    }

    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    static DWORD dwCharSet = DEFAULT_CHARSET ;
    static int cxChar, cyChar,xCaret, yCaret;
    TCHAR * character;
    HDC hdc ;
    PAINTSTRUCT ps ;
    TEXTMETRIC tm ;
    TCHAR temp;
    BOOL NEWLINE= TRUE;

    switch (message){
    case WM_INPUTLANGCHANGE:
    dwCharSet = wParam ;

    // fall through
    case WM_CREATE:
    hdc = GetDC (hwnd) ;
    SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
    dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;

    GetTextMetrics (hdc, &tm) ;
    cxChar = tm.tmAveCharWidth ;
    cyChar = tm.tmHeight ;

    DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
    ReleaseDC (hwnd, hdc) ;
    character = (TCHAR*) calloc(80,sizeof(TCHAR));
    MessageBox(hwnd,"yes","yes",MB_OK);
    // fall through

    case WM_SIZE:
    // set caret to upper left corner
    xCaret = 0 ;
    yCaret = 0 ;

    if (hwnd == GetFocus ())
    SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
    InvalidateRect (hwnd, NULL, TRUE) ;
    return 0 ;

    case WM_SETFOCUS:
    // create and show the caret
    CreateCaret (hwnd, NULL, cxChar, cyChar) ;
    SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
    ShowCaret (hwnd) ;
    return 0 ;

    case WM_KILLFOCUS:
    // hide and destroy the caret
    HideCaret (hwnd) ;
    DestroyCaret () ;
    return 0 ;

    case WM_KEYDOWN:
    return (0);

    case WM_CHAR:
    /*get the character*/
    temp = (TCHAR) wParam;
    /*add the character onto the string to print*/
    if (NEWLINE == TRUE){
    strcpy(character,&temp);
    NEWLINE = FALSE;
    }
    else
    strcat(character,&temp);

    HideCaret (hwnd) ;
    hdc = GetDC (hwnd) ;

    SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0, dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;

    TextOut (hdc, xCaret * cxChar, yCaret * cyChar, character, 1) ;

    return(0);

    case WM_PAINT:
    hdc = BeginPaint (hwnd, &ps) ;
    SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
    dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;

    DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
    EndPaint (hwnd, &ps) ;
    return 0 ;

    case WM_DESTROY:
    PostQuitMessage (0) ;
    return 0 ;
    }
    return DefWindowProc (hwnd, message, wParam, lParam) ;
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Because &temp is not NUL-terminated. You have in memory:
    temp (your character, one DWORD), several DWORDS that don't belong to your app, probably not NUL.
    strcpy copies as long as the character is not NUL. The same problem with strcat. Both access unreserved memory and therefor cause an access violation that will result in termination of your application. Use strncpy and strncat, that also take the number of characters to read. Or better yet, since you seem to try to make your program UNICODE compatible, replace
    #include <string.h>
    by
    Code:
    #ifdef UNICODE
    #ifndef _UNICODE
    #define _UNICODE
    #endif
    #endif
    #ifdef _UNICODE
    #ifndef UNICODE
    #define UNICODE
    #endif
    #endif
    
    #include <tchar.h>
    and use _tcsncpy and _tcsncat. This is important because UNICODE characters are 16-bit. If you have a string function yu want to use you can either look up the name of the tchar function in MSDN, or put an underline at the front and replace str with tcs.
    Exception: strstr is _tcsstr and not _tcstcs.
    itoa becomes _itot and atoi becomes _ttoi.

    To correct your code:
    Code:
    if (NEWLINE == TRUE){ 
    _tcsncpy(character,&temp, 1); 
    NEWLINE = FALSE; 
    } 
    else 
    _tcsncat(character,&temp, 1);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width