Jump to content
supersonic

DllCall - trying to read an array of pointers

Recommended Posts

Hi -

Currently I'm playing around with Windows Credential Manager. I'm trying to access it with DllCall("advapi32.dll", ...) using the functions 'CredWriteW', 'CredReadW' and 'CredDeleteW'. All well. Another function I have to deal with is 'CredEnumerateW': https://docs.microsoft.com/en-us/windows/desktop/api/wincred/nf-wincred-credenumeratew/ .

That's the test code I have so far:

#include <Array.au3>
#include <String.au3>


Local $tCredentialsCount = DllStructCreate("DWORD;")
Local $tPointerToArrayOfPointers = DllStructCreate("PTR;")
; Local $tPointerToArrayOfPointers = DllStructCreate(_StringRepeat("PTR;", 200)) ; ???
Local $aResult = DllCall("advapi32.dll", "BOOL", "CredEnumerateW", _
        "WSTR", Null, "DWORD", 1, "DWORD", DllStructGetPtr($tCredentialsCount), "PTR", DllStructGetPtr($tPointerToArrayOfPointers))
If (Not @error) Then


    Local $iCredentialsCount = DllStructGetData($tCredentialsCount, 1)
    _ArrayDisplay($aResult, $iCredentialsCount)


    Local $hPointerToArrayOfPointers = DllStructGetData($tPointerToArrayOfPointers, 1)
    MsgBox(0, "$hPointerToArrayOfPointers", $hPointerToArrayOfPointers)


    ; Fails...
    For $i = 1 To 10 ; $iCredentialsCount
        MsgBox(0, $i & "___" & (($i * 2) - 1), DllStructGetData($tPointerToArrayOfPointers, ($i * 2) - 1))
    Next


    $tCredentialsCount = 0
    $tPointerToArrayOfPointers = 0
    DllCall("advapi32.dll", "NONE", "CredFreeW", "PTR", $hPointerToArrayOfPointers)
EndIf

The DllCall seems to function properly - I get a valid count of credentials (on my computer ~ 133) and a pointer "to array of pointers".

What is meant by "array of pointers"? :blink:

Microsoft says: Pointer to an array of pointers to credentials. The returned credential is a single allocated block. Any pointers contained within the buffer are pointers to locations within this single allocated block.

How to access these pointers... Contained within the buffer???

Any information you can provide me would be greatly appreciated.

Edited by supersonic

Share this post


Link to post
Share on other sites

hello. you need to do something like this:

#include <Array.au3>
#include <String.au3>


Local $tCredentialsCount = DllStructCreate("DWORD;")
Local $tPointerToArrayOfPointers = DllStructCreate("PTR;")
; Local $tPointerToArrayOfPointers = DllStructCreate(_StringRepeat("PTR;", 200)) ; ???
Local $aResult = DllCall("advapi32.dll", "BOOL", "CredEnumerateW", _
        "WSTR", Null, "DWORD", 1, "DWORD*", 0, "PTR*", 0)
If (Not @error) Then


    Local $iCredentialsCount = $aResult[3]
    Local $pArrayCredentials = $aResult[4]
;~     _ArrayDisplay($aResult, $iCredentialsCount)

    Local $taPointer = DllStructCreate("ptr[" & $iCredentialsCount & "]", $pArrayCredentials)

    ;show each pointer to credential
    For $i = 1 To $iCredentialsCount
        MsgBox(0, $i, DllStructGetData($taPointer, 1, $i))
    Next

    ;free
    For $i = 1 To $iCredentialsCount
        MsgBox(0, $i,)
        DllCall("advapi32.dll", "NONE", "CredFreeW", "PTR", DllStructGetData($taPointer, 1, $i))
    Next
    $taPointer = 0
EndIf
 

 

 

PD: sorry for syntax highlighting not working in my slow connection

 

Saludos

Share this post


Link to post
Share on other sites

