1. 實作方式與語法形式
基本方式:將 Go 程式編譯成 DLL 供 C# 呼叫,
1.1 Go代碼
注意:代碼中 export 的注釋是定義的入口描述不能省略
package main
import "C"
import "fmt"
func main() {
fmt.Println(Test())
}
var _count = 0
//Test :
//export Test
func Test() int {
_count++
return _count
}
在 LiteIDE 中將編譯配置的 BUILDARGS 自定義值為 --buildmode=c-shared -o Test.dll,從而形成以下編譯陳述句,
go build --buildmode=c-shared -o Test.dll
1.2 C# 代碼
[DllImport("Test.dll", EntryPoint = "Test")]
extern static int Test();
2. Windows 下編譯依賴的環境
生成 DLL 依賴于 gcc,沒有 gcc 環境時,會報以下錯誤:
"gcc": executable file not found in %PATH%
GCC下載:Windows 64位版本 || Windows 32位版本,也可以從從云盤下載,
下載之后,解壓后確保 gcc 命令在搜索路徑(Path)中,
更多資訊可參考:https://www.cnblogs.com/ghj1976/p/3540257.html
3. 作業系統 64 位與 32 的編譯
在 LiteIDE 中,可以通過配置 win32.env 和 win64.env 來指定不同的 gcc 環境路徑達到生成指定系統的 DLL 檔案,
4. c# 中作業系統 64 位與 32 的適配
在 c# 中判斷作業系統是否 64 位,可以使用以下陳述句,
bool is64 = Environment.Is64BitOperatingSystem;
為了在不同的作業系統下,加載不同的 DLL,采取以下步驟來進行組織,
(1)將 32 位的版本命名為 Test32.dll,將 64 位的版本命名為 Test64.dll
(2)定義 ITest 介面,將 DLL 將要呼叫的方法定義為介面方法
(3)分別為ITest介面實作 Test32 與 Test64 類,在類中加載相應的 DLL
(4)通過判斷作業系統型別,實體化一個 ITest 的具體實作類實體來使用
具體介面與類實作代碼如下:
public interface ITest
{
int Test();
}
public class Test32 : ITest
{
class TestDLL
{
const string DLL_NAME = "Test32.dll";
[DllImport(DLL_NAME, EntryPoint = "Test")]
public extern static int Test();
}
public int Test()
{
return TestDLL.Test();
}
}
public class Test64 : ITest
{
class TestDLL
{
const string DLL_NAME = "Test64.dll";
[DllImport(DLL_NAME, EntryPoint = "Test")]
public extern static int Test();
}
public int Test()
{
return TestDLL.Test();
}
}
實體化與呼叫:
ITest test = Environment.Is64BitOperatingSystem ? (ITest)new Test64() : (ITest)new Test32();
int result = test.Test();
5. 其它一些問題
5.1 字串轉換
- 傳入字串,C#: byte[] -> GO: *C.char
- 接收字串,GO: string -> C#: GoString struct
GO 定義示例
//Hello :
//export Hello
func Hello(name *C.char) string {
return fmt.Sprintf("hello %s", C.GoString(name))
}
C# GoString struct 定義
public struct GoString
{
public IntPtr p;
public int n;
public GoString(IntPtr n1, int n2)
{
p = n1; n = n2;
}
}
C# DllImport 宣告
[DllImport(DLL_NAME, EntryPoint = "Hello", CallingConvention = CallingConvention.Cdecl)]
public extern static GoString Hello(byte[] name);
C# GoString struct 轉 String
public string GoStringToCSharpString(GoString goString)
{
byte[] bytes = new byte[goString.n];
for (int i = 0; i < goString.n; i++)
{
bytes[i] = Marshal.ReadByte(goString.p, i);
}
string result = Encoding.UTF8.GetString(bytes);
return result;
}
C# 呼叫示例
GoString goResult = test.Hello(Encoding.UTF8.GetBytes("張三"));
Debug.WriteLine(GoStringToCSharpString(goResult));
5.2 除錯
CallingConvention
在宣告中加入 CallingConvention = CallingConvention.Cdecl 避免未知例外,
[DllImport("Test.dll", CallingConvention = CallingConvention.Cdecl)]
程式崩潰甚至例外提示都沒有,可在加載 DLL 之前:
Environment.SetEnvironmentVariable("GODEBUG", "cgocheck=0");
6. 相關參考
- GO 生成 DLL,C# 呼叫的一個完整小示例:
https://github.com/Baozisoftware/go-dll - 字串處理相關的一個問答
https://stackoverflow.com/questions/48208098/using-generated-golang-dll-to-return-string-or-c-char
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/69339.html
標籤:Go
