欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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庫內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家! 

相關文章

  • Go?并發(fā)編程協(xié)程及調度機制詳情

    Go?并發(fā)編程協(xié)程及調度機制詳情

    這篇文章主要介紹了Go并發(fā)編程協(xié)程及調度機制詳情,協(xié)程是Go語言最大的特色之一,goroutine的實現其實是通過協(xié)程,更多相關內容需要的朋友可以參考一下
    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性能基準測試可以幫助開發(fā)人員比較不同的實現方式對性能的影響,以便優(yōu)化程序,本文就來講解一下如何使用Golang的性能基準測試功能,需要的朋友可以參考下
    2023-06-06
  • Golang利用Template模板動態(tài)生成文本

    Golang利用Template模板動態(tài)生成文本

    Go語言中的Go?Template是一種用于生成文本輸出的簡單而強大的模板引擎,它提供了一種靈活的方式來生成各種格式的文本,下面我們就來看看具體如何使用Template實現動態(tài)文本生成吧
    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實現一個內存緩存系統(tǒng)的示例代碼

    go實現一個內存緩存系統(tǒng)的示例代碼

    本文主要介紹了go實現一個內存緩存系統(tǒng)的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-10-10

最新評論