DllClose makes the function _WinAPI_GetLastError() return zero. OK that would not be a problem and I understand why this is as it is. But if I make a DllCall without DLL handle but with DLL name, then DllOpen and DllCose is automatically called. And then _WinAPI_GetLastError() always returns zero. I now use 3.3.8.1 and can not test on newer version, but I think it's the same result.
#include <WinAPI.au3>
Global Const $tagDATA_BLOB = "DWORD cbData;ptr pbData;"
; DllClose sets last Windows error to zero
_BugTest
()
_WorkingTest
()
Func _BugTest
()
Local $bData = Binary("0x1234567890")
Local $tDataBuf = DllStructCreate($tagDATA_BLOB)
Local $tDataIn = __DataToBlob
($bData)
Local $aRet = DllCall("crypt32.dll", "BOOL", "CryptUnprotectData", "struct*", $tDataIn, "ptr*", 0, "ptr", 0, "ptr", 0, "ptr", 0, "DWORD", 0, "struct*", $tDataBuf)
If $aRet[0] = 0 Then ;of cource this fails
MsgBox(16, "BugTest", "Last Windows error: " & _WinAPI_GetLastError())
Else
_WinAPI_LocalFree(DllStructGetData($tDataBuf, "pbData"))
EndIf
_WinAPI_LocalFree(DllStructGetData($tDataIn, "pbData"))
EndFunc
Func _WorkingTest
()
Local $bData = Binary("0x1234567890")
Local $tDataBuf = DllStructCreate($tagDATA_BLOB)
Local $tDataIn = __DataToBlob
($bData)
Local $hDLL_Crypt32 = DllOpen("crypt32.dll")
Local $aRet = DllCall($hDLL_Crypt32, "BOOL", "CryptUnprotectData", "struct*", $tDataIn, "ptr*", 0, "ptr", 0, "ptr", 0, "ptr", 0, "DWORD", 0, "struct*", $tDataBuf)
;~ DllClose($hDLL_Crypt32)
If $aRet[0] = 0 Then ;of cource this fails
MsgBox(16, "WorkingTest", "Last Windows error: " & _WinAPI_GetLastError())
Else
_WinAPI_LocalFree(DllStructGetData($tDataBuf, "pbData"))
EndIf
DllClose($hDLL_Crypt32)
_WinAPI_LocalFree(DllStructGetData($tDataIn, "pbData"))
EndFunc
Func __DataToBlob
($data)
;funkey 2014.08.11th
Local $iLen, $tDataIn, $tData, $aMem
Local Const $LMEM_ZEROINIT = 0x40
Select
Case IsString($data)
$iLen = StringLen($data)
Case IsBinary($data)
$iLen = BinaryLen($data)
Case Else
Return SetError(1, 0, 0)
EndSelect
$tDataIn = DllStructCreate($tagDATA_BLOB)
$aMem = DllCall("Kernel32.dll", "handle", "LocalAlloc", "UINT", $LMEM_ZEROINIT, "UINT", $iLen)
$tData = DllStructCreate("byte[" & $iLen & "]", $aMem[0])
DllStructSetData($tData, 1, $data)
DllStructSetData($tDataIn, "cbData", $iLen)
DllStructSetData($tDataIn, "pbData", DllStructGetPtr($tData))
Return $tDataIn
EndFunc ;==>__DataToBlob