golang调用windows平台的dll库

文章讲述了在Golang中调用DLL时需要注意的事项,包括确保DLL和Golang编译架构一致以避免错误,使用CGO进行类型转换,尤其是字符串处理,以及参数转换为uintptr类型。同时,文章提到了使用golang.org/x/sys/windows包的安全加载方法来防止DLL预加载攻击。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)
}

在这里插入图片描述

要在Go语言中调用MATLAB生成的DLL文件,你可以按照下面步骤进行操作: ### 步骤一:准备MATLAB DLL 1. **创建MATLAB函数**:首先,在MATLAB环境中编写你需要的功能,并测试其正确性。 2. **打包成共享(.dll)**:利用MATLAB Compiler SDK将上述功能封装进一个动态链接(DLL)内。这通常涉及到设置输入输出参数、选择目标平台(x64/x86),以及指定所需的依赖项等配置工作。完成后会得到`.h`头文件及相应的Windows下的.dll或Linux下的.so文件。 ### 步骤二:在Go项目里引入并加载该DLL 为了能够在Go程序中引用到这个外部,可以借助cgo工具来完成这一任务。需要注意的是CGO_ENABLED环境变量应该设为1(默认值),并且保证系统安装了TDM-GCC MinGW-w64或者MSYS2这样的GCC编译套件用于处理C/C++代码片段. ```go /* #cgo CFLAGS: -I${SRCDIR}/path/to/matlab/include/ #cgo LDFLAGS: -L${SRCDIR}/path/to/matlab/lib/ -lYourMatlabLibNameWithoutDllExtension #include "mclmcrrt.h" #include "YourMatlabLibHeaderFile.h" // 这是你从matlab compiler sdk获得的.h 文件路径 */ import "C" func init() { // 初始化运行时支持(如果需要的话) } ``` ### 步骤三:调用MATLAB提供的APIs 一旦成功导入之后就可以像平常那样通过导出符号名直接访问那些由MATLAB自动生成的C风格接口啦! 例如假设有一个求解线性方程组Ax=b的相关接口,则可以在Go端如此编码: ```go package main // ... (上面提到过的 import 和其他必要的声明) func SolveLinearEquation(A *[]float64, B *[]float64) (*C.struct_MWArray_double_T , error){ var result C.struct_MWArray_double_T status := C.mlxDllFunctionCall(/*...*/) if int(status)!=0{ return nil,fmt.Errorf("Failed to solve linear equation") } return &result,nil } func main(){ /* 示例数据 */ A:=[]float64{9.,7.,5., 3.,5.,7., 2.,5.,8.} b := []float64{-8,-9,-4} res,err := SolveLinearEquation(&A,&b) if err!=nil{ log.Fatal(err.Error()) } fmt.Printf("Solution x = %v\n",res.Data) } ``` 请注意以上只是一个简化版的例子,实际应用时还需根据具体情况调整参数传递方式如矩阵维度说明等等;同时也要考虑内存管理的问题防止出现资源泄漏的情况发生。 此外,由于跨语言互操作性的复杂度较高,强烈建议详细阅读官方文档以便更好地理解和掌握其中涉及的知识点和技术细节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值