从Go语言调用C#DLL(类库)函数 [英] C# DLL (Class Library) functions called from Go Language

查看:1280
本文介绍了从Go语言调用C#DLL(类库)函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Go Lang和C#专家,

Hello Go Lang and C# gurus,

美好的一天。
我想问一下是否有人尝试过从Go程序中调用C#DLL函数(类库类型)?

Good day. I would like to ask if anyone of you have tried calling a C# DLL functions (Class Library type) from a Go program?

我做了一些初始的研究并看到以下文章:

I have made some initial research and saw these articles:

- https://github.com/golang/go/wiki/WindowsDLLs
- https://stackoverflow.com/questions/27849137/golang-call-windows-dll-functions

但是这些是从C Win32实现中创建的DLL。
我尝试搜索加载到Go程序中的C#DLL,所有这些都会告诉您在Go程序上调用它之前,您需要具有C ++ / C(Win32)包装器。

But these are DLLs created out from C Win32 implementations. I tried searching for C# DLL loaded into Go program all of them will tell you that you need to have a C++/C (Win32) wrapper before you can call it on a Go program.

此外,上面的链接将告诉您Go期待我认为的 C声明(类型为__declspec)或类似内容(例如WINAPI)。

Plus, the links above will tell you that Go is expecting a "C" declaration I think (which is of type __declspec) or the likes (e.g. WINAPI).

在这种情况下是否可以完全跳过C / C ++包装器并直接在Go中调用C#DLL函数?

Is there a way in this case where we can completely skip the C/C++ wrapper and directly call the C# DLL functions in Go?

您的

谢谢

推荐答案

选项1 将您的.NET DLL转换为COM组件,例如,然后您可以按照您的第一个链接。请记住,该选项仅适用于Windows。

Option 1 Turn your .NET DLL into a COM component like this, then you can invoke it from golang following the steps in your first link. Remember this option is for windows only.

选项2 将DLL包装在一个简单的C#控制台应用程序中,然后使用< a href = https://golang.org/pkg/os/exec/ rel = nofollow noreferrer> os / exec 。如果您的C#DLL是跨平台的,那么您的最终程序仍然是跨平台的。

Option 2 Wrap the DLL within a simple C# console application, and invoke the application in golang using os/exec. Your final program is still cross-platform if your C# DLL is cross-platform.

两个选项都需要C#的基本知识,或者创建COM组件,或者编写控制台。应用程序来调用DLL。 IMO后者更容易。

Both options need a basic knowledge of C#, either creating COM components, or writing a console application to invoke the DLL. IMO the latter is easier.

这篇关于从Go语言调用C#DLL(类库)函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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