您的位置 首页 golang

Go语言编写带回调参数的dll给C++调用

go编译dll命令行为:go build –buildmode=c-shared -o main.dll main.go

在LiteIDE下编译dll设置如下

编写go的dll文件

主文件XNGo.go

// XNGo project main.go

package main

import “fmt”

/*

#cgo CFLAGS: -I .

#include <unistd.h>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

typedef void (*callback)(int,void *,int);

extern void c_callback (int,void *,int);

extern callback _cb;

*/

import “C”

import “unsafe”

func main() {

fmt.Println(“Hello World!”)

}

func WebSocketCallBack(com int, gostring string, goint int) {

var cmsg *C.char = C.CString(gostring)

var ivalue C.int = C.int(len(gostring))

var icom C.int = C.int(com)

defer C.free(unsafe.Pointer(cmsg))

C.c_callback(icom, unsafe.Pointer(cmsg), ivalue)

}

//export register_rsucallback

func register_rsucallback(p C.callback) {

C._cb = p

return

}

//export test_callback

func test_callback() {

WebSocketCallBack(1, “hello,this is func callback.”, 123)

}

/*以下两行标识导出特别重要导出函数StartWeb*/

//StartWeb :

//export StartWeb

func StartWeb(a, b int) (int, string) {

return a + b, “hello world来个中文”

}

/*以下两行标识导出特别重要导出函数InitWebSocket*/

//InitWebSocket :

//export InitWebSocket

func InitWebSocket(name string, nPort int, p C.callback) (int, string) {

if p != nil {

C._cb = p

WebSocketCallBack(0, “call OK”, 4)

}

return 500, “hello world来个中文”

}

bridge.go文件

package main

/*

#include <stdio.h>

typedef void (*callback)(int,void *,int);

callback _cb;

void c_callback(int pclass,void * data,int datalength )

{

if(_cb!=NULL)

_cb(pclass,data,datalength);

}

*/

import “C”

编译之后生成头文件

生成的头文件XNGo.h

/* Code generated by cmd/cgo; DO NOT EDIT. */

/* package XNGo */

#line 1 “cgo-builtin-export-prolog”

#include <stddef.h> /* for ptrdiff_t below */

#ifndef GO_CGO_EXPORT_PROLOGUE_H

#define GO_CGO_EXPORT_PROLOGUE_H

#ifndef GO_CGO_GOSTRING_TYPEDEF

typedef struct { const char *p; ptrdiff_t n; } _GoString_;

#endif

#endif

/* Start of preamble from import “C” comments. */

#line 6 “main.go”

#include <unistd.h>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

typedef void (*callback)(int,void *,int);

extern void c_callback (int,void *,int);

extern callback _cb;

#line 1 “cgo-generated-wrapper”

/* 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];

#ifndef GO_CGO_GOSTRING_TYPEDEF

typedef _GoString_ GoString;

#endif

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 void register_rsucallback(callback p0);

extern void test_callback();

/* Return type for StartWeb */

struct StartWeb_return {

GoInt r0;

GoString r1;

};

/*以下两行标识导出特别重要*/

//StartWeb :

extern struct StartWeb_return StartWeb(GoInt p0, GoInt p1);

/* Return type for InitWebSocket */

struct InitWebSocket_return {

GoInt r0;

GoString r1;

};

/*以下两行标识导出特别重要*/

//InitWebSocket :

extern struct InitWebSocket_return InitWebSocket(GoString p0, GoInt p1, callback p2);

#ifdef __cplusplus

}

#endif

生成VC中需要的lib文件

编写def文件

LIBRARY

EXPORTS

StartWeb

InitWebSocket

register_rsucallback

test_callback

生成lib

有了def文件我们就可以生成lib文件了,此时我们需要使用vs2015提供的lib.exe文件(路径:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin)。

首先将lib.exe所在目录添加到环境变量的path目录下,然后在cmd下执行以下命令

生成目标64位的命令格式:

lib /def:deffile.def /machine:x64 /out:libfile.lib

生成目标32位的命令格式:

lib /def:deffile.def /machine:x86 /out:libfile.lib

此时lib文件生成就完成了

有了头文件和lib接下来你可以愉快的在VC里调用了

文章来源:智云一二三科技

文章标题:Go语言编写带回调参数的dll给C++调用

文章地址:https://www.zhihuclub.com/86399.shtml

关于作者: 智云科技

热门文章

网站地图