ERROR C2440

hello, i was just messing around the other day and when i tried to compile this code and error came up.

c:\documents and settings\stealthop1217\my documents\c++ work\jyfjtjyulkuyl\jyfjtjyulkuyl\bdfbdh.cpp(26) : error C2440: '=' : cannot convert from 'const char *' to 'LPCWSTR'


heres the code:













#include <windows.h>

const char *ClsName = "BasicApp";
const char *WndName = "A Simple Window";

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam);

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
HWND hWnd;
WNDCLASSEX WndClsEx;

// Create the application window
WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WndProcedure;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClsEx.lpszMenuName = NULL;
WndClsEx.lpszClassName = ClsName;
WndClsEx.hInstance = hInstance;
WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

// Register the application
RegisterClassEx(&WndClsEx);

// Create the window object
hWnd = CreateWindow(ClsName,
WndName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);

// Find out if the window was created
if( !hWnd ) // If the window was not created,
return 0; // stop the application

// Display the window to the user
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);

// Decode and treat the messages
// as long as the application is running
while( GetMessage(&Msg, NULL, 0, 0) )
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

return Msg.wParam;
}

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
// If the user wants to close the application
case WM_DESTROY:
// then close it
PostQuitMessage(WM_QUIT);
break;
default:
// Process the left-over messages
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
// If something was not done, let it go
return 0;
}















What am i doing wrong!!!!!!!!!!!!!!!!!!!!


LPCWSTR = Long Pointer Constant WIDE String
In other words a pointer to a UNICODE string is expected.

const char *WndName = "A Simple Window"; This is a normal
8 bit ascii string.

With Microsoft Visual Studio, the default program setup is for the USE of UNICODE string - hence the problem.

To prevent this problem always wrap your strings literals with the _TEXT macro like this when coding for with Microsoft Windows;

const char *WndName = _TEXT("A Simple Window");

(Note you can also use _T("string") in Visual Studio, but I don't think DEV C++ understands _T);

The _TEXT macro either does nothing (not in UNICODE code) or it prepends the L in front of the string literal to indicate a UNICODE string if in UNICODE mode. See my next post below about the L directive.
Last edited on
Note that if you want a string literal to be absolutely UNICODE
then you do this:
(This is the c++ standard)

wchar_t* pwchar = L"Hello World";
Topic archived. No new replies allowed.