From f1339eeb125d26f483975f80001e5a8ca0f450af Mon Sep 17 00:00:00 2001 From: ZGGSONG Date: Fri, 20 May 2022 20:59:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20'README.md'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/README.md b/README.md index 125734b..d6d9d0a 100644 --- a/README.md +++ b/README.md @@ -8,3 +8,65 @@ - MordenWPF美化控件 - Hardcodet实现WPF系统托盘效果 - 代码实现图标闪烁 + + +## CSharp using Go + +```Go +package main + +import ( + "C" + "zggsong.cn/scadatool/server" +) + +func main() { + run() +} + +//export run +func run() string { + // 启动服务 + server.StartServer() + return "success" +} +``` + +```C# +/// +/// 引入Go项目库 +/// +/// +[DllImport("scadatool_go.dll", EntryPoint = "run")] +extern static GoString run(); + +#本项目中的代码示例 +string result = GoCSharpHelper.Instance().GoStringToCSharpString(run()); + +``` + +**Go字符串乱码问题** +```C# +/// +/// 转换Go string类型为C# String +/// +public struct GoString +{ + public IntPtr p; + public int n; + public GoString(IntPtr n1, int n2) + { + p = n1; n = n2; + } +} +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; +} +``` \ No newline at end of file