duanci3845
2014-09-16 03:44
浏览 243
已采纳

遇到syscall.Syscall和WinAPI的麻烦

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
}

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用 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)
返回int16(ret),callErr 
} 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumeng1143 2014-09-16 04:15
    已采纳

    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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题