Jump to content

[SOLVED] DllCall return 0 with Error 5


xYuri
 Share

Recommended Posts

This simple dllcall gives me error 5, access denied,

Func _WinAPI_VkKeyScan($__key)
    _WinAPI_SetLastError(0)
    $res = DllCall('User32.dll', 'SHORT', 'VkKeyScan', 'CHAR', $__key)
    _xConsole('res: '&$res)
    $_LastErr = _WinAPI_GetLastError()
    If $_LastErr <> 0 Then _xConsole('Err: {' & $_LastErr & '}> ' & _WinAPI_GetLastErrorMessage())
    Return $res
EndFunc

Am i doing something wrong?

Also tried VkKeyScanA and W

Edit:

I want to send `:` via PostMessage() WM_KEYDOWN

Edited by xYuri
Missing Info
Link to comment
Share on other sites

@KaFu Thank you very much for your answer,

Sorry that i forgot to include this in my question,

But it still not doing what i really wanted, in first place i used `VkKeyScan` to get the shift+ code for `;` which makes it `:`, 

Now even after using ProgAndy's code it still send `;` , which i can get from https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes,

 

Also `GetLastError()` still gives error code 5, which is weird cuz it give a VKCode to a key.

Screenshot_1.png

Link to comment
Share on other sites

dllcall returns an array.  You should test @error before using winapi* error function.

Provide full code not just the func.  You must send message to the right control.

Link to comment
Share on other sites

@Nine Yes i am aware of that, and i am sending to the right control as it show ;;;; in notepad i think?

Anyway here is the code,

Func _WinAPI_VkKeyScanW($__key)
    _WinAPI_SetLastError(0)
    $res = DllCall('User32.dll', 'SHORT', 'VkKeyScanW', 'USHORT', AscW($__key))
    _xConsole('res: ' & $res[0])
    $_LastErr = _WinAPI_GetLastError()
    If @error Then _xConsole('Err: {' & @error & '}> ' & _WinAPI_GetLastErrorMessage())
    Return SetExtended(BitShift($res[0], 8), BitAnd($res[0], 0xFF))
EndFunc   ;==>_WinAPI_VkKeyScan

$h = WinGetHandle('[CLASS:Notepad]')
MsgBox(0, '', $h)
$C_H = ControlGetHandle($h, '', '[CLASS:Edit; INSTANCE:1]')
_WinAPI_PostMessageW($C_H, $WM_KeyDown, _winapi_VkKeyScanW(':'), 2)

 

Result:

Screenshot_1.png

Edited by xYuri
Link to comment
Share on other sites

VkKey are not actual character.  They are assign to physical key.  So this is why you will always get lowercase value.

One way to solve your problem is to use WM_CHAR like this :

_SendMessage ($C_H, $WM_CHAR, Asc(":"))

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...