none
C++ Express... ERROR C2664

    Question

  • These are the errors i am getting when i build...

    error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR'

    error C2664: 'UnregisterClassW' : cannot convert parameter 1 from 'const char [5]' to 'LPCWSTR'

    error C2440: '=' : cannot convert from 'const char [5]' to 'LPCWSTR'

    Heres the code:

    #include "WinMain.h"

    int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

    LPSTR lpszCmdLine, int nCmdShow)

    {

    WNDCLASSEX wcex;

    MSG msg;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style = CS_CLASSDC;

    wcex.lpfnWndProc = WndProc;

    wcex.cbClsExtra = 0;

    wcex.cbWndExtra = 0;

    wcex.hInstance = g_hInst;

    wcex.hIcon = NULL;

    wcex.hCursor = NULL;

    wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

    wcex.lpszMenuName = NULL;

    wcex.lpszClassName = g_szClassName;

    wcex.hIconSm = NULL;

    if(!RegisterClassEx(&wcex))

    {

    return 0;

    }

    g_hWnd = CreateWindow(g_szClassName, g_szWindowTitle, WS_OVERLAPPEDWINDOW,

    300, 50, 640, 480, NULL, NULL, g_hInst, NULL);

    if(!g_hWnd)

    {

    return 0;

    }

    ShowWindow(g_hWnd, SW_NORMAL);

    UpdateWindow(g_hWnd);

    ZeroMemory(&msg, sizeof(MSG));

    while(msg.message != WM_QUIT)

    {

    if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))

    {

    TranslateMessage(&msg);

    DispatchMessage(&msg);

    }

    else

    {

    }

    }

    UnregisterClass(g_szClassName, g_hInst);

    return 0;

    }

    LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)

    {

    switch(Msg)

    {

    case WM_DESTROY:

    PostQuitMessage(0);

    return 0;

    }

    return DefWindowProc(hWnd, Msg, wParam, lParam);

    }

     

    Hope someone can tell me whats going on...

    Thanks,

    Friday, May 12, 2006 8:46 PM

Answers

