Search code examples
autohotkeyuser32

Autohotkey DLLcall to CreateCaret returns 0 (The function failed) but A_LastError is also 0 (The function succeeded)


I'm trying to use a simple AHK DLLCall to the user32 api function CreateCaret. I want it to give notepad a thick text caret.

caretWidth := 10
NULL := 0
Ptr := A_PtrSize ? "Ptr" : "UInt" ; If A_PtrSize is not defined, use UInt instead.
WinHwnd := WinExist("A")
result := Dllcall("CreateCaret", Ptr, WinHwnd, Ptr, NULL, "Int", caretWidth, "Int", 0)
msgbox % "LE " A_lasterror  ; Gives LE 0
msgbox % result  ; Gives 0

This should be equivalent to the following c++ call:

CreateCaret(0x37072c, NULL, 10, 0);

Purposefully screwing up the function name makes result blank, so I believe the function is being called correctly. I just don't know why it is failing

Using similar code for GetCursorBlinkRate returns a sensible number.

I have also tried with caretWidth := 0

Similar questions about GetLastError are c++ calls, and AHK supposedly ensures that GetLastError is called in a timely enough manner to ensure A_lasterror is set correctly. Why is it showing no error code when the function apparently fails?


Solution

  • You can't change the caret of an external process.
    If you try this for example, you'll see it works just fine if you own the process:

    #Persistent
    Gui, +hwndHWND
    Gui, Add, Edit
    Gui, Show
    
    DllCall("CreateCaret", Ptr, HWND, Ptr, 0, Int, 50, Int, 50)
    DllCall("ShowCaret", Ptr, HWND)
    
    ESC::
    GuiClose:
    ExitApp