如何在C ++ Console或Windows应用程序中调用C#classLibrary方法和事件 [英] How to call C# classLibrary methods and events in C++ Console or Windows application

查看:103
本文介绍了如何在C ++ Console或Windows应用程序中调用C#classLibrary方法和事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将C#ClassLibrary方法集成到C ++应用程序中。我已经创建了一个用于导入C#方法的C ++动态库。之后,使用_declspec(dllexport)将导入的方法导出到特定的C ++应用程序。当我编译它时会抛出这样的错误。



错误:



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:

'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# Library

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





C ++包装应用

< br $> b $ b myFuncs.h





C++ Wrapper Application

myFuncs.h

_declspec(dllexport) double Display();



myfuncs.cpp




myfuncs.cpp

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



C ++控制台应用程序:




C++ console Application:

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



请帮我在(C ++ windows应用程序和c ++控制台应用程序)中调用C#包装器类和方法。



先谢谢


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

Thanks in Advance

推荐答案

导出导入的方法......?我的祝贺。



您的C ++代码看起来像常规C ++代码,非托管,而不是C ++ / CLI代码。

没有将托管代码导出为非托管的简单方法。有些人甚至认为这是不可能的,但事实并非如此。



您可以使C#代码COM可见并创建可在非托管代码中使用的COM组件。这种方式很恶心(使用方式太过时和无聊的COM的麻烦),但它会起作用。



另一种方式并非无足轻重,但它有效。请查看我过去的答案:

加载C#DLL MFC [ ^ ],

如何在Visual Basic 6.0中使用Visual Basic 2008中创建的dll [ ^ ],

使用C#从Unmanged调用托管DLL代码VC ++ [ ^ ],

API在.Net:托管代码或非托管代码 [ ^ ]。



总的来说,我强烈反对在非托管中使用托管代码。



我强烈建议做一些非常不同的事情。首先,在您的应用程序技术中切换到.NET。您可以使用P / Invoke轻松使用用C ++编写的遗留代码。更好的是,切换到C ++ / CLI,您可以在其中自由混合托管代码和非托管代码,或仅使用托管代码或仅使用非托管代码。另请参阅: https://msdn.microsoft.com/en-us/library/ms235282.aspx [ ^ ]。



另见我过去的答案:如何在C#中发出哔哔声[ ^ ]。



-SA
"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


这篇关于如何在C ++ Console或Windows应用程序中调用C#classLibrary方法和事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