我想用 C 语言转换这段代码,效果很好
#include <windows.h>
void main() {
double* mdl_G;
void* dll = LoadLibrary("./test_win64.dll");
mdl_G = ((double*)GetProcAddress(dll, "G"));
printf("G = %.2f",*mdl_G);
}
Go 语言。我刚试过这个技巧,但没有用:
func main() {
dll, _ := syscall.LoadDLL("./test_win64.dll")
mdl_G, _ := syscall.GetProcAddress(dll.Handle, "G")
real_G := (*float64)(unsafe.Pointer(&mdl_G))
log.Print(*real_G)
}
但是不起作用。 有什么建议吗?
谢谢
错误是 & operator in unsafe pointer。 方法 GetProcAddress 已经返回一个 uintptr。
func main() {
dll, _ := syscall.LoadDLL("./test_win64.dll")
mdl_G, _ := syscall.GetProcAddress(dll.Handle, "G")
real_G := (*float64)(unsafe.Pointer(mdl_G)) // this conversion is safe.
log.Print(*real_G)
}
Go vet 将报告可能的功能滥用。但是,这是正确的:allow of uintptr to unsafe.Pointer when it points to non-Go memory #58625