Click here to Skip to main content
15,886,095 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm working in integrating C# ClassLibrary methods into a C++ application. I have created a C++ dynamic library for importing C# methods. After that imported methods are exported to particular C++ application using _declspec(dllexport). When I compile it throws error like this.

Errors:

XML
'Error 1 error LNK2019: unresolved external symbol "public: double __thiscall MathFuncs::MyFuncs::Display()" (Display@MyFuncs@Funcs@@QAENNN@Z) referenced in function _main Error 2 error LNK1120: 1 unresolved externals'


C# Library

C#
namespace Test
    {
        public class Class1
        {
            public int Display()
            {
                Console.WriteLine("Hai");
            }
        }
    }


C++ Wrapper Application

myFuncs.h

C++
_declspec(dllexport) double Display();

myfuncs.cpp

C++
MyFuncs::Display()
{
    Test::Class1 a1;
    a1.Display();
}

C++ console Application:

C++
int main()
{
    Funcs::MyFuncs object;
    object.Display();
}  

Please help me to calling C# wrapper class and methods in (C++ windows application and also in c++ console application).

Thanks in Advance
Posted

1 solution

"Imported methods are exported…"? My congratulations.

You C++ code looks like "regular" C++ code, unmanaged, not C++/CLI code.
There is no a simple way to export managed code to unmanaged. Some even think it's impossible, but this is not really so.

You can make your C# code COM-visible and create a COM component you can use in your unmanaged code. This way is disgusting (the troubles of using way too obsolete and boring COM), but it will work.

Another way is not trivial, but it works. Please see my past answers:
loading C# DLL in MFC[^],
How can I use a dll created in Visual Basic 2008 in Visual Basic 6.0[^],
Call Managed DLL written in C# from Unmanged Code VC++[^],
API's in .Net: Managed or UnManaged Code[^].

Overall, I would strongly discourage using managed code in the unmanaged.

I would strongly advise doing very different thing. First of all, switch to .NET in your application technology. You can easily use your legacy code written in C++, using P/Invoke. Even better, switch to C++/CLI, where you can freely mix managed and unmanaged code, or use only managed or only unmanaged. See also: https://msdn.microsoft.com/en-us/library/ms235282.aspx[^].

See also my past answer: How to play beeps in C#[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900