Wednesday, May 07, 2008

Convert VC++6 to VC++7 -- (2)

Below is a laundry list of the problems and solutions.

Compiler Error: fatal error C1083: Cannot open include file: 'fstream.h': No such file or directory
Solution: change include from fsream.h to fstream, and add using namespace std;
Related problems: ios::nocreate and ios::noreplace are deprecated. ios::nocreate is replace by ios::in

CFile::ReadHuge() and CFile::WriteHuge() are obsolete. Use CFile::Read() and CFile::Write() instead.

Replace &afxChNil by empty string "".

Compiler Error:
  • error C2059: syntax error : '<'
  • error C2143: syntax error : missing ';' before '<'
  • error C2182: 'ConstructElements' : illegal use of type 'void'
  • error C2988: unrecognizable template declaration/definition
Solution: ConstructElements and DestructElements are deprecated. Remove definition of these functions. Ref: Microsoft KB 318734

WINVER default changed to 0x0501 (Windows XP). If your program still want to support Windows 2000, the the following line must be included in the project:
#define WINVER 0x0400

CPropetySheetEx, CPropertyPageEx are deprecated, they are included back to CPropertySheet and CPropertyPage.


CString s(45) no longer compiles. Because CString is changed to template based function with more constructors. This definition has ambiguous overloaded constructors. Change to CString s((TCHAR)45).



VC++7.1 has stricter type requirements. For example, conversion from HANDLE to int and uint is not allowed.

Compiler Error:
error C2668: 'sqrt' : ambiguous call to overloaded function
Solution:
sqrt, fabs, log and other CRT math functions support both double and float type now. An integer input to these functions will cause the error above.
Example:
int i = 10;
float f = sqrt(i); //error C2688
float f = sqrt((float)i); //compiles ok


Compiler Error:
error C2440: 'static_cast' : cannot convert from 'void (__thiscall XXXXXXXX::* )(void)' to 'void (__thiscall XXXXXX::* )(NMHDR *,LRESULT *)'
Solution:
Function signature of event handler OnKillfocus was changed from void Func(void) to void Func(NMHDR*, LRESULT*).

Thursday, May 01, 2008

Convert VC++6 to VC++7 -- (1)

VC++6 is a pretty solid product, which can still produce decent software in Windows XP. But Microsoft has decided to stop support for VC6 for about 2 years now. With the arrival of VS2008 and Windows Vista, it might finally be the time to convert those old projects that you still want to keep alive into a newer platform.

To start the conversion is really easy, just open the .dsp file in Visual Studio 2003. It will convert the project for you automatically, and generate the .vcproj and .sln files. If you are lucky, the next step is press F7 to build the solution and press F5 to run it, and you are done. Unfortunately, 90% of us will need to change the code to make it even compile. VC++6 is less standard compliant than VC++7 and there are some other breaking changes in VC++7.

This series will report problems I've seen when I convert a VC++6 project to VC++7 and some tips to help smooth the process.

Tip 1: for a project that is still actively maintained, during the conversion care must be taken so that changes do not break the software under VC++6. Use this to allow the code changes exists peacefully in VC++6:

#if _MSC_VER >=1500
//this is VC++9.0 or above
#elif _MSC_VER >= 1400
// this is VC++8.0
#elif _MSC_VER >= 1310
// this is VC++7.1
#elif _MSC_VER > 1300
// this is VC++7.0
#else
//assume VC++6
#endif