
离线 TA的专栏
- 打卡等级:热心大叔
- 打卡总天数:205
- 打卡月天数:0
- 打卡总奖励:3134
- 最近打卡:2023-08-27 08:03:57
|
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库内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:  进行删除处理。
4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
|