Skip to content

C#调用Go版DLL

Baozisoftware edited this page Mar 16, 2017 · 3 revisions

一. 前提

生成一个Go版DLL.

二. 关于Go导出类型的声明

关于test.h(32位版本)

/* Created by "go tool cgo" - DO NOT EDIT. */

/* package command-line-arguments */

/* Start of preamble from import "C" comments.  */




/* End of preamble from import "C" comments.  */


/* Start of boilerplate cgo prologue.  */
#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt32 GoInt;
typedef GoUint32 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

/*
  static assertion to make sure the file is being used on architecture
  at least with matching size of GoInt.
*/
typedef char _check_for_32_bit_pointer_matching_GoInt[sizeof(void*)==32/8 ? 1:-1];

typedef struct { const char *p; GoInt n; } GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue.  */

#ifdef __cplusplus
extern "C" {
#endif


extern GoInt32 Sum(GoInt32 p0, GoInt32 p1);

extern void Hello();

extern GoString GetStr();

extern GoSlice GetBytes();

//extern ...

#ifdef __cplusplus
}
#endif

可以看到,Go的导出类型,除了一些整数型之外,还有结构体,比如Go里的string导出后是GoString,slice导出后是GoSlice等,这些都需要自己重新用C#代码定义一遍,例如:

struct GoString
{
    public IntPtr p;
    public int n;
}

struct GoSlice
{
    public IntPtr data;
    public int len;
    public int cap;
}

三. DLL调用

声明

关于声明,和普通的Win32 API一样,例如:

[DllImport("test", CallingConvention = CallingConvention.Cdecl)]
static extern int Sum(int a, int b);

[DllImport("test", CallingConvention = CallingConvention.Cdecl)]
static extern void Hello();

[DllImport("test", CallingConvention = CallingConvention.Cdecl)]
static extern GoString GetStr();

[DllImport("test", CallingConvention = CallingConvention.Cdecl)]
static extern GoSlice GetBytes();

注意CallingConvention = CallingConvention.Cdecl)不能被省略,否则可能会抛出异常.

关于Go的数组(切片)返回问题

例如上面的GetBytes就是返回一个数组(切片),但是直接调用就会发现程序直接崩溃,甚至不会抛出异常. 这是因为Go1.5以上版本运行时检查引发的异常退出,当返回值或者参数包含Go指针的时候,就会异常退出,因此需要设置一个GODEBUG环境变量,如:

Environment.SetEnvironmentVariable("GODEBUG", "cgocheck=0");

注意,这行代码必须在任何Go版DLL函数执行之前执行,否则无效.