Jump to content
  • Advertisement
Sign in to follow this  
kaktusas2598

Win32 Hello World App problems...

This topic is 3033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Yesterday I started to learn Win32 api in C++ because I need it to know before starting learning DirectX, I wrote code from "Introduction to 3D Game Programming with DirectX 9.0" byFrank D. Luna and got 6 errors... Here's the code:
//2010 - 05 - 01
#include <windows.h>

HWND MainWindowHandle = 0;

bool InitWindowsApp(HINSTANCE instanceHandle,int show);

int Run();

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

int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   PSTR pCmdLine,
				   int ShowCmd)
{
	if(!InitWindowsApp(hInstance,ShowCmd))
	{
		::MessageBox(0,"Init - Failed","Error",MB_OK);
		return 0;
	}
	return Run();
}

bool InitWindowsApp(HINSTANCE instanceHandle,int show)
{
	WNDCLASS wc;

	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = instanceHandle;
	wc.hIcon = ::LoadIcon(0,IDI_APPLICATION);
	wc.hCursor = ::LoadCursor(0,IDC_ARROW);
	wc.hbrBackground = 
		static_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH));
	wc.lpszMenuName = 0;
	wc.lpszClassName = "Hello";

	if(!::RegisterClass(&wc))
	{
		::MessageBox(0,"RegisterClass - Failed",0,0);
		return false;
	}

	MainWindowHandle = ::CreateWindow("Hello",
		                              "Hello",
									  WS_OVERLAPPEDWINDOW,
									  CW_USEDEFAULT,
									  CW_USEDEFAULT,
									  CW_USEDEFAULT,
									  CW_USEDEFAULT,
									  0,
									  0,
									  instanceHandle,
									  0);
	if(MainWindowHandle == 0)
	{
		::MessageBox(0,"CreateWindow - Failed",0,0);
		return false;

		::ShowWindow(MainWindowHandle,show);
		::UpdateWindow(MainWindowHandle);

		return true;
	}

}

int Run()
{
	MSG msg;
	::ZeroMemory(&msg,sizeof(MSG));

	while(::GetMessage(&msg,0,0,0))
	{
		::TranslateMessage(&msg);
		::DispatchMessage(&msg);
	}
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND windowHandle,
						 UINT msg,
						 WPARAM wParam,
						 LPARAM lParam)
{
	switch(msg)
	{
	case WM_LBUTTONDOWN:
		::MessageBox(0,"Hello, World","Hello",MB_OK);
		return 0;
	case WM_KEYDOWN:
		if(wParam == VK_ESCAPE)
			::DestroyWindow(MainWindowHandle);
		return 0;
	case WM_DESTROY:
		::PostQuitMessage(0);
		return 0;
	}
	return ::DefWindowProc(windowHandle,
		                   msg,
						   wParam,
						   lParam);
}

Errors:
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [14]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
error C2440: '=' : cannot convert from 'const char [6]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [23]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [6]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [22]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [13]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

I tried to cast all char pointers, but app didn't worked for me, I really would appreciate your help mates.

Share this post


Link to post
Share on other sites
Advertisement
If you want to use multi-byte chars then include <TCHAR.H>

Windows headers use macro's to use diff versions of some routines...

if defined unicode
define MessageBox MessageBoxW
else
define MessageBox MessageBoxA
endif

Then you use _T or _TEXT macro for strings...

MessageBox(0,_T("RegisterClass - Failed"),0,0);

Share this post


Link to post
Share on other sites
<link>http://msdn.microsoft.com/en-us/library/e0w9f63b(VS.80).aspx</link>
You need to convert the pointer. I remember dealing with what you are a long time ago. Nowadays I prefer to let some other library deal with that stuff for me.

Share this post


Link to post
Share on other sites
if(MainWindowHandle == 0)
{
::MessageBox(0,"CreateWindow - Failed",0,0);
return false;

::ShowWindow(MainWindowHandle,show);
::UpdateWindow(MainWindowHandle);

return true;
}


change the above code to this:

if(MainWindowHandle == 0) {
::MessageBox(0,"CreateWindow - Failed",0,0);
return false;
}

::ShowWindow(MainWindowHandle,show);
::UpdateWindow(MainWindowHandle);

return true;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!