1

I have a basic C++/CLI DLL in VS 2017 which defines the wrapper class described below. The project properties are set to produce CLR with .NET 4.6.1 as target.

#include "ApiManagerWrapper.h" // very basic CLI wrapper header
#include "Handler.h" // This header is an API example that includes windows.h

using namespace System;
using namespace std;

namespace CLI
{
    ApiManager::ApiManager()
        : ManagedObject(new GXP_API::ApiManager())
    {
        Console::WriteLine("Creating a new ApiManager-wrapper object!");
    }

}

I need to reuse a C++ file (handler.h, out of an API devkit) that includes itself windows.h, mainly to use HANDLE and CRITICAL_SECTION structures. I know this because if I remove the windows.h include in the handler.h header (and a good part of the code using the above mentionned structures), the CLI wrapper DLL compiles just fine.

The problem is that FILETIME is defined both in one of the headers included by windows.h (minwinbase.h) and in the namespace System::Runtime::InteropServices. The error is reported as E0266 in VS 2017 with a link to a thread, which recommends to qualify use of FILETIME with the intended usage scope (::FILETIME). This is however not possible in this case, as this header is from the OS. I also need to use the System namespace, as it is the basis on the CLI assembly mechanism.

How is it possible to resolve this conflict? Is there any way not to use a given part of the System namespace? Any way to instruct the compiler how to resolve the conflict?

1 Answers1

0

I've head some similar issue and my guess is that you are using using namespace in one of your header files.

Many definitions from the <windows.h> are the same to those in the c++/cli wrapper as the wrapper wraps them. This is why they are in different namespaces.

By using namespace System::Runtime::InteropServices in a header file for example, everywhere the header is imported there is now a potential collision for any definition which is also present in the windows header. And if you know have a file where both headers are imported you define "the same" entity twice and now you have your ambiguity issue.

So in all of you header files you need to cut using namespace and fully qualify all your entities. This could solve your problem.

Side note: using namespace is perfectly fine in .cpp files as there are not imported anywhere else.

jojo599k
  • 21
  • 4