All replies

  • Define your strings using TCHAR, not char.
    Friday, May 12, 2006 8:51 PM
  •  

    i am recieving the error:

    Error 1 error C2664: 'SetConsoleTitleW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR' c:\documents and settings\lloyd noone\my documents\visual studio 2005\projects\monsterproject\monsterproject\conlib.cpp 69

    ive looked it up and im supposed to change it from char * to TCHAR because of unicode or something.

     

    // ConLib.cpp

    // ConLib complement header file

    #include "ConLib.h"

    // Get standard screen and keyboard handles

    ConLib::ConLib()

    {

    m_Screen = GetStdHandle (STD_OUTPUT_HANDLE);

    m_Keyboard = GetStdHandle (STD_INPUT_HANDLE);

    SetTextColor (ConRed | ConGreen | ConBlue);

    SetBackgroundColor (0);

    }

    ConLib::~ConLib()

    {

    }

    // sets background color

    void ConLib::SetBackgroundColor (WORD Color)

    {

    m_BackgroundColor = 0;

    // use bit manipulation to get the colour combinations

    if (Color & ConRed)

    {

    m_BackgroundColor |= BACKGROUND_RED;

    }

    if (Color & ConGreen)

    {

    m_BackgroundColor |= BACKGROUND_GREEN;

    }

    if (Color & ConBlue)

    {

    m_BackgroundColor |= BACKGROUND_BLUE;

    }

    // Set the color using the combinations from above

    SetConsoleTextAttribute (m_Screen, m_TextColor | m_BackgroundColor);

    }

    // sets text colour

    void ConLib::SetTextColor(WORD Color)

    {

    m_TextColor = 0;

    // use bit manipulation to get the color combinations

    if (Color & ConRed)

    {

    m_TextColor |= FOREGROUND_RED;

    }

    if (Color & ConGreen)

    {

    m_TextColor |= FOREGROUND_GREEN;

    }

    if (Color & ConBlue)

    {

    m_TextColor |= FOREGROUND_BLUE;

    }

    // set the colour using the color combinations from above

    SetConsoleTextAttribute (m_Screen, m_TextColor | m_BackgroundColor);

    }

    // sets window title

    void ConLib::SetTitle(char * Title)

    {

    SetConsoleTitle (Title);

    }

    // clears the screen

    void ConLib::Clear(void)

    {

    COORD Start;

    DWORD Written;

    Start.X = 0;

    Start.Y = 0;

    FillConsoleOutputAttribute (m_Screen, m_TextColor | m_BackgroundColor, 80*25, Start, &Written);

    FillConsoleOutputCharacter (m_Screen, ' ', 80*25, Start, &Written);

    SetConsoleCursorPosition (m_Screen, Start);

    }

    // sets the cursor position

    void ConLib::SetPosition(COORD Position)

    {

    SetConsoleCursorPosition (m_Screen, Position);

    }

    // sends a string to the screen

    void ConLib::OutputString (char * String)

    {

    DWORD Written;

    WriteConsole (m_Screen, String, strlen (String), &Written, NULL);

    }

    // Reads a string from the keyboard

    void ConLib::Read (char * Buffer, DWORD BufferSize)

    {

    DWORD Read;

    ReadConsole (m_Keyboard, Buffer, BufferSize, &Read, NULL);

    }

    // gets a key from the keyboard

    int ConLib::GetKey (void)

    {

    DWORD Read;

    INPUT_RECORD Event;

    // get console input

    ReadConsoleInput (m_Keyboard, &Event, 1, &Read);

    // if input event is a key event see if there is any key pressed

    // and return its virtual key code

    if(Event.EventType == KEY_EVENT)

    {

    if (Event.Event.KeyEvent.bKeyDown)

    {

    return Event.Event.KeyEvent.wVirtualKeyCode;

    }

    }

    return 0;

    }

    Thursday, June 01, 2006 2:05 PM
  • As Brian replied to the OP you need to use TCHAR instead of char (or wchar_t) - thie way your program will compile either as Unicode or as ASCII.
    Thursday, June 01, 2006 2:32 PM
  • As I had previously said, and as you said yourself, you need to use TCHAR.

    void ConLib::SetTitle(TCHAR * Title)
    {
       SetConsoleTitle (Title);
    }

    To extend this, it's a good idea to make the argument const (then it becomes a good idea to use the same typedef as the Platform SDK uses). Furthermore, marking the method as const is something I usually do also.

    void ConLib::SetTitle(LPCTSTR Title) const
    {
       ::SetConsoleTitle (Title); // :: helps to see that you're using a global function, hinting at an API
    }

    For a discussion of the various typedefs, read http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=136608&SiteID=1

    If it is not possible for you to use the TCHAR type everywhere, then you will need to convert the types.  If this is the case, use #include <atlconv.h> and the macro A2T (or related).  More info: http://msdn2.microsoft.com/en-US/library/87zae4a3.aspx

    Finally, _UNICODE is the cause of the use of the "wide character" APIs (the ones taking wchar_t* instead of char*)  Note that using wchar_t directly is an unusual practice.  When your project uses _UNICODE, TCHAR adapts accordingly. 

    Brian

     

     

    Thursday, June 01, 2006 2:35 PM
  •  

    whenever i use TCHAR, or tchar_w i just get an error like this:

    Error 1 error C2511: 'void ConLib::SetTitle(TCHAR)' : overloaded member function not found in 'ConLib' c:\documents and settings\lloyd noone\my documents\visual studio 2005\projects\monsterproject\monsterproject\conlib.cpp 69

    Thursday, June 01, 2006 2:58 PM
  • You need to change the method definition.  See my example.

    Thursday, June 01, 2006 3:00 PM
  • ohh yeh, you wre right. ma an idiot.

    but now i get these messages:

    a warning that i forgot to mention earlier and two errors

    Warning 1 warning C4267: 'argument' : conversion from 'size_t' to 'DWORD', possible loss of data c:\documents and settings\lloyd noone\my documents\visual studio 2005\projects\monsterproject\monsterproject\conlib.cpp 98

    Error 2 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib

    Error 3 fatal error LNK1120: 1 unresolved externals C:\Documents and Settings\Lloyd Noone\My Documents\Visual Studio 2005\Projects\MonsterProject\Debug\MonsterProject.exe

    Thursday, June 01, 2006 3:08 PM
  • I had the same problem as you working through that ConLib example - if you're still having issues with it the error arises because the example conveniently leaves out the main() function. I'm not sure if the example was meant to be complied or not, but this solved the error.

    int main(void)

    {

    return 0;

    }

    Wednesday, June 28, 2006 11:23 AM
  • yeh thats right thats what i was beginning to think it was, thanks for confirming it. it was probably written that way because yer suppossed to include it in the "Monster" example later on in the book which has a main function.

    thanks! am glad to know it will work in the end and my typin int for nothin.

    Saturday, July 01, 2006 8:12 PM
  • Hey, I've been trying to get that ConLib code to work too, but nothin I do possibly seems to fix it! I'm using VSC++ 2008, my code is currently thus:

    //Sets window title

    void

    ConLib::SetTitle(char *Title)  //SetTitle method, taking argument char *

    {

     

    SetConsoleTitle(Title);               //Do SetConsoleTitle method, using supplied argument

    }

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Error is:

    1>.\ConLib.cpp(79) : error C2664: 'SetConsoleTitleW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    So I went online and looked for possinle solutions to this error, found this webpage and tried your suggestion, but that doesnt work, I change the argument type to TCHAR in the ConLib.cpp and ConLib.h files, but I get this error instead:

    1>.\Main.cpp(14) : error C2664: 'ConLib::SetTitle' : cannot convert parameter 1 from 'const char [8]' to 'TCHAR *'
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    This is refering to the main class file, where the program is executed, there I am passing in the argument of char * type "Monsters" ..

    So back online I went and looked for more solutions and found one where I keep the SetTitle method arguments as char * type, but convert them within the method body:

    //Sets window title

    void

    ConLib::SetTitle(char *Title)

    {

    TCHAR NewTitle[256];

     

    for(int i=0; i<strlen(Title); i++)

    {

    NewTitle[i] = Title[i];

    }

    SetConsoleTitle(NewTitle);

    }

     

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    When this compiles, I get 51 error messages that are along the lines of:

    1>Main.obj : error LNK2005: "public: __thiscall CGame::CGame(void)" (??0CGame@@QAE@XZ) already defined in Game.obj


    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    So, I got rid of this solution and found another one where I explicitly declare the argument string as literal, using the L macro, when callung the SetTitle method,and changing the method argument type to TCHAR in the ConLib source and header files:

     

    //Set window title

    Console.SetTitle(L

    "Monster");

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    THIS PRODUCES THE EXACT SAME ERRORS AS WITH THE FOR-LOOP SOLUTION!!!!
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    I'm starting to think this code is self-conscious and outrightly refuses to compile for me, its almost as if it has a vendetta against me of unknown origins.... This program simply does NOT work! I'd love to hear any help and suggestions you might have....

    Sunday, March 29, 2009 1:15 AM
  • I got the same error too.  I fixed it by changing... in the ConLib.cpp file from:

    void ConLib::SetTitle( char * Title )
    {
    	SetConsoleTitle( Title );
    }
    to:
    void ConLib::SetTitle( TCHAR * Title )
    {
    	SetConsoleTitle( Title );
    }

    And... in th ConLib.h file from:

    	void SetTitle( char * Title);
    to:
    	void SetTitle( TCHAR * Title);

    And lastly... in the ConLib.cpp file, after the #include "ConLib.h" line... add:

    #include <tchar.h>

    Hope that helps.  Don't know if that will bite us in the butt later in the project.

    dj

    Sunday, September 20, 2009 8:09 PM