您现在的位置是:网站首页> Go语言
Go语言编写复杂dll带回调参数给C++调用
- Go语言
- 2024-06-03
- 1171人已阅读
// 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 :
//export StartWeb
func StartWeb(a, b int) (int, string) {
return a + b, "hello world来个中文"
}
/*以下两行标识导出特别重要*/
//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来个中文"
}
生成的头文件
/* 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
编写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文件生成就完成了
上一篇:LiteIDE编译环境配置
下一篇:Go电子秤代码