Jump to content

VarSetCapacity equivalent and pointer in DllCall


vince100
 Share

Recommended Posts

I am trying to convert the following piece of code from AHK to autoit.

I've read relevant parts of documentation of both AHK and autoit, but as a newbie I am still very confused.

I am thinking to use Binary() or DllCreateStruct, but couldn't figure out the exact syntax.

Would someone please give a hint?

VarSetCapacity( AndMask, 32*4, 0xFF )
        VarSetCapacity( XorMask, 32*4, 0 )

         b%A_Index% = DllCall("CreateCursor","uint",0, "int",0, "int",0
                , "int",32, "int",32, "uint",&AndMask, "uint",&XorMask )
Link to comment
Share on other sites

Hi, yep DLLStructs is what you use to put the Byte data array for AND/XOR mask, then when you make the DLLCall to CreateCursor you pass a pointer to each struct.

Here's where I found a MSDN example I translated to AutoIt.

Hopefully you can get a beter understanding from my crude translation.

#include <WinAPI.au3>

Global $hInstance, $xHotSpot, $yHotSpot, $nWidth, $nHeight, $tANDPlane, $tXORPlane, $hOldCursor, $hCursor_Example
Global $hGui

$hInstance = _WinAPI_GetModuleHandle(0) ; handle to current instance
$xHotSpot = 19 ; horizontal position of hot spot
$yHotSpot = 2 ; vertical position of hot spot
$nWidth = 32 ; cursor width
$nHeight = 32 ; cursor height
$tANDPlane = DllStructCreate("byte AND[128]"); Struct for AND mask (32*4 = 128)
$tXORPlane = DllStructCreate("byte XOR[128]") ; Struct for XOR mask (32*4 = 128)

_Example_ANDMaskToStruct($tANDPlane)
_Example_XORMaskToStruct($tXORPlane)

$hCursor_Example = _WinAPI_CreateCursor($hInstance, $xHotSpot, $yHotSpot, $nWidth, $nHeight, $tANDPlane, $tXORPlane)

$hGui = GUICreate("Example Create Cursor")
GUISetCursor(16, 1, $hGui)
GUISetState(@SW_SHOW, $hGui)

$hOldCursor = _WinAPI_SetCursor($hCursor_Example)

While GUIGetMsg() <> -3
    Sleep(10)
    _WinAPI_SetCursor($hCursor_Example)
WEnd

_WinAPI_SetCursor($hOldCursor)
_WINAPI_DestroyCursor($hCursor_Example)

Func _WinAPI_CreateCursor($hInstance, $xHotSpot, $yHotSpot, $nWidth, $nHeight, $tANDPlane, $tXORPlane)
    Local $aResult
    $aResult = DllCall("User32.dll", "handle", "CreateCursor", "handle", $hInstance, "int", $xHotSpot, "int", $yHotSpot, "int", $nWidth, "int", $nHeight, "ptr", DllStructGetPtr($tANDPlane), "ptr", DllStructGetPtr($tXORPlane))
    If @error Or Not $aResult[0] Then Return SetError(1, 0, 0)
    Return $aResult[0]
EndFunc   ;==>_WinAPI_CreateCursor

Func _WINAPI_DestroyCursor($hCursor)
    Local $aResult
    $aResult = DllCall("User32.dll", "int", "DestroyCursor", "handle", $hCursor)
    If @error Or Not $aResult[0] Then Return SetError(1, 0, 0)
    Return $aResult[0]
EndFunc

