C# DLL(类库)函数被Go语言调用

huangapple go评论102阅读模式
英文:

C# DLL (Class Library) functions called from Go Language

问题

你好,Go Lang 和 C# 高手们,

祝你有个愉快的一天。
我想问一下,你们中是否有人尝试过从 Go 程序调用 C# DLL 函数(类库类型)?

我进行了一些初步的研究,并看到了以下文章:

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

此外,上面的链接会告诉你,Go 期望一个 "C" 声明(我认为是 __declspec 类型)或类似的声明(例如 WINAPI)。

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

非常感谢你们的启发性想法和建议!

谢谢!

英文:

Hello Go Lang and C# gurus,

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

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.

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).

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?

Your enlightening ideas and inputs will be greatly appreciated =)!

Thanks

答案1

得分: 4

选项1 将你的.NET DLL转换为COM组件,可以参考这个链接,然后你可以按照你提供的第一个链接中的步骤从golang中调用它。请记住,这个选项只适用于Windows系统。

选项2 在一个简单的C#控制台应用程序中包装DLL,然后使用golang中的os/exec调用该应用程序。如果你的C# DLL是跨平台的,那么你的最终程序仍然是跨平台的。

这两个选项都需要基本的C#知识,无论是创建COM组件还是编写一个调用DLL的控制台应用程序。在我看来,后者更容易一些。

英文:

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.

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.

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.

huangapple
  • 本文由 发表于 2017年6月9日 08:01:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/44447318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定