Oh dear - an error occurs if running AutoIt in 64 bit :(

The code to reproduce:

;~ #AutoIt3Wrapper_UseX64 = Y


#include <Array.au3>
#include <String.au3>


Local $aResult = DllCall("advapi32.dll", "BOOL", "CredEnumerateW", "WSTR", Null, "DWORD", 1, "DWORD*", 0, "PTR*", 0)
If (Not @error) Then


    Local $iCredentialsCount = $aResult[3]
    Local $pArrayCredentials = $aResult[4]
    ; _ArrayDisplay($aResult, $iCredentialsCount)


    Local $taPointer = DllStructCreate("ptr[" & $iCredentialsCount & "]", $pArrayCredentials)
    For $i = 1 To $iCredentialsCount
        ; MsgBox(0, $i, DllStructGetData($taPointer, 1, $i))


        DllCall("advapi32.dll", "NONE", "CredFree", "PTR", DllStructGetData($taPointer, 1, $i))
        ConsoleWrite("___" & @error & @CRLF)
    Next
    $taPointer = 0


EndIf

In 32 bit everything works fine. In 64 bit the first 10-12 pointers are read. Then AutoIt crashes.

Any idea to resolve that issue?

P.S.: There is no function 'CredFreeA' or 'CredFreeW'... Only 'CredFree'.

Edited by supersonic

Share this post


Link to post
Share on other sites

sorry my fault. I read wrong in msdn. the correct way is free the pointer to the array.

#AutoIt3Wrapper_UseX64 = Y


#include <Array.au3>
#include <String.au3>


Local $aResult = DllCall("advapi32.dll", "BOOL", "CredEnumerateW", "WSTR", Null, "DWORD", 1, "DWORD*", 0, "PTR*", 0)
If (Not @error) Then


    Local $iCredentialsCount = $aResult[3]
    Local $pArrayCredentials = $aResult[4]
    ; _ArrayDisplay($aResult, $iCredentialsCount)


    Local $taPointer = DllStructCreate("ptr[" & $iCredentialsCount & "]", $pArrayCredentials)
    For $i = 1 To $iCredentialsCount
        MsgBox(0, $i, DllStructGetData($taPointer, 1, $i))
    Next
    DllCall("advapi32.dll", "NONE", "CredFree", "PTR", $pArrayCredentials)
    $taPointer = 0


EndIf

Saludos

Share this post


Link to post
Share on other sites

Thank you - I tried this before, too. And it worked. But 'pArrayCredentials' is a structure not a pointer!? Or am I wrong? :blink:

Furthermore... Running in 32 bit works for all entries. Do you know why?

Edited by supersonic

Share this post


Link to post
Share on other sites

1.- pArrayCredentials Its Pointer to an array of pointers to credentials

2.- just luck I think. But it's wrong. MSDN says.

Credential

Pointer to an array of pointers to credentials. The returned credential is a single allocated block. Any pointers contained within the buffer are pointers to locations within this single allocated block. The single returned buffer must be freed by calling CredFree.

Saludos

 

Share this post


Link to post
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

  • Similar Content

    • By IndianSage
      Hi,
      My AutoIt script is as folllows:
      ;use for calling function add2NosA in dll ;Local $vNo1 = 33 ;Local $vNo2 = 11 ;use for calling function sortNos in dll Local $vNo1 = [11,7,9] Local $vNo2 = [1,3,2] ; _ArrayDisplay($vNo1, "vNo1 display") ; _ArrayDisplay($vNo2, "vNo2 display") ;Local $hWnd = DllOpen("E:\CV-Dell-1\autoit3\myComObj1.dll") Local $hWnd = ObjCreate("myComObj1.clsMath") if (@error) Then MsgBox (0, "Error", "Error1 = " & @error) Exit EndIf ;function call method - DllCall with function name ;Local $aRes = DllCall($hWnd, "int", "addNosA", "int", $vNo1, "int", $vNo2) ;Local $aRes = DllCall($hWnd, "Ptr", "sortNos", "Array", $vNo1, "Array", $vNo2) ;function call method - $hWnd.<function name> ;Local $aRes = $hWnd.add2NosA($vNo1, $vNo2) ; this works fine with ObjCreate Local $aRes = $hWnd.sortNos($vNo1, $vNo2) if (@error) Then MsgBox (0, "Error", "Error2 = " & @error) DllClose($hWnd) Exit EndIf ;use appropriate msgbox ;MsgBox(0,"Result", "Result = " & $vNo1[0]) MsgBox(0,"Result", "Result = " & $aRes) _ArrayDisplay($vNo1, "vNo1 display") _ArrayDisplay($aRes , "aRes display") DllClose($hWnd) My VB.Net - ClassLibrary - Dll - COM obj is as follows - has 2 functions - add2NosA and sortNos:
      <ComClass(clsMath.ClassId, clsMath.InterfaceId, clsMath.EventsId)> Public Class clsMath Public Const ClassId As String = "3A42F85E-24C8-4BAA-91B5-AE56C4683C13" Public Const InterfaceId As String = "D99D7C79-2BA7-4A33-B7BC-9B7F19FDF828" Public Const EventsId As String = "CA128AC4-580C-4112-9EAD-8D1599E3F37A" Public Sub New() MyBase.New() End Sub Public Function add2NosA(ByVal no1 As Integer, ByVal no2 As Integer) As Integer Return (no1 + no2) End Function Public Sub sortNos(ByRef no1 As Array, ByRef no2 As Array) Array.Sort(no1) no2 = no1 End Sub End Class  Over all I tried various 8 options mentioned in the attached Excel file - with only 1 combination working.
      Overall could not make Array returned capture in AutoIt script. 
      Can someone help please?
      Thanks in  advance.
      Options-Tried-Matrix-Results.xlsx
    • By IndianSage
      I have create a function in FreeBasic like below:
      Extern "Windows-MS" Type tA f1 As Integer f2 As Integer End Type Public Function _switchOrder(ByVal no1 As Integer, ByVal no2 As Integer) As tA Export Dim result As Integer Dim taa As tA taa.f1 = no2 taa.f2 = no1 Return taa End Function End Extern Caller AutoIt code is:
      #include <MsgBoxConstants.au3> Global Const $sTag_ftdi_version_info="struct; int no1a; int no2a; endstruct" Local $aRet=DllCall("Math1.dll","Ptr","_switchOrder", "Int", 10, "Int", 30) ;MsgBox (0,"",@error & "-" & $aRet[0] & "-" & $aRet[1]& "-" & $aRet[2]) Local $t_ftdi_version_info=DllStructCreate($sTag_ftdi_version_info,$aRet[0]) MsgBox (0,"msg1=",@error & "---" & $aRet[0] & "-" & $aRet[1]& "-" & $aRet[2]) ;Local $retData1 = DllStructGetData($t_ftdi_version_info,"",1) Local $retData1 = DllStructGetData($t_ftdi_version_info,"no1a") MsgBox (0,"msg2=",@error & "--" & $retData1) ;Local $retData1 = DllStructGetData($t_ftdi_version_info,"",2) Local $retData1 = DllStructGetData($t_ftdi_version_info,"no2a") MsgBox (0,"msg2=",@error & "--" & $retData1) ;ConsoleWrite(DllStructGetData($t_ftdi_version_info,"",2) & "--" & @error) ;ConsoleWrite(DllStructGetData($t_ftdi_version_info,"no2a") & @CRLF) ;ConsoleWrite(DllStructGetData($tversion_str,1) & @CRLF) Getting error 2 for DllStructGetData or it give Close Application AutoIt popup message. 
      Certainly DllCall is not returning pointer to the Structure in $aRet[0] hence issue.
      Can someone help me fix this please?
      Thanks in advance.
       
       
    • By xYuri
      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
    • By Luigi
      A simple function to print a DllStruct on AutoIt.

       
      #include <StringConstants.au3> #include <Array.au3> Func DllStructPrint($str, $tag) Local $ret = "" If IsDllStruct($str) Then $tag = StringLower($tag) Local $arr1, $arr2 $arr1 = StringSplit($tag, ";", $STR_NOCOUNT) Local $aLabel[1] For $ii = 0 To UBound($arr1, 1) - 1 If Not ($arr1[$ii] = "struct") And Not ($arr1[$ii] = "endstruct") Then $arr2 = StringSplit($arr1[$ii], " ", $STR_NOCOUNT) If IsArray($arr2) Then _ArrayAdd($aLabel, $arr2[1]) EndIf Next For $ii = 1 To UBound($aLabel, 1) - 1 $ret &= $aLabel[$ii] & "=" & DllStructGetData($str, $aLabel[$ii]) Next ElseIf IsArray($str) Then $ret &= "[" & @LF Local $iSize = UBound($str, 1) - 1 For $ii = 0 To $iSize $ret &= " " & DllStructPrint($str[$ii], $tag) & ($ii = $iSize ? "" : ",") & @LF Next $ret &= "]" & @LF EndIf Return $ret EndFunc ;==>DllStructPrint Local $tag = "Struct;byte key;EndStruct" Local $str1 = DllStructCreate($tag) $str1.key = Random(100, 999) Local $str2 = DllStructCreate($tag) $str2.key = Random(100, 999) Local $str3 = DllStructCreate($tag) $str3.key = Random(100, 999) Local $arr[2] $arr[0] = $str2 $arr[1] = $str3 Local $var1 = DllStructPrint($str1, $tag) ConsoleWrite($var1 & @LF) Local $var2 = DllStructPrint($arr, $tag) ConsoleWrite($var2 & @LF)  
      OUTPUT: key=189 [ key=155, key=62 ]  
    • By Skysnake
      This is relevant
      From here https://stackoverflow.com/questions/3454315/is-it-possible-to-pin-a-dll-in-memory-to-prevent-unloading
      I use several UDFs on the Forum to do various things.  Those UDFs work very well.
      Effectively the UDFs are DLL wrappers, that make it possible to access DLL functions easily without the long hard slog of DLLCall() every time.
      However, I have now run into the issue that multiple UDF DLLCalls are slow. Not mind numbingly slow, but slow enough to become noticeable with a large of repeated function calls.
      So I was wondering, is it possible to "load a DLL into memory" and leave it there for the duration of my script's lifetime, avoid repeated DLL on-disk reads with a persistent in memory DLL?
      From Microsoft
      https://docs.microsoft.com/en-us/windows/desktop/dlls/about-dynamic-link-libraries
      Looks like what I want to do is: load-time dynamic linking,
      So next question, (a) how do I do this with AutoIt (b) How would this impact on standard AutoIt type DLL calls?
       
      The point is speed.  Is there a different approach?
      Or am I barking up the wrong tree?
      Skysnake
×
×
  • Create New...