>
>
>
Issues of 64-bit code in real programs:…

Andrey Karpov
Articles: 673

Issues of 64-bit code in real programs: virtual functions

We have already written in our articles about one of the problems of code migration to 64-bit systems relating to incorrect overload of virtual functions. For example, our article "20 issues of porting C++ code on the 64-bit platform" was published in March, 2007 (although is still relevant). It described the issue of virtual functions.

The point of the problem consists in the following. There is CWinApp class in MFC library which has WinHelp function:

class CWinApp {
  ...
  virtual void WinHelp(DWORD dwData, UINT nCmd);
};

This function must be overlapped to allow showing its own Help in a user application:

class CSampleApp : public CWinApp {
  ...
  virtual void WinHelp(DWORD dwData, UINT nCmd);
};

Everything went alright until 64-bit systems appeared. And MFC developers had to change the interface of WinHelp function (and some other functions) in this way:

class CWinApp {
  ...
  virtual void WinHelp(DWORD_PTR dwData, UINT nCmd);
};

In 32-bit mode DWORD_PTR and DWORD types coincided but in 64-bit one... Of course developers of user applications, too, had to change the type to DWORD_PTR for correct overload but the compiler did not inform about this and the error appeared only at the stage of testing when Help system began to behave "mysteriously". To learn the details I refer you to the article mentioned above.

What made me recall this error? The fact that now, in the end of 2009, this error is still present in the code of real applications. You doubt?

There is an excellent component library BCGControlBar. You must have heard about it because the components by BCGSoft Ltd are included into Microsoft Visual Studio 2008 Feature Pack. So, if you download the demo-version of this library, install it and search the word "WinHelp" through .h-files... you will see that everywhere this function is supposedly overlapped, DWORD parameter is used instead of DWORD_PTR. And this means that Help system in these classes will behave incorrectly on a 64-bit system.

Can it really be true that this error is still in the code of such a popular library? I think the point is that the company's clients have access to source codes of this library and they can always introduce some corrections into it. Besides, WinHelp function is used very rarely nowadays. HtmlHelp is much more popular. And it does have the right parameter DWORD_PTR in BCGControlBar. But the fact remains: there is an error in a real code and the compiler will not detect it.

What to do? Use PVS-Studio :-) . For our analyzer has been able to detect such errors from its very appearance, and Help system includes a detailed example (see description of error V301).