Go语言调用dll

1、Go语言调用dll

	user32 := syscall.NewLazyDLL("imobiledevice.dll")
	MessageBoxW := user32.NewProc("idevice_event_subscribe")
	MessageBoxW.Call(uintptr(C.test), uintptr(s))

imobiledevice.dll为调用的dll名字
idevice_event_subscribe为dll内函数名。
通过MessageBoxW.Call调用函数,需要把参数转指针类型.

2、传递参数

传递整数型参数

func IntPtr(n int) uintptr {
	return uintptr(n)
}

传递字符串型参数

func StrPtr(s string) uintptr {
	return uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(s)))
}

3、实例: Go调用dll

go代码:

package main

import (
    "fmt"
    "syscall"
    "unsafe"
)

func main() {
    test := syscall.NewLazyDLL("Win32Project1.dll")
    add := test.NewProc("add")
    outRand := make([]byte, 2)
    a := 1
    b := 2
    sum, _, _ := add.Call(uintptr(a), uintptr(b), uintptr(unsafe.Pointer(&outRand[0])))
	fmt.Printf("sum:%d\noutRand:%s\n", sum, outRand)
}

uintptr(a), uintptr(b), uintptr(unsafe.Pointer(&outRand[0]))
参数1、参数2、参数3:整数,整数,char*

c++代码(.cpp)

#include "Win32Project1.h"
#include "stdafx.h"

int _stdcall add(int a, int b,char* c) {
    *c ='e' ;
    *(c + 1) = 't';
    return a + b;
}

c++代码(.h)

extern _declspec(dllexport) int _stdcall add(int a,int b);

c++代码(.def)

LIBRARY Win32Project1
EXPORTS
add @ 1

原文链接: https://www.cnblogs.com/snyn/p/16253706.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Go语言调用dll

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/189086

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午2:37
下一篇 2023年2月12日 下午2:38

相关推荐