c# 调用Go 动态库

  • 2020 年 11 月 24 日
  • 笔记
[StructLayout(LayoutKind.Sequential)]
        public struct GoMem
        {
            public IntPtr data;
            public UInt64 len;
            public UInt64 cap;
        }

        public string GetPosition()
        {
            double[] iresult = new double[6] { 0, 0, 0, 0, 0, 0 };


            GCHandle handle4 = GCHandle.Alloc(iresult, GCHandleType.Pinned);
            GoMem goMem4 = new GoMem();
            IntPtr pointer4 = handle4.AddrOfPinnedObject();
            goMem4.cap = (ulong)iresult.Count();
            goMem4.len = (ulong)iresult.Count();
            goMem4.data = pointer4;

            CTToRobotGo(goMem4);
            Marshal.Copy(goMem4.data, iresult, 0, iresult.Length);
            return string.Join(",", iresult);

        }

        [DllImport("DockingCal.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern void CTToRobotGo(GoMem robotpos);

Go代码里面用到了片段类型,转换到c# 就是结构体

typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

VS 编译的时候记得用X64编译