Home Articles Books Downloads FAQs Tips

Q: Fix compiler errors in shellapi.h or shlobj.h


Answer

You may encounter compiler errors in the API header files shellapi.h and shlobj.h if you try to include them from a BCB5 VCL program. The problem is caused by conflicts between the API header files, and the VCL header files shlobj.hpp and shellapi.hpp. The HPP version of the files exist for the sake of other VCL headers (ie you should never need to include shlobj.hpp or shellapi.hpp in your code).

The solution to shellapi.h and shlobj.h compiler errors is to globally define the constant NO_WIN32_LEAN_AND_MEAN in your project. You can do this in the IDE from the Directories/Conditionals tab of the project options dialog. Enter NO_WIN32_LEAN_AND_MEAN in the Conditional Defines box. If you have multiple defines listed, you can separate them with semicolons.

If you compile from the commandline, you can define NO_WIN32_LEAN_AND_MEAN inside your makefile, or by passing it to make with the -D switch. You can also use the -D switch if you compile directly with bcc32.

You might be tempted to define NO_WIN32_LEAN_AND_MEAN by placing a #define statement right in the source file that needs shlobj.h or shellapi.h. Like this:

#include <vcl.h>
#pragma hdrstop

#define NO_WIN32_LEAN_AND_MEAN
#include <shellapi.h>

Unfortunately, this won't help. NO_WIN32_LEAN_AND_MEAN must be defined before you include vcl.h. To understand why, look at the files vcl0.h and shlobj.hpp. Since NO_WIN32_LEAN_AND_MEAN must be defined before including vcl.h, you could try this:

#define NO_WIN32_LEAN_AND_MEAN
#include <vcl.h>
#pragma hdrstop

#include <shellapi.h>

This will solve the compiler errors, but it will also disrupt your use of precompiled header files. As a result, your project will take longer to compile. For information on why, see the article on precompiled header use.

Since you can't use a local #define to solve the problem, you are forced to define NO_WIN32_LEAN_AND_MEAN globally in your project options or makefile. While this will solve your problems regarding shlobj.h, it does come at a cost. Defining NO_WIN32_LEAN_AND_MEAN forces vcl0.h not to define WIN32_LEAN_AND_MEAN (note the missing NO_). WIN32_LEAN_AND_MEAN affects how many header files are pulled in when you include windows.h. If WIN32_LEAN_AND_MEAN is defined, fewer API header files are included (see windows.h to understand why).

Defining NO_WIN32_LEAN_AND_MEAN prevents the definition of WIN32_LEAN_AND_MEAN, which in turn causes more API header files to be pulled in when windows.h is included (windows.h is included by all VCL apps via sysmac.h). These extra header files usually aren't necessary, and may increase your compile times slightly. Otherwise, the extra header files shouldn't cause any problems.



Copyright © 1997-2000 by Harold Howe.
All rights reserved.