英文:
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 #define
d 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论