New to Directx

Started by
5 comments, last by Depressedrobot 17 years, 7 months ago
I recently started Directx, and I bought a book on it, but when I try to compile the code for the first chapter (opening a window) I get the following error messages. 1>c:\documents and settings\michael\my documents\visual studio 2005\projects\example1\example1\winmain.cpp(19) : error C2731: 'WinMain' : function cannot be overloaded 1> c:\documents and settings\michael\my documents\visual studio 2005\projects\example1\example1\winmain.cpp(16) : see declaration of 'WinMain' 1>c:\documents and settings\michael\my documents\visual studio 2005\projects\example1\example1\winmain.cpp(52) : error C2440: '=' : cannot convert from 'const char [15]' to 'LPCWSTR' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 1>c:\documents and settings\michael\my documents\visual studio 2005\projects\example1\example1\winmain.cpp(67) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [15]' 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\Michael\My Documents\Visual Studio 2005\Projects\example1\example1\Debug\BuildLog.htm" 1>example1 - 3 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== The book came with a CD with all the source code, and even when I copy and paste the code into VCpp I get the same problem. Here is the code

/****************************************************************
* example1
* this application shows how to setup a standard windows 
* application
****************************************************************/
#include <windows.h>

// global variables
HINSTANCE hInst;
HWND wndHandle;

// forward declarations
bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, 
				   HINSTANCE hPrevInstance, 
				   LPTSTR lpCmdLine, int nCmdShow)
{
	if (!initWindow(hInstance))
		return false;
	
	// Main message loop:
    MSG msg; 
    ZeroMemory( &msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
			TranslateMessage( &msg );
            DispatchMessage( &msg );
        }		
    }
		
	return (int) msg.wParam;
}

bool initWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= 0;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "DirectXExample";
	wcex.hIconSm		= 0;
	RegisterClassEx(&wcex);

	// create the window
	wndHandle = CreateWindow("DirectXExample", 
							 "DirectXExample", 
							 WS_OVERLAPPEDWINDOW,
							 CW_USEDEFAULT, 
							 CW_USEDEFAULT, 
							 640, 
							 480, 
							 NULL, 
							 NULL, 
							 hInstance, 
							 NULL);
   if (!wndHandle)
      return false;
   
   ShowWindow(wndHandle, SW_SHOW);
   UpdateWindow(wndHandle);

   return true;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

Please Help.
Advertisement
1: Check the WinMain's third parameter type carefully.
2: Prefix the string literals with L, such that "DirectXExample" becomes L"DirectXExample". This will "convert" the strings to correct type.
3: Repeat point #2 with the other string literals.

Your problems have absolutely nothing to do with DirectX, by the way.

Niko Suni

Thanks,

#2 worked really well and I'm not getting those "Cannot be converted" errors.

The third paremeter in WinMain is exactly the same as it is in the book, if the book has a typo, please tell me what I should change it to because I'm still getting the "WINAPI cannot be overloaded" error.
LPSTR instead of LPTSTR :)

See here. I also recommend reading MSDN in other situations; it contains, among other very useful stuff, the entire Windows library reference.

Niko Suni

I tried that and I got 11 new error messages:

1>winmain.obj : error LNK2019: unresolved external symbol __imp__DispatchMessageW@4 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__TranslateMessage@4 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__PeekMessageW@20 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__UpdateWindow@4 referenced in function "bool __cdecl initWindow(struct HINSTANCE__ *)" (?initWindow@@YA_NPAUHINSTANCE__@@@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__ShowWindow@8 referenced in function "bool __cdecl initWindow(struct HINSTANCE__ *)" (?initWindow@@YA_NPAUHINSTANCE__@@@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__CreateWindowExW@48 referenced in function "bool __cdecl initWindow(struct HINSTANCE__ *)" (?initWindow@@YA_NPAUHINSTANCE__@@@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__RegisterClassExW@4 referenced in function "bool __cdecl initWindow(struct HINSTANCE__ *)" (?initWindow@@YA_NPAUHINSTANCE__@@@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__LoadCursorW@8 referenced in function "bool __cdecl initWindow(struct HINSTANCE__ *)" (?initWindow@@YA_NPAUHINSTANCE__@@@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__DefWindowProcW@16 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__PostQuitMessage@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)
1>C:\Documents and Settings\Michael\My Documents\Visual Studio 2005\Projects\example1\Debug\example1.exe : fatal error LNK1120: 10 unresolved externals
1>Build log was saved at "file://c:\Documents and Settings\Michael\My Documents\Visual Studio 2005\Projects\example1\example1\Debug\BuildLog.htm"
1>example1 - 11 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Have you linked the Windows lib files (kernel32.lib, user32.lib)?

Possible solution.

Niko Suni

How do I link them?

This topic is closed to new replies.

Advertisement