BleepingComputer.com: Windows programming - Errors that I cant seem to find?

Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Windows programming - Errors that I cant seem to find? C++

#1 User is offline   Wolfy87 

  • Senior Member
  • PipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 414
  • Joined: 25-July 08
  • Gender:Male
  • Location:England

Posted 27 September 2009 - 12:05 PM

I have been following a tutorial of creating a basic window with C++, I understand 90% and I am researching some of the more complicated parts, although, when I go to compile with MinGW it throws two errors.

The first is: Undefined reference to 'GetStockObject@4'
And the second is: Undefined reference to 'Ellipse@20'

Do you think these are both to do with the painting because Ellipse obviously is so maybe there is something wrong there?

Here is my source code:

#include <windows.h>

// Fuction prototypes
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );

// Create WinMain()
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow )
{
	#pragma region part 1 - STARTUP STUFF
	
	WNDCLASS wc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = TEXT("Philip");
	wc.lpszMenuName = 0;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	
	RegisterClass( &wc );
	
	HWND hwnd = CreateWindow(TEXT("Philip"), TEXT("My window's title"), WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, hInstance, NULL );
	
	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);
	
	#pragma endregion
	
	#pragma region part 2 - ENTER A LOOP TO CONTINUALLY KEEP CHECKING WITH WIN O/S FOR USER INTERACTION
	
	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	
	#pragma endregion
	
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
	switch(message)
	{
		case WM_CREATE:
			Beep(50, 10);
			return 0;
			break;
		
		case WM_PAINT:
			{
				HDC hdc;
				PAINTSTRUCT ps;
				hdc = BeginPaint(hwnd, &ps); // Error here?
				Ellipse(hdc, 10, 10, 10, 10);
				EndPaint(hwnd, &ps);
			}
			return 0;
			break;
		
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
			break;
	}	
	return DefWindowProc(hwnd, message, wparam, lparam);
}


Thanks, Wolfy87.

#2 User is offline   Wolfy87 

  • Senior Member
  • PipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 414
  • Joined: 25-July 08
  • Gender:Male
  • Location:England

Posted 27 September 2009 - 12:24 PM

Ok, I have found that if I take out the line where it draws the ellipse and where the background colour is set then it compiles but the window will not show.

#3 User is offline   Romeo29 

  • Learning To Bleep
  • PipPipPipPipPipPip
  • Find Topics
  • Group: BC Advisor
  • Posts: 2,814
  • Joined: 06-July 08
  • Gender:Not Telling
  • Location:127.0.0.1

Posted 27 September 2009 - 03:27 PM

You need to link with gdi32.lib in Visual C++ and libgdi32.a in MINGW. If you have MINGW installed and its added to PATH variable, you can give following command:
gcc hello.c -l gdi32  -o hello.exe


Also try changing the Ellipse code to something like Ellipse(hdc, 10, 10, 100, 100); to make it visible on the window. If you keep both upper-left and lower-right corrdinates at (10,10) then radius of ellipse(circle) would be zero.
[url="http://www.avast.com/"]avast! free antivirus[/url]

#4 User is offline   Wolfy87 

  • Senior Member
  • PipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 414
  • Joined: 25-July 08
  • Gender:Male
  • Location:England

Posted 28 September 2009 - 09:54 AM

Legend! :thumbsup: it worked perfectly, I did'nt realise I ever had to link in things like that with MinGW, I thought that was just in VC++ etc anyway, now to learn how to put buttons on there etc =]

Thank you so much for that, Wolfy87.

#5 User is offline   Romeo29 

  • Learning To Bleep
  • PipPipPipPipPipPip
  • Find Topics
  • Group: BC Advisor
  • Posts: 2,814
  • Joined: 06-July 08
  • Gender:Not Telling
  • Location:127.0.0.1

Posted 28 September 2009 - 12:18 PM

yw :thumbsup:
[url="http://www.avast.com/"]avast! free antivirus[/url]

#6 User is offline   Wolfy87 

  • Senior Member
  • PipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 414
  • Joined: 25-July 08
  • Gender:Male
  • Location:England

Posted 28 September 2009 - 02:26 PM

I have removed my post, no longer needed..

This post has been edited by Wolfy87: 28 September 2009 - 02:31 PM


#7 User is offline   Romeo29 

  • Learning To Bleep
  • PipPipPipPipPipPip
  • Find Topics
  • Group: BC Advisor
  • Posts: 2,814
  • Joined: 06-July 08
  • Gender:Not Telling
  • Location:127.0.0.1

Posted 28 September 2009 - 02:42 PM

In C/C++ sin() function takes angle parameter in Radians.
As far as your calculator is concerned, you are using it in Degree mode.

180 degrees = PI radians

So you can make a simple function to convert degree values to radian like
double deg2rad(float degree){
	const float PI = 3.1415;
	return degree * (PI / 180.0);
}


Now you can find out value of sin(90 degree) as sin(deg2rad(90.0)) :thumbsup:

This post has been edited by Romeo29: 28 September 2009 - 02:42 PM

[url="http://www.avast.com/"]avast! free antivirus[/url]

#8 User is offline   Wolfy87 

  • Senior Member
  • PipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 414
  • Joined: 25-July 08
  • Gender:Male
  • Location:England

  Posted 28 September 2009 - 04:59 PM

Cant try it right now, 11 o clock at night and im on my ipod but im sure it will work perfectly, you are the god of C++ in my eyes at the moment :thumbsup:

I spent over three hours trying to get it to work lol.

Thank you so much for all of your help, Wolfy87.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users