• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

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

    发布者: 涵韵3588 | 发布时间: 2025-8-14 07:04| 查看数: 110| 评论数: 0|帖子模式

    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. }
    复制代码

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

    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

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