duanci3845 2014-09-15 19:44
浏览 348
已采纳

遇到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
}
  • 写回答

1条回答 默认 最新

  • doumeng1143 2014-09-15 20: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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部