简体   繁体   English

遇到syscall.Syscall和WinAPI的麻烦

[英]Having trouble with syscall.Syscall and the WinAPI

I'm trying to use VkKeyScan from the Window's API, however the program crashes whenever that function is called. 我正在尝试使用Windows API的VkKeyScan ,但是只要调用该函数,程序就会崩溃。 I've had no problems with other Window's API functions I've imported and used in this way. 我以这种方式导入和使用的其他Window API函数没有问题。 Is there something wrong with my syscall.Syscall call? 我的syscall.Syscall调用有问题吗?

var (
    user32, _ = syscall.LoadLibrary("user32.dll")
    vkKeyScan, _ = syscall.GetProcAddress(user32, "VkKeyScan")
)

func VkKeyScan(char byte) (int16, syscall.Errno) {
    var nargs uintptr = 1
    ret, _, callErr := syscall.Syscall(uintptr(vkKeyScan), nargs, uintptr(char), 0, 0)
    return int16(ret), callErr
}

VkScanKey works in C because it's #define d roughly like this: VkScanKey可在C中工作,因为它的#define大致如下所示:

#ifdef UNICODE
#   define VkScanKey VkScanKeyW
#else
#   define VkScanKey VkScanKeyA
#endif

So VkScanKey isn't the real symbol— VkScanKeyW is, and that's the only form GetProcAddress will take it in. If you had been doing proper error handling you might have noticed that GetProcAddress was failing rather than Syscall , which might have tipped you off to this fact. 所以VkScanKey不是真正的VkScanKeyW是,并且这是GetProcAddress接受的唯一形式。如果您一直在进行适当的错误处理,您可能会注意到GetProcAddress发生了故障,而不是Syscall ,这可能提示您这个事实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM