Win32 program compile errors

LPCWSTR fails my application

Page 1 of 1

4 Replies - 4024 Views - Last Post: 20 August 2007 - 10:07 AM Rate Topic: -----

#1 Soundwave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-August 07

Win32 program compile errors

Posted 19 August 2007 - 02:03 PM

I've been starting with some basics and going over everything. I've written a basic Win32 Windows App that will draw a bouncing ball and use basic physics to have the ball bounce. I've tried everything I can think of but I can't get it compile properly.

#include <windows.h>

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

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPreInst, LPSTR lpszCmdLine, int nCmdShow)
{
	HWND		hWnd;
	MSG			msg;
	WNDCLASSEX	wc;

	//fill the WNDCLASSEX object with the appropriate values
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInst;
	wc.hIcon = LoadIcon(NULL, IDI_EXCLAMATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "BouncingBall";
	wc.hIconSm = LoadIcon(NULL, IDI_EXCLAMATION);

	//register the new class
	RegisterClassEx(&wc);

	//create a window
	hWnd = CreateWindowEx(
		NULL,
		wc.lpszClassName,
		"The Bouncing Ball Program",
		WS_OVERLAPPEDWINDOW | WS_VISIBLE,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		NULL,
		NULL,
		hInst,
		NULL
	);

	//event loop - handle all messages
	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	//standard return value
	return (int)(msg.wParam);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	//static variables used to keep track of the balls position
	static int dX = 5, dY = 5; //stores directions
	//stores position
	static int x = 0, y = 0, oldX = 0, oldY = 0;
	//device context and brush used for drawing
	HDC hDC;
	HBRUSH brush;

	//find out which message is being set
	switch(nMsg)
	{
	case WM_CREATE:
		//create the timer (0.02 seconds)
		SetTimer(hWnd, 1, 20, NULL);
		break;

	case WM_TIMER:
		//when the timer goes off (only one)
		//get the dc for drawing
		hDC = GetDC(hWnd);
		//use pure white
		brush = (HBRUSH)SelectObject(hDC, GetStockObject(WHITE_BRUSH));

		//fill a RECT object with the appropriate values
		RECT temp;
		temp.left = oldX;
		temp.top = oldY;
		temp.right = oldX + 30;
		temp.bottom = oldY + 30;

		//cover the old ellipse
		FillRect(hDC, &temp, brush);
		//get ready to draw the new ellipse
		brush = (HBRUSH)SelectObject(hDC, GetStockObject(GRAY_BRUSH));
		//draw it
		Ellipse(hDC, x, y, 30 + x, 30 + y);
		//update the values
		oldX = x;
		oldY = y;
		//prep the new coordinates for next time
		x += dX;
		y += dY;
		//get the window size and store it in rect
		RECT rect;
		GetClientRect(hWnd, &rect);
		//if the circle is going off the edge then reverse its direction
		if(x + 30 > rect.right || x < 0)
		{
			dX = -dX;
		}
		if(y + 30 > rect.bottom || y < 0)
		{
			dY = -dY;
		}
		//put the old brush back
		SelectObject(hDC, brush);
		//release the dc
		ReleaseDC(hWnd, hDC);
		break;

	case WM_DESTROY:
		//destroy the timer
		KillTimer(hWnd, 1);
		//end the program
		PostQuitMessage(0);
		break;

	default:
		//let Windows handle every other message
		return(DefWindowProc(hWnd, nMsg, wParam, lParam));

	}

	return 0;
}



Compiler errors

Quote

c:\documents and settings\kevin\my documents\prog projects\bounce\bounce\bounce.cpp(22) : error C2440: '=' : cannot convert from 'const char [13]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\kevin\my documents\prog projects\bounce\bounce\bounce.cpp(42) : error C2664: 'CreateWindowExW' : cannot convert parameter 3 from 'const char [26]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


I've tried changing 'wc.lpszClassName' around, LPSTR as well as CreateWindowEx...so far nothing has had any luck with running the program.

Any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: Win32 program compile errors

#2 vijaysnkar  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 11-August 07

Re: Win32 program compile errors

Posted 20 August 2007 - 12:49 AM

Hey buddy,

Your code is working fine for me. Am using Borland Compiler to compile your code. I didnt find any problem in your code and it results good.

View PostSoundwave, on 19 Aug, 2007 - 02:03 PM, said:

I've been starting with some basics and going over everything. I've written a basic Win32 Windows App that will draw a bouncing ball and use basic physics to have the ball bounce. I've tried everything I can think of but I can't get it compile properly.

#include <windows.h>

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

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPreInst, LPSTR lpszCmdLine, int nCmdShow)
{
	HWND		hWnd;
	MSG			msg;
	WNDCLASSEX	wc;

	//fill the WNDCLASSEX object with the appropriate values
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInst;
	wc.hIcon = LoadIcon(NULL, IDI_EXCLAMATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "BouncingBall";
	wc.hIconSm = LoadIcon(NULL, IDI_EXCLAMATION);

	//register the new class
	RegisterClassEx(&wc);

	//create a window
	hWnd = CreateWindowEx(
		NULL,
		wc.lpszClassName,
		"The Bouncing Ball Program",
		WS_OVERLAPPEDWINDOW | WS_VISIBLE,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		NULL,
		NULL,
		hInst,
		NULL
	);

	//event loop - handle all messages
	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	//standard return value
	return (int)(msg.wParam);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	//static variables used to keep track of the balls position
	static int dX = 5, dY = 5; //stores directions
	//stores position
	static int x = 0, y = 0, oldX = 0, oldY = 0;
	//device context and brush used for drawing
	HDC hDC;
	HBRUSH brush;

	//find out which message is being set
	switch(nMsg)
	{
	case WM_CREATE:
		//create the timer (0.02 seconds)
		SetTimer(hWnd, 1, 20, NULL);
		break;

	case WM_TIMER:
		//when the timer goes off (only one)
		//get the dc for drawing
		hDC = GetDC(hWnd);
		//use pure white
		brush = (HBRUSH)SelectObject(hDC, GetStockObject(WHITE_BRUSH));

		//fill a RECT object with the appropriate values
		RECT temp;
		temp.left = oldX;
		temp.top = oldY;
		temp.right = oldX + 30;
		temp.bottom = oldY + 30;

		//cover the old ellipse
		FillRect(hDC, &temp, brush);
		//get ready to draw the new ellipse
		brush = (HBRUSH)SelectObject(hDC, GetStockObject(GRAY_BRUSH));
		//draw it
		Ellipse(hDC, x, y, 30 + x, 30 + y);
		//update the values
		oldX = x;
		oldY = y;
		//prep the new coordinates for next time
		x += dX;
		y += dY;
		//get the window size and store it in rect
		RECT rect;
		GetClientRect(hWnd, &rect);
		//if the circle is going off the edge then reverse its direction
		if(x + 30 > rect.right || x < 0)
		{
			dX = -dX;
		}
		if(y + 30 > rect.bottom || y < 0)
		{
			dY = -dY;
		}
		//put the old brush back
		SelectObject(hDC, brush);
		//release the dc
		ReleaseDC(hWnd, hDC);
		break;

	case WM_DESTROY:
		//destroy the timer
		KillTimer(hWnd, 1);
		//end the program
		PostQuitMessage(0);
		break;

	default:
		//let Windows handle every other message
		return(DefWindowProc(hWnd, nMsg, wParam, lParam));

	}

	return 0;
}



Compiler errors

Quote

c:\documents and settings\kevin\my documents\prog projects\bounce\bounce\bounce.cpp(22) : error C2440: '=' : cannot convert from 'const char [13]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\kevin\my documents\prog projects\bounce\bounce\bounce.cpp(42) : error C2664: 'CreateWindowExW' : cannot convert parameter 3 from 'const char [26]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


I've tried changing 'wc.lpszClassName' around, LPSTR as well as CreateWindowEx...so far nothing has had any luck with running the program.

Any suggestions?

Was This Post Helpful? 0
  • +
  • -

#3 Soundwave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-August 07

Re: Win32 program compile errors

Posted 20 August 2007 - 09:24 AM

Damn. It must be the fact that I'm using VS.NET 2005. Any loop arounds for this? (In VS.NET 2005 that is)
Was This Post Helpful? 0
  • +
  • -

#4 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Win32 program compile errors

Posted 20 August 2007 - 09:40 AM

Just a check. (I don't know Win32 programming.) Its working fine for me in VC++ 6 and Dev-C++.
Try
wc.lpszClassName= TEXT("BouncingBall");
//and
TEXT("The Bouncing Ball Program"),



It works with that.( I don't know why. All I did was search MSDN).
Hope this helps :)!!
Was This Post Helpful? 0
  • +
  • -

#5 Soundwave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-August 07

Re: Win32 program compile errors

Posted 20 August 2007 - 10:07 AM

Awesome, I will give that a try, hopefully it works. Would be strange if that's all it was. Stupid .NET
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1