Sign in to follow this  
Illuminate

It's ok, but unresolved external symbols

Recommended Posts

Illuminate    139
Looking through the gl.h I saw that it is using GDI to work. I find some help for GDI and decided to make GDI Window. Nehe's first lesson use GDI by CreateWindowEx(), but if I make GDI window without GL I use CreateWindow() with hDC, hInstance, hWnd but without hRC (its only for GL). Here source file:
#include <windows.h>

HDC hDC;
HWND hWnd;
HINSTANCE hInstance;
bool keys[256];
LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL CreateCustomWindow(char* title, int width, int height)
{
	WNDCLASS WndClass;
	RECT WndRect;
	WndRect.top = (long) 0;
	WndRect.left = (long) 0;
	WndRect.right = (long) width;
	WndRect.bottom = (long) height;
	hInstance = GetModuleHandle(NULL);
	WndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	WndClass.lpfnWndProc = (WNDPROC) WndProc;
	WndClass.cbClsExtra = 0;
	WndClass.cbWndExtra = 0;
	WndClass.hInstance = hInstance;
	WndClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
	WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	WndClass.hbrBackground = NULL;
	WndClass.lpszMenuName = NULL;
	WndClass.lpszClassName = "CustomWindow";
	hWnd = CreateWindow(	"CustomWindow",
							title,
							CS_HREDRAW | CS_VREDRAW,
							0,
							0,
							width,
							height,
							NULL,
							NULL,
							hInstance,
							NULL);
	return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_SYSCOMMAND:
		switch(wParam)
		{
		case SC_SCREENSAVE:
		case SC_MONITORPOWER:
			return 0;
		}
		break;
	case WM_CLOSE:
		PostQuitMessage(0);
		return 0;
	case WM_KEYDOWN:
		keys[wParam] = true;
		return 0;
	case WM_KEYUP:
		keys[wParam] = false;
		return 0;
	}
	return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
On compile it has no error and warnings, but when linking I receive these errors:
Quote:
--------------------Configuration: hwnd - Win32 Debug-------------------- Linking... LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16 Debug/hwnd.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. Creating browse info file... hwnd.exe - 2 error(s), 0 warning(s)
Where is the problem? P.S. Sorry for my bad English ;)

Share this post


Link to post
Share on other sites
rip-off    10978
you need a WinMain function. if you have one then its not getting included for compilation. otherwise, i dont do windows programming but i think it takes a HINSTANCE and a LPTSTR, IIRC.


int WinMain( HINSTANCE inst, LPTSTR args )
{
CreateCustomWindow("my window!", 300, 300);
// more stuff here
return 0;
}


have fun!

Share this post


Link to post
Share on other sites
Illuminate    139
To work the program I added:


int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
CreateCustomWindow("My Window", 640, 480);
return 0;
}



10x to rip-off
Now program can build but no effect. Program create window but its not specified nShowCmd. If I change any arguments of WinMain it's an error. I Must call this function with arguments (hInstance, NULL, /*Something*/, SW_SHOW) What would be lpCmdLine ?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
It seems to me that you've done a bit of coding in C++ before, so I'll freely use terminology you should already know.

In windowed programs on Windows (often called Win32 programs), WinMain takes the place of main, which you've probably seen before. This means that you never "call" WinMain in your code - it's the entry point for your program and is called by the operating system. The penultimate parameter, CmdLine, is the command line argument string passed to your executable (very much akin, but not identical to, argv in a usual C/C++ program). CmdShow is just a number that represents certain attributes your window is expected to have when it is created (i.e., visible, maximized), but it's your responsibility to make sure these traits are actually used.

My recommendation is to get a good book on the Win32 API and programming in the Windows environment, if you feel that you're sufficiently advanced to move into Win32. Petzold is a good author here. For extensive coverage from a game programming perspective, TotWGPG-2e by LaMothe will be your bible.

To help you get started, see what happens when you insert the following in between you call to create the window and the return statement:

while(1)
{
MSG msg;
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;

TranslateMessage(&msg);
DispatchMessage(&msg);
}
}


I leave any syntax errors as an exercise for you, the reader.

Peace,
ZE.

Share this post


Link to post
Share on other sites
Illuminate    139
Quote:
Original post by Anonymous Poster
while(1)
{
MSG msg;
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;

TranslateMessage(&msg);
DispatchMessage(&msg);
}
}


Function PeekMessage gets the message, handle to window, min, max (messages), PM_REMOVE/PM_NOREMOVE. Handle to window is hWnd in my code.
It isn't true that I never use WndProc. Program starts with WinMain Function, then call CreateCustomWindow where register RECT (rectangle of the window) and WNDCLASS (window class). WNDCLASS is a typedef struct tagWNDCLASS and has some variable in this struct to initialize. lpfnWndProc tells name of the WndProc.
Next Initialize of hWnd must Create the window. I change style to WS_OVERLAPPEDWINDOW (CS_HREDRAW and CS_VREDRAW must be Exstyle). After that program return TRUE and application is closed. No window? I verify what CreateWindow method return.

hWnd = CreateWindow( "CustomWindow",
title,
WS_OVERLAPPEDWINDOW,
0,
0,
width,
height,
NULL,
NULL,
hInstance,
NULL);
if (hWnd == NULL)
{
MessageBox(NULL, "Window has not been created", "Error", MB_OK | MB_ICONSTOP);
return FALSE;
}



And on start program I must receive a messagebox or a window. I receive messagebox :( I think other function are ok. My mistake must be somewhere in CreateWindow Arguments or in WndwClass. but Where???

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