Sign in to follow this  
Plasmarobo

WinAPI rejecting c++?

Recommended Posts

When I try and call CreateWindowEx using string literals I get conversion errors: COMPILE LOG
1>------ Rebuild All started: Project: Austens Direct X, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'Austens Direct X', configuration 'Debug|Win32'
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>Austens Direct X.cpp
1>c:\documents and settings\admin\my documents\c++ projects\direct x\austens direct x\austens direct x\austens direct x.cpp(36) : error C2440: '=' : cannot convert from 'const char [14]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\admin\my documents\c++ projects\direct x\austens direct x\austens direct x\austens direct x.cpp(51) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [14]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>Build log was saved at "file://c:\Documents and Settings\Admin\My Documents\C++ Projects\Direct X\Austens Direct X\Austens Direct X\Debug\BuildLog.htm"
1>Austens Direct X - 2 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

My CPP file:
// Austens Direct X.cpp : Defines the entry point for the application.
//
#include <windows.h>
#include <windowsx.h>
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include "Austens Direct X.h"

#define WIN32_LEAN_AND_MEAN

LRESULT CALLBACK MsgHandler(
    HWND hwnd,     // window handle
    UINT msg,      // the message identifier
    WPARAM wparam, // message parameters
    LPARAM lparam  // more message parameters
);

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WindowsClass;                                   // declare structure variable

WindowsClass.cbSize =        sizeof(WNDCLASSEX);           // always use this!
WindowsClass.style =         CS_DBLCLKS | CS_OWNDC |
                            CS_HREDRAW | CS_VREDRAW;      // standard settings
WindowsClass.lpfnWndProc =   MsgHandler;                   // we need to write this!
WindowsClass.cbClsExtra =    0;                            // extra class info, not used
WindowsClass.cbWndExtra =    0;                            // extra window info, not used
WindowsClass.hInstance =     hinstance;                    // parameter passed to WinMain()
WindowsClass.hIcon =         LoadIcon(NULL, IDI_WINLOGO);  // Windows logo
WindowsClass.hCursor =       LoadCursor(NULL, IDC_ARROW);  // standard cursor
WindowsClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);  // a simple black brush
WindowsClass.lpszMenuName =  NULL;                         // no menu
WindowsClass.lpszClassName = "Windows Class";                // class name
WindowsClass.hIconSm =       LoadIcon(NULL, IDI_WINLOGO);  // Windows logo again

RegisterClassEx(&WindowsClass);
MSG msg;
HWND hwnd;

if (!(hwnd = CreateWindowEx(NULL,                   // extended style, not needed
                            "Windows Class",         // class identifier
                            "Windows Class",        // window title
                            WS_POPUP | WS_VISIBLE,  // parameters
                            0, 0, 320, 240,         // initial position, size
                            NULL,                   // handle to parent (the desktop)
                            NULL,                   // handle to menu (none)
                            hinstance,              // application instance handle
                            NULL)))                 // who needs it?
return(0);

bool bactive = true;

while(bactive)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}


}
    return(0);
}

LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	switch(msg)
	{
	case WM_CREATE:
		//Initialize game here
		return(0);
	}

    return(DefWindowProc(hwnd, msg, wparam, lparam));
}

Share this post


Link to post
Share on other sites
Sounds to me like you have Unicode enabled; find the box in the Project Settings menu that says Character Set and switch from Unicode to Multi-Byte Character Set.

EDIT: You'll find it under Project->Properties->Configuration Properties->General.

Share this post


Link to post
Share on other sites
You're compiler is defaulting to Unicode. This means that characters are not 16-bits rather than 8 for Windows functions (Win32 uses UTF-16 internally). To fix this you can either wrap your string literals in TEXT() (that is "Window Class" would become TEXT("Window Class") or you can change your compiler's settings to not use Unicode.

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