1

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!

casablanca
  • 69,683
  • 7
  • 133
  • 150
Daniel
  • 11
  • 1
  • Don't you have to have CLR support to call into managed assemblies. – rerun Oct 22 '10 at 05:33
  • @rerun: I have support to CLR in ProjectA (.lib), which I use in ProjectB. So, ProjectA has CLR support, ProjectB does not. ProjectA is still a Win32 project, just that I have added CLR support to it. – Daniel Oct 22 '10 at 05:45

1 Answers1

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks for the info. I am able to call Managed functions implemented in ProjectA from ProjectB - both are Win32 projects as explained above. However, I have now hit another unusual(rather usual) situation (for me) though. I have both static and non-static functions in ProjectA::ClassA. I have a ProjectB::ClassB object using which I want to call these ClassA functions. I am ABLE to call the non-static/instance methods in ProjectA::ClassA using ClassB object. – Daniel Oct 22 '10 at 06:45
  • However I am UNABLE to call the static functions either using the class names of either the ClassA:: or ClassB:: (class ClassB : public ClassA is how I am sub-classing). I am new to this interop betwen "Win32+CLR" and "Win32". Any pointers??? Am I blatantly missing something? – Daniel Oct 22 '10 at 06:45
  • Probably, static functions are easier. I cannot possibly guess what you are missing, you didn't even include an error message. Update your question. – Hans Passant Oct 22 '10 at 07:07
  • Sorry, the error was error LNK2001: unresolved external symbol "public: static void __cdecl ClassA::StaticFuncA(void)" (?StaticFuncA@ClassA@@SAXXZ) Helper.obj – Daniel Oct 22 '10 at 07:13