Func _Example_ANDMaskToStruct(ByRef $tANDPlane)
    Local $sANDMask, $aANDMask
    $sANDMask &= "0xFF,0xFC,0x3F,0xFF," ; line 1
    $sANDMask &= "0xFF,0xC0,0x1F,0xFF," ; line 2
    $sANDMask &= "0xFF,0x00,0x3F,0xFF," ; line 3
    $sANDMask &= "0xFE,0x00,0xFF,0xFF," ; line 4
    $sANDMask &= "0xF7,0x01,0xFF,0xFF," ; line 5
    $sANDMask &= "0xF0,0x03,0xFF,0xFF," ; line 6
    $sANDMask &= "0xF0,0x03,0xFF,0xFF," ; line 7
    $sANDMask &= "0xE0,0x07,0xFF,0xFF," ; line 8
    $sANDMask &= "0xC0,0x07,0xFF,0xFF," ; line 9
    $sANDMask &= "0xC0,0x0F,0xFF,0xFF," ; line 10
    $sANDMask &= "0x80,0x0F,0xFF,0xFF," ; line 11
    $sANDMask &= "0x80,0x0F,0xFF,0xFF," ; line 12
    $sANDMask &= "0x80,0x07,0xFF,0xFF," ; line 13
    $sANDMask &= "0x00,0x07,0xFF,0xFF," ; line 14
    $sANDMask &= "0x00,0x03,0xFF,0xFF," ; line 15
    $sANDMask &= "0x00,0x00,0xFF,0xFF," ; line 16
    $sANDMask &= "0x00,0x00,0x7F,0xFF," ; line 17
    $sANDMask &= "0x00,0x00,0x1F,0xFF," ; line 18
    $sANDMask &= "0x00,0x00,0x0F,0xFF," ; line 19
    $sANDMask &= "0x80,0x00,0x0F,0xFF," ; line 20
    $sANDMask &= "0x80,0x00,0x07,0xFF," ; line 21
    $sANDMask &= "0x80,0x00,0x07,0xFF," ; line 22
    $sANDMask &= "0xC0,0x00,0x07,0xFF," ; line 23
    $sANDMask &= "0xC0,0x00,0x0F,0xFF," ; line 24
    $sANDMask &= "0xE0,0x00,0x0F,0xFF," ; line 25
    $sANDMask &= "0xF0,0x00,0x1F,0xFF," ; line 26
    $sANDMask &= "0xF0,0x00,0x1F,0xFF," ; line 27
    $sANDMask &= "0xF8,0x00,0x3F,0xFF," ; line 28
    $sANDMask &= "0xFE,0x00,0x7F,0xFF," ; line 29
    $sANDMask &= "0xFF,0x00,0xFF,0xFF," ; line 30
    $sANDMask &= "0xFF,0xC3,0xFF,0xFF," ; line 31
    $sANDMask &= "0xFF,0xFF,0xFF,0xFF " ; line 32
    $aANDMask = StringSplit($sANDMask, ",")
    For $i = 1 To $aANDMask[0]
        DllStructSetData($tANDPlane, "AND", $aANDMask[$i], $i)
    Next
EndFunc   ;==>_Example_ANDMaskToStruct

Func _Example_XORMaskToStruct(ByRef $tXORPlane)
    Local $sXORMask, $aXORMask
    $sXORMask &= "0x00,0x00,0x00,0x00," ; line 1
    $sXORMask &= "0x00,0x03,0xC0,0x00," ; line 2
    $sXORMask &= "0x00,0x3F,0x00,0x00," ; line 3
    $sXORMask &= "0x00,0xFE,0x00,0x00," ; line 4
    $sXORMask &= "0x0E,0xFC,0x00,0x00," ; line 5
    $sXORMask &= "0x07,0xF8,0x00,0x00," ; line 6
    $sXORMask &= "0x07,0xF8,0x00,0x00," ; line 7
    $sXORMask &= "0x0F,0xF0,0x00,0x00," ; line 8
    $sXORMask &= "0x1F,0xF0,0x00,0x00," ; line 9
    $sXORMask &= "0x1F,0xE0,0x00,0x00," ; line 10
    $sXORMask &= "0x3F,0xE0,0x00,0x00," ; line 11
    $sXORMask &= "0x3F,0xE0,0x00,0x00," ; line 12
    $sXORMask &= "0x3F,0xF0,0x00,0x00," ; line 13
    $sXORMask &= "0x7F,0xF0,0x00,0x00," ; line 14
    $sXORMask &= "0x7F,0xF8,0x00,0x00," ; line 15
    $sXORMask &= "0x7F,0xFC,0x00,0x00," ; line 16
    $sXORMask &= "0x7F,0xFF,0x00,0x00," ; line 17
    $sXORMask &= "0x7F,0xFF,0x80,0x00," ; line 18
    $sXORMask &= "0x7F,0xFF,0xE0,0x00," ; line 19
    $sXORMask &= "0x3F,0xFF,0xE0,0x00," ; line 20
    $sXORMask &= "0x3F,0xC7,0xF0,0x00," ; line 21
    $sXORMask &= "0x3F,0x83,0xF0,0x00," ; line 22
    $sXORMask &= "0x1F,0x83,0xF0,0x00," ; line 23
    $sXORMask &= "0x1F,0x83,0xE0,0x00," ; line 24
    $sXORMask &= "0x0F,0xC7,0xE0,0x00," ; line 25
    $sXORMask &= "0x07,0xFF,0xC0,0x00," ; line 26
    $sXORMask &= "0x07,0xFF,0xC0,0x00," ; line 27
    $sXORMask &= "0x01,0xFF,0x80,0x00," ; line 28
    $sXORMask &= "0x00,0xFF,0x00,0x00," ; line 29
    $sXORMask &= "0x00,0x3C,0x00,0x00," ; line 30
    $sXORMask &= "0x00,0x00,0x00,0x00," ; line 31
    $sXORMask &= "0x00,0x00,0x00,0x00" ; line 32
    $aXORMask = StringSplit($sXORMask, ",")
    For $i = 1 To $aXORMask[0]
        DllStructSetData($tXORPlane, "XOR", $aXORMask[$i], $i)
    Next
EndFunc   ;==>_Example_XORMaskToStruct

Cheers

Edited by smashly
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...