11 Replies - 6631 Views - Last Post: 06 July 2012 - 10:36 AM Rate Topic: -----

#1 Jimbo7136  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-July 12

Making a basic win32 app

Posted 05 July 2012 - 03:36 PM

I'm dipping my toe into the win32 pool with this code from this tutorial.

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   LPSTR szCmdLine, int nShowCmd)
{
	static char name[] = "My Application";
	WNDCLASSEX wc;
	HWND hwnd;
	MSG Msg;

	//Step 1: Registering the Window Class
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style		 = CS_HREDRAW | CS_VREDRAW; 
	wc.lpfnWndProc	 = WndProc;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = name;
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Registration Failure"),
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	// Step 2: Creating the Window
	hwnd = CreateWindowEx(
		WS_EX_CLIENTEDGE,
		name,
		"My First Window",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, 400, 400,
		NULL, NULL, hInstance, NULL);

	ShowWindow(hwnd, nShowCmd);
	UpdateWindow(hwnd);

	// Step 3: The Message Loop
	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return (int)Msg.wParam;
}

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



I'm getting the following errors building it, though. Maybe someone can look at it, and see what I'm missing here.

Errors:

Error 1 error C2440: '=' : cannot convert from 'char [15]' to 'LPCWSTR' c:\users\marcus\documents\visual studio 2010\projects\myfirstwindow\myfirstwindow\myfirstwindow.cpp 24

Error 2 error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'char [15]' to 'LPCWSTR' c:\users\marcus\documents\visual studio 2010\projects\myfirstwindow\myfirstwindow\myfirstwindow.cpp 41

3 IntelliSense: a value of type "char *" cannot be assigned to an entity of type "LPCWSTR" c:\users\marcus\documents\visual studio 2010\projects\myfirstwindow\myfirstwindow\myfirstwindow.cpp 24

4 IntelliSense: argument of type "char *" is incompatible with parameter of type "LPCWSTR" c:\users\marcus\documents\visual studio 2010\projects\myfirstwindow\myfirstwindow\myfirstwindow.cpp 37

5 IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR" c:\users\marcus\documents\visual studio 2010\projects\myfirstwindow\myfirstwindow\myfirstwindow.cpp 38

Is This A Good Question/Topic? 0
  • +

Replies To: Making a basic win32 app

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3621
  • View blog
  • Posts: 11,280
  • Joined: 05-May 12

Re: Making a basic win32 app

Posted 05 July 2012 - 03:38 PM

Well, you are running in the same ANSI/UNICODE issues all over again...
Was This Post Helpful? 0
  • +
  • -

#3 Jimbo7136  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-July 12

Re: Making a basic win32 app

Posted 05 July 2012 - 03:41 PM

View PostSkydiver, on 05 July 2012 - 04:38 PM, said:

Well, you are running in the same ANSI/UNICODE issues all over again...

Yeah the first two I ran into I fixed by wrapping TEXT() around them, these I've never seen before though.

I fixed it, nevermind. Sorry to bother you.

This post has been edited by Jimbo7136: 05 July 2012 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3621
  • View blog
  • Posts: 11,280
  • Joined: 05-May 12

Re: Making a basic win32 app

Posted 05 July 2012 - 03:57 PM

Remember what I said in the other thread about char, and using wchar_t instead? Or use the WCHAR macro.

I highly recommend getting a copy of Petzold's Programming Windows. It will ease your transition into Windows programming a lot better. http://www.amazon.co...ramming+windows
Was This Post Helpful? 1
  • +
  • -

#5 Jimbo7136  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-July 12

Re: Making a basic win32 app

Posted 05 July 2012 - 04:00 PM

View PostSkydiver, on 05 July 2012 - 04:57 PM, said:

Remember what I said in the other thread about char, and using wchar_t instead? Or use the WCHAR macro.

I highly recommend getting a copy of Petzold's Programming Windows. It will ease your transition into Windows programming a lot better. http://www.amazon.co...ramming+windows


Thank you for the suggestion, I'll look into the text you suggested.
Was This Post Helpful? 0
  • +
  • -

#6 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Making a basic win32 app

Posted 05 July 2012 - 04:19 PM

In Visual C++, change the character set to Multi-Byte under "Configuration Proprieties/ General/Character Set". I would say that it't the easiest and, for the most part, the best option.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3621
  • View blog
  • Posts: 11,280
  • Joined: 05-May 12

Re: Making a basic win32 app

Posted 05 July 2012 - 04:44 PM

Yes, that is a good short term solution, but looking at Jimbo7136's trajectory, I'm guessing that he'll he asking about COM within the next two weeks, if not within a week. Might as well go UNICODE now rather than dealing with the headache of of doing all the ANSI to UNICODE conversions later since COM is UNICODE only.

