C ++ / cli header file

I have a C ++ project (visual studio 2010), which consists of native code and C ++ / cli code. I cannot compile the whole project with / clr, so I just do this for the corresponding C ++ / cli files. My problem is that the header file cannot be compiled with / clr, but I want some C ++ / cli functions to be reused throughout the project and therefore defined prototypes of the method in the header file to include in each file in which I need him. Is there a solution? I tried to define some prototypes of the mixed code method in the header file, but you need to enable / clr to compile.

Here is my example:

test.h

#include <Windows.h> #include <vector> #include <string> using std::vector; using std::string; #include <msclr/marshal.h> #pragma managed using namespace msclr::interop; using namespace System; using namespace System::IO; using namespace System::Runtime::InteropServices; public ref class Test { public: int Foo(); }; 

test.cpp

 #include "Test.h" int Test::Foo() { return 4; } 

Intellisense complains about errors in Test.h, since C ++ / cli must be enabled to use #using. But I think this is negligible, and it will still compile.

Compilation fails with Linker error (sry, I have VS VS version)

 Fehler 6 error LNK1255: Fehler bei Verknüpfung aufgrund von Metadatenfehlern. Fehler 4 error LNK2022: Fehler bei Metadatenoperation (8013118D) : Duplizierte Typen (_PROPSHEETPAGEA) wurden gefunden, aber die Typenlayoutinformationen sind nicht konsistent: (0x02000198). Fehler 5 error LNK2022: Fehler bei Metadatenoperation (8013118D) : Duplizierte Typen (_PROPSHEETPAGEW) wurden gefunden, aber die Typenlayoutinformationen sind nicht konsistent: (0x020001d1). 

I definitely don’t have a duplicate Test class anywhere else, so I don’t know where the duplicate comes from. What is type information and why do they not match?

+7
source share
2 answers

I think you all missed.

You can easily compile a native project with some classes / clr. (For example: the native DLL will still function as the native DLL, however it can also be loaded in C #, and then the compiled it / clr classes can be accessed in C #.)

This is why such an option exists at the file level. (Right-click .cpp: Properties-> C \ C ++ → Common Language Runtime Support - / clr)

The problem is this:

Communication between native / managed classes, since .H files cannot be set to use / clr, they cannot be used to refer to a managed class elsewhere, including other / clr files within the same project. (i.e. you can create / clr files, but they cannot talk to each other and cannot link to them inside their own parts of the project.)

The best solution I can find is to create a file with the CLE extension. FROM#.

Create a new C # class library, add the Native DLL as a reference, then compile.

Now, in your own project, you can load the C # DLL and access it. (The managed code you are referring to can be used with native / managed code.)

It is very possible, but I cannot find an easy way to accomplish this.

Regarding the topic, there seems to be no way to refer to the / clr classes because the header files do not work when you install / clr at the file level. (i.e. the header cannot contain the / clr code if the WHOLE project is not installed in / clr.)

There must be a way to link / clr without headers, or C ++ \ CLI is simply broken, I can easily load my own code in / clr files using pragma + headers, however the opposite seems impossible, a “glue” solution.

This is what I consider this topic.


My method works, but it’s very difficult to get right, and compiling is a pain due to circular dependencies, etc.

I really hope that there is the right way to do this, I was looking, and my search brought me here ...

The real solution would be to make support for .h files / clr, then you could define your managed class in the header and be able to freely reference it using standard methods such as include \ use \ pragma, etc.

+3
source

You may be looking for

 #if __cplusplus_cli 

Documented here on MSDN

0
source

All Articles