Calling a function from a Win32 .lib project with

2019-08-07 02:27发布

问题:

I want to know how do I exactly call a function from ProjectA::ClassA::FuncA() that is a Win32 .lib with /clr from a ProjectB::ClassB::FuncB() that does not have clr support and is a pure Win32 project. Both these projects are under same solution.

First, this is what I have tried: 1. Created the ProjectA with the .lib 2. Added the .lib path to ProjectB properties (in Linker:Input:Add.Dependencies) 3. I added the .h for the .lib created by ProjectA in ProjectB 4. Created the object for ProjectA::ClassA in ProjectB::ClassB and tried to call the FuncA().

I get the following error:

Error 1 error LNK2019: unresolved external symbol "public: static void __cdecl ClassA::FuncA(void)" (?FuncA@ClassA@@SAXXZ) referenced in function "public: static void __cdecl ClassB::FuncB(void)" (?FuncB@ClassB@@SAXXZ) Helper.obj

I am using third-party .lib s in ProjectB successfully. I follow the same process but it fails; the only difference being ProjectA() is with CLR support.

Am I missing something? Please enlighten me ;-)

Thanks!

回答1:

Native code can call managed code but that needs to be done in a source code file that's compiled with /clr. You need a little adapter class that's native (no "ref") in ProjectB. If these are instance methods then you'll need gcroot<> in the adapter to store a reference to the managed class.