The downside of going UNICODE with windows is that there's a speedbump when you want to crib code that is written for Linux which typically uses ASCII, but is supposedly UTF-8. Have to do a little bit of porting.

Don't get me wrong. I resisted moving to UNICODE as well when I moved from Win16 to Win32 programming. I kept on thinking about all those unused high bytes in all those strings... Later, I saw code running through a profiler with all the thunking that was happening to convert ANSI API calls to UNICODE calls, as well as some perf numbers of the processor handling even numbered addresses for pointers better. Then I finally drank the Koolaid and went UNICODE.

This post has been edited by Skydiver: 05 July 2012 - 04:49 PM

Was This Post Helpful? 1
  • +
  • -

#8 Jimbo7136  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-July 12

Re: Making a basic win32 app

Posted 05 July 2012 - 08:38 PM

View PostSkydiver, on 05 July 2012 - 04:57 PM, said:

Remember what I said in the other thread about char, and using wchar_t instead? Or use the WCHAR macro.

I highly recommend getting a copy of Petzold's Programming Windows. It will ease your transition into Windows programming a lot better. http://www.amazon.co...ramming+windows


What about the sixth edition of petzold's book? Is there a reason I should go with the fifth edition specifically?
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3621
  • View blog
  • Posts: 11,280
  • Joined: 05-May 12

Re: Making a basic win32 app

Posted 05 July 2012 - 09:52 PM

You can hear/read in straight from the horses mouth: http://www.charlespe...Programmer.html

Metro style apps will be a new paradigm compared to classic Win32 apps. If you want something more modern than Win32, I recommend learning WPF or WinForms in C#, until Metro proves itself in the industry.

This post has been edited by Skydiver: 05 July 2012 - 09:55 PM

Was This Post Helpful? 1
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: Making a basic win32 app

Posted 06 July 2012 - 04:40 AM

Nice post, thanks.

I love Petzold. I still have his Windows 95 book on my shelf. Writing windows programs in C is fun and also gives a deeper understanding of how the whole mess works.

I'd agree that C# is the way to go for modern Windows. Well, to be honest, any .NET language, but VB and C# are the darlings of the suite and VB is fugly. The post is interesting in that Charles declares himself a C# guy and offers C++ code that actually C++ CLI.

Managed C++ is an unholy fusion of C++ and .NET that is basically still born but is still dragged out because a dead baby is maybe better than no baby at all. If you're going to use C++ CLI, don't. Use C#.

For the OP. For Win32 I use C ( keeps me honest. ) The switch in VS is actually Character Set: Not Set. This lets the char * fly. For a standard ASCII based Windows program, this works. Move your program to a system that uses unicode (e.g Chinese Windows) and it can crash like a turkey. It depends on if you're learning from code that chose an old style. If you're down in Win32, old style constructs are common.
Was This Post Helpful? 0
  • +
  • -

#11 Jimbo7136  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-July 12

Re: Making a basic win32 app

Posted 06 July 2012 - 10:12 AM

View PostSkydiver, on 05 July 2012 - 10:52 PM, said:

You can hear/read in straight from the horses mouth: http://www.charlespe...Programmer.html

Metro style apps will be a new paradigm compared to classic Win32 apps. If you want something more modern than Win32, I recommend learning WPF or WinForms in C#, until Metro proves itself in the industry.


I'm on chapter 3 of the 5th edition, I think it's solid, just a little dated, it mentions the state of the art as being Windows 98 and NT 4.0.

I don't know what to think of C#.

WPF seems like something I should know, but first things first, I have to gain a solid foundation in Win32, I think.

This post has been edited by Jimbo7136: 06 July 2012 - 10:17 AM

Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3621
  • View blog
  • Posts: 11,280
  • Joined: 05-May 12

Re: Making a basic win32 app

Posted 06 July 2012 - 10:36 AM

Considering that the 5th edition was published back in 1998 or 1999, it's amazing how well the text holds up. I'm glad you are getting value out of the book. I still go back to it from time to time to reinforce some first concepts and know what to expect before I try to debug Win32 code written with ATL or MFC code.

Speaking ATL and MFC, I know that you are only in the early chapters of Petzold, but one thing you'll notice quickly is that there is a lot of boilerplate code. ATL and MFC help you focus on your app rather than the Win32 minutia of the boilerplate. I highly recommend ATL over MFC despite there being a lot more Internet resources that can help you with MFC. ATL is much thinner and cleaner than MFC. You'll have a much better chance of debugging a crash in a Win32 program using ATL and understand what is going on vs. MFC which seems to revel in side-effects. You'll have to be pretty up on use of C++ templates, though.

For WPF, a solid understanding of Win32 isn't as necessary. Useful, yes, but you can get by without. For WinForms, once you get beyond basic dialogs and controls, a solid understanding of Win32 is needed.

This post has been edited by Skydiver: 06 July 2012 - 10:53 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1