Thread: This is a sick error

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    This is a sick error

    Code:
    #include <windows.h>
    HRGN hRegion2,hRegion1;
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    DWORD dwStyle;
    HDC dcSkin;
    char mamu[256];
    char gamu[256];
    HBITMAP hSkinBmp,hOldBmp;
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(255, 255, 255));
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_CREATE:
                strcpy(gamu,"hello");
                dwStyle = GetWindowLong(hwnd, GWL_STYLE);
                dwStyle &= ~(WS_CAPTION|WS_SIZEBOX);
                SetWindowLong(hwnd, GWL_STYLE, dwStyle);
                SetWindowPos(hwnd,0,0,0,1024,768,SWP_SHOWWINDOW);
                ShowCursor(FALSE);
                hSkinBmp = LoadBitmap(GetModuleHandle(NULL), "THEBALL");
                if(!hSkinBmp){
                return -1;
                }
                dcSkin = CreateCompatibleDC(0);
                hOldBmp = (HBITMAP)SelectObject(dcSkin, hSkinBmp);
                break;
            case WM_PAINT:
                PAINTSTRUCT ps;
                BeginPaint(hwnd,&ps);
                BitBlt(ps.hdc,400,355,1024,768,dcSkin,0,0,SRCCOPY);
                EndPaint(hwnd,&ps);
                break;
            case WM_ACTIVATEAPP:
                if(wParam==TRUE){
                SetWindowPos(hwnd,0,0,0,1024,768,SWP_SHOWWINDOW);
                }
                else{
                SetWindowPos(hwnd,0,0,0,0,0,SWP_SHOWWINDOW);
                }
                break;
            case WM_CHAR:
                if(strlen(mamu)<250){
                strcat(mamu,(TCHAR)wParam);
                }
                if(gamu==mamu){
                MessageBox(hwnd,"You were not meant to know the secret!","Doomed",MB_OK);
                }
                break;
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    Error that I get:
    Code:
    113 C:\Programs\Dev-Cpp\Templates\solaris3.cpp invalid conversion from `TCHAR' to `const char*'

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Try changing

    Code:
    strcat(mamu,(TCHAR)wParam);
    to

    Code:
    strcat(mamu,(TCHAR*)wParam);

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    If I use TCHAR* or char* or const char* I always get illegal operation message.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    Try using the conversion function A2T() instead of just trying to force a TCHAR

    http://msdn.microsoft.com/library/de...ion_macros.asp

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Not a wise idea to hook ATL into this.
    Besides, this has nothing to do with TCHAR.

    Code:
            case WM_CHAR:
                if(strlen(mamu)<250){
                strcat(mamu,(TCHAR)wParam);
                }
                if(gamu==mamu){
                MessageBox(hwnd,"You were not meant to know the secret!","Doomed",MB_OK);
                }
                break;
    Code:
    size_t mlen = strlen(mamu);
    if(mlen < 250) {
      mamu[mlen] = wParam;
      mamu[mlen+1] = 0;
    }
    And of course you can't use == here.
    Code:
    if(strcmp(mamu, gamu) == 0) {
      MessageBox(...);
    }
    The whole TCHAR thing? Well, you can't compile this thing as a UNICODE app, because you bound yourself to ANSI, with the narrow string literals and all that.
    Which is bad, because all NT-based Windows (NT, 2k, XP, ... - everything but 95, 98 and ME) prefer UNICODE - and are slightly faster with it, too.



    Nothing sick about your error, btw. Just the usual char/string trouble that so many newcomers have.
    Last edited by CornedBee; 11-17-2005 at 12:39 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    Firstly:
    And of course you can't use == here.
    Code:
    if(strcmp(mamu, gamu) == 0) {
      MessageBox(...);
    }
    Why is that not valid? strcmp returns an interger value of 0 if the char* are the same. [NOTE] woops, sorry i was reading your corrected code instead of his original, sorry about that.

    TCHAR is not necessarily of type unicode, TCHAR is microsoft generic text and is only interpreted as unicode if #define _UNICODE is declared, otherwise it is treated as an ASCII value.
    Last edited by ExxNuker; 11-17-2005 at 12:56 PM.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by ExxNuker
    Why is that not valid? strcmp returns an interger value of 0 if the char* are the same.
    It is valid. It's my correction of the invalid code in the original.

    TCHAR is not necessarily of type unicode, TCHAR is microsoft generic text and is only interpreted as unicode if #define _UNICODE is declared, otherwise it is treated as an ASCII value.
    Nearly correct.
    TCHAR depends on the UNICODE macro, not _UNICODE. _TCHAR depends on that one.
    Also, the character set without UNICODE is rarely just ASCII. More likely it's Windows-1252, but that depends on the Windows locale settings.

    My point was that the app won't compile with UNICODE defined.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    thanks for the clarification. been trying to teach myself windows programming..still have a long ways to go.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM