设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间00 现在时间00 缓存数据 对自己狠一点,逼自己努力,再过几年你将会感谢今天发狠的自己、恨透今天懒惰自卑的自己。晚安!

对自己狠一点,逼自己努力,再过几年你将会感谢今天发狠的自己、恨透今天懒惰自卑的自己。晚安!

查看: 894|回复: 0

golang调用windows平台的dll库的方法实现

[复制链接]

  离线 

TA的专栏

  • 打卡等级:热心大叔
  • 打卡总天数:205
  • 打卡月天数:0
  • 打卡总奖励:3134
  • 最近打卡:2023-08-27 08:03:57
等级头衔

等級:晓枫资讯-上等兵

在线时间
0 小时

积分成就
威望
0
贡献
405
主题
373
精华
0
金钱
4319
积分
810
注册时间
2022-12-24
最后登录
2025-5-31

发表于 2025-5-31 06:53:16 | 显示全部楼层 |阅读模式
1、dll 和 golang 的编译架构要一直,32位对应32位,64位对应64位,比如如果dll是32位的,而golang是64位的,可能会报错
  1. %1 is not a valid Win32 application.
复制代码
2、有时候存在类型转换,需要开启CGO,比如,如果dll中的函数返回一个字符串,那么返回值是一个字符串指针,还需要将返回值转换成go字符串
  1. import "C"

  2. str := C.GoString((*C.Char)(unsafe.Pointer(ret)))
复制代码
3、传入dll函数的参数被要求是 uintptr 类型,因此需要做转换。
  1. func IntPtr(n int) uintptr {
  2.         return uintptr(n)
  3. }
  4. func Int2IntPtr(n int) uintptr {
  5.         return uintptr(unsafe.Pointer(&n))
  6. }
  7. func IntPtr2Ptr(n *int) uintptr {
  8.         return uintptr(unsafe.Pointer(n))
  9. }
  10. func BytePtr(s []byte) uintptr {
  11.         return uintptr(unsafe.Pointer(&s[0]))
  12. }
  13. func StrPtr(s string) uintptr {
  14.     return uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(s)))
  15. }
复制代码
  1. package main

  2. import (
  3.         "fmt"
  4.         "syscall"
  5. )

  6. // 设置console模式下标准输出的字体前景色
  7. var f = "SetConsoleTextAttribute"

  8. func main() {
  9.         f1()
  10.         f2()
  11.         f3()
  12.         f4()
  13. }

  14. func f1() {
  15.         // A LazyDLL implements access to a single DLL.
  16.         // It will delay the load of the DLL until the first
  17.         // call to its Handle method or to one of its
  18.         // LazyProc's Addr method.
  19.         //
  20.         // LazyDLL is subject to the same DLL preloading attacks as documented
  21.         // on LoadDLL.
  22.         //
  23.         // Use LazyDLL in golang.org/x/sys/windows for a secure way to
  24.         // load system DLLs.
  25.         dll := syscall.NewLazyDLL("kernel32.dll")
  26.         p := dll.NewProc(f)
  27.         r1, r2, lastError := p.Call(uintptr(syscall.Stdout), uintptr(3))
  28.         fmt.Println(r1, r2, lastError)
  29. }

  30. func f2() {
  31.         // LoadDLL loads the named DLL file into memory.
  32.         //
  33.         // If name is not an absolute path and is not a known system DLL used by
  34.         // Go, Windows will search for the named DLL in many locations, causing
  35.         // potential DLL preloading attacks.
  36.         //
  37.         // Use LazyDLL in golang.org/x/sys/windows for a secure way to
  38.         // load system DLLs.
  39.         dll, _ := syscall.LoadDLL("kernel32.dll")
  40.         p, _ := dll.FindProc(f)
  41.         r1, r2, lastError := p.Call(uintptr(syscall.Stdout), uintptr(4))
  42.         fmt.Println(r1, r2, lastError)
  43. }

  44. func f3() {
  45.         // MustLoadDLL is like LoadDLL but panics if load operation fails.
  46.         dll := syscall.MustLoadDLL("kernel32.dll")
  47.         p := dll.MustFindProc(f)
  48.         r1, r2, lastError := p.Call(uintptr(syscall.Stdout), uintptr(5))
  49.         fmt.Println(r1, r2, lastError)
  50. }

  51. func f4() {
  52.         handle, _ := syscall.LoadLibrary("kernel32.dll")
  53.         defer syscall.FreeLibrary(handle)

  54.         p, _ := syscall.GetProcAddress(handle, f)
  55.         r1, r2, errorNo := syscall.Syscall(p, 2, uintptr(syscall.Stdout), uintptr(6), 0)
  56.         fmt.Println(r1, r2, errorNo)
  57.         // 恢复到白色
  58.         syscall.Syscall(p, 2, uintptr(syscall.Stdout), uintptr(7), 0)
  59. }
复制代码
1.png

到此这篇关于golang调用windows平台的dll库的方法实现的文章就介绍到这了,更多相关golang调用dll库内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表