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

 更新时间:2025年03月03日 09:59:31   作者:raoxiaoya  
本文主要介绍了golang调用windows平台的dll库的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1、dll 和 golang 的编译架构要一直,32位对应32位,64位对应64位,比如如果dll是32位的,而golang是64位的,可能会报错%1 is not a valid Win32 application.

2、有时候存在类型转换,需要开启CGO,比如,如果dll中的函数返回一个字符串,那么返回值是一个字符串指针,还需要将返回值转换成go字符串

import "C"

str := C.GoString((*C.Char)(unsafe.Pointer(ret)))

3、传入dll函数的参数被要求是 uintptr 类型,因此需要做转换。

func IntPtr(n int) uintptr {
	return uintptr(n)
}
func Int2IntPtr(n int) uintptr {
	return uintptr(unsafe.Pointer(&n))
}
func IntPtr2Ptr(n *int) uintptr {
	return uintptr(unsafe.Pointer(n))
}
func BytePtr(s []byte) uintptr {
	return uintptr(unsafe.Pointer(&s[0]))
}
func StrPtr(s string) uintptr {
    return uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(s)))
}
package main

import (
	"fmt"
	"syscall"
)

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

func main() {
	f1()
	f2()
	f3()
	f4()
}

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

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

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

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

	p, _ := syscall.GetProcAddress(handle, f)
	r1, r2, errorNo := syscall.Syscall(p, 2, uintptr(syscall.Stdout), uintptr(6), 0)
	fmt.Println(r1, r2, errorNo)
	// 恢复到白色
	syscall.Syscall(p, 2, uintptr(syscall.Stdout), uintptr(7), 0)
}

在这里插入图片描述

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

相关文章

  • Go 并发编程协程及调度机制详情

    Go 并发编程协程及调度机制详情

    这篇文章主要介绍了Go并发编程协程及调度机制详情,协程是Go语言最大的特色之一,goroutine的实现其实是通过协程,更多相关内容需要的朋友可以参考一下
    2022-09-09
  • golang 实现 pdf 转高清晰度 jpeg的处理方法

    golang 实现 pdf 转高清晰度 jpeg的处理方法

    这篇文章主要介绍了golang 实现 pdf 转高清晰度 jpeg,下面主要介绍Golang 代码使用方法及Golang PDF转JPEG的详细代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-10-10
  • Go语言使用组合的思想实现继承

    Go语言使用组合的思想实现继承

    这篇文章主要为大家详细介绍了在 Go 里面如何使用组合的思想实现“继承”,文中的示例代码讲解详细,对我们学习Go语言有一定的帮助,需要的可以了解一下
    2022-12-12
  • Golang 性能基准测试(benchmark)详解

    Golang 性能基准测试(benchmark)详解

    Golang性能基准测试可以帮助开发人员比较不同的实现方式对性能的影响,以便优化程序,本文就来讲解一下如何使用Golang的性能基准测试功能,需要的朋友可以参考下
    2023-06-06
  • Golang利用Template模板动态生成文本

    Golang利用Template模板动态生成文本

    Go语言中的Go Template是一种用于生成文本输出的简单而强大的模板引擎,它提供了一种灵活的方式来生成各种格式的文本,下面我们就来看看具体如何使用Template实现动态文本生成吧
    2023-09-09
  • go语言实现同步操作项目示例

    go语言实现同步操作项目示例

    本文主要介绍了go语言实现同步操作项目示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • golang关闭chan通道的方法示例

    golang关闭chan通道的方法示例

    在go语言中,通道(channel)是一个非常重要的概念,通道提供了一种在不同 goroutine 之间安全地传递数据的方式,在本文中,我们将讨论如何关闭通道以及在关闭通道时需要考虑的事项,需要的朋友可以参考下
    2024-02-02
  • Go 语言入门学习之正则表达式

    Go 语言入门学习之正则表达式

    这篇文章主要介绍了Go 语言入门学习之正则表达式,文章基于GO语言的相关资料展开详细内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-04-04
  • 基于Go编写一个可视化Navicat本地密码解析器

    基于Go编写一个可视化Navicat本地密码解析器

    这篇文章主要给大家介绍了基于Go编写一个可视化Navicat本地密码解析器的方法,文中有详细的代码示例和图文介绍,有需要的朋友可以参考阅读本文
    2023-08-08
  • go实现一个内存缓存系统的示例代码

    go实现一个内存缓存系统的示例代码

    本文主要介绍了go实现一个内存缓存系统的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-10-10

最新评论