我遇到了syscall.Syscall和WinAPI的问题。

huangapple go评论76阅读模式
英文:

Having trouble with syscall.Syscall and the WinAPI

问题

我正在尝试使用Window API中的VkKeyScan,但是每次调用该函数时程序都会崩溃。我导入并以这种方式使用其他Window API函数时没有任何问题。我的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
}
英文:

I'm trying to use VkKeyScan from the Window's API, however the program crashes whenever that function is called. I've had no problems with other Window's API functions I've imported and used in this way. Is there something wrong with my syscall.Syscall call?

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
}

答案1

得分: 5

VkScanKey在C语言中起作用,因为它被大致定义为:

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

所以VkScanKey并不是真正的符号,而是VkScanKeyW,而且只有这种形式的GetProcAddress才能使用它。如果你进行了正确的错误处理,你可能会注意到GetProcAddress失败了,而不是Syscall,这可能会让你意识到这个事实。

英文:

VkScanKey works in C because it’s #defined roughly like this:

#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.

huangapple
  • 本文由 发表于 2014年9月16日 11:44:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/25860325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定