• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
思念以南
博客园    首页    新随笔    联系   管理    订阅  订阅

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
posted @ 2022-05-10 15:11  思念以南  阅读(2949)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3