0
votes

I have C++ managed dll compiled with /clr that has some global functions.

for example

void Managed2UnManaged(DataStructures::AAA^ elem, DataStructures::CPP::AAA* copy_elem);    

when I try to use these functions in a C++ managed console application I get the following errors

error LNK2028: unresolved token (0A00048C) "void __clrcall Managed2UnManaged(class DataStructures::AAA ^,class DataStructures::CPP::AAA *)" (?Managed2UnManaged@@$$FYMXP$AAVAAA@DataStructures@NextIn@@PAV1CPP@23@@Z)

error LNK2019: unresolved external symbol "void __clrcall Managed2UnManaged(class DataStructures::AAA ^,class DataStructures::CPP::AAA *)" (?Managed2UnManaged@@$$FYMXP$AAVAAA@DataStructures@NextIn@@PAV1CPP@23@@Z)

I added to the console application a reference to the dll but it still gives the error.

I also tried adding __declspec(dllexport) but then I get

error C3395: 'Managed2UnManaged' : __declspec(dllexport) cannot be applied to a function with the __clrcall calling convention

and if I specify __stdcall I get

error C4439: 'Managed2UnManaged' : function definition with a managed type in the signature must have a __clrcall calling convention

I am using VS2012.

the h file has

class XXX {
 public:
static void Managed2UnManaged(DataStructures::RegResult^ elem,     DataStructures::CPP::RegResult* copy_elem);
};

and the cpp file

 void XXX::Managed2UnManaged(DataStructures::RegResult^ elem, DataStructures::CPP::RegResult* copy_elem)
{

}

Problem Solved. the problem was that I also needed to declare the class public ref. this caused a different problem because that the native c++ types are private. so I had to use the make_public pragma. Thanks all.

1
did this function exported by the DLL? if it is, please check the calling conventions are the same, the error message indicates the calling convention is clrcall.Matt
I tried exporting by adding __declspec(dllexport) but then I get error C3395: 'Managed2UnManaged' : __declspec(dllexport) cannot be applied to a function with the __clrcall calling conventiongerstla
_clrcall is not compatible with dllexport. Is your console application a native application, you may change it to a managed one.Matt
no the console is managed as well and compiled with /clrgerstla
You only posted the declaration of the function. Where is the code for the function actually located?Hans Passant

1 Answers

2
votes

I think that the problem may be that the CLR doesn't allow global functions.

You must create a managed class and write your function as a static member function of that class, for that function to be exported.