Page 1 of 1

Visual Studio Quirks

Posted: Wed Sep 23, 2009 3:41 pm
by captjack
I thought a discussion of Visual Studio quirks might be helpful. For those out there that are new to the environment, it's a bit heavy handed vis-a-vis some of the other options. When I write code for Linux, gcc is really straight forward. MS always throws in a wrinkle or two.

Case in point. When I tried to compile a small test unit, I got a cornball error that said:
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [6]' to 'LPCWSTR'
I stared at my four line WinMain() like a monkey working a math problem.

Tell-tale hint, and easy to read past, is the bit that says "MessageBoxW". I eventually noticed that my program used MessageBox.

Turns out that there are three versions of MessageBox, each one depends on compile time flags. There is: an ANSI version, "MessageBoxA"; a unicode version, "MessageBoxW"; and a TCHAR version, "MessageBox".

ANSI

Code: Select all

MessageBoxA(NULL, "Hello!", "Hello World", MB_OK | MB_ICONEXCLAMATION);
UNICODE

Code: Select all

MessageBoxW(NULL, L"Hello!", L"Hello World", MB_OK | MB_ICONEXCLAMATION);
TCHAR

Code: Select all

MessageBox(NULL, _T("Hello!"), _T("Hello World"), MB_OK | MB_ICONEXCLAMATION);
And it got better. I used the TCHAR version, since I wanted MessageBox and not one of the other two versions. When I compiled I got an error:
error C3861: '_T': identifier not found
WTF? Well, _T is defined in tchar.h so I #include'd that and Voila! My totally useless test worked. I also discovered that if you don't include the tchar.h, remove the _T(), and turn off the UNICODE flag you can compile successfully also with "MessageBox".

Go to Project->Properties->Configuration Properties->C/C++->Preprocessor and click on the three dots next to Preprocessor Definitions. Uncheck the box next to "Inherit from parent or project defaults" and click OK. This sets $(NOINHERIT) to drop unicode support.

C++ seems like child's play compared to the wacked out easter eggs in the VS IDE! :roll:

Anyone else have helpful hints or tricks for VS?

-capt jack

EDIT:
Forgot to reference my sources! :shock:
http://social.msdn.microsoft.com/Forums ... b549f04be8
http://social.msdn.microsoft.com/Forums ... 7fab8dcd21

Re: Visual Studio Quirks

Posted: Thu Sep 24, 2009 10:57 am
by MarauderIIC
Yeah. Make sure that when you start a new C++ project, you choose "empty project", so that Precompiled Headers is off. If it's not off, you probably want to turn it off (I forget where the option is), or it will bother you about stdafx.h (IIRC, that's the file name), even if you don't need it.

As for helpful hints, there's an old thread about MSVS's customizable Task List. It grabs its data from comments. So you can have it get all the "TODO"s and the "FIXME"s and put them all in one place.

I think you can have it search the project directory for library includes (so that everyone uses the same copy of GL.h out of the SVN repository, for instance, without changing the #include < > to #include " "), although I forget the variable name and don't have access to my MSVS at the moment.