Ticket #1260: WinAPI_GetLastErrorMessageW.au3

File WinAPI_GetLastErrorMessageW.au3, 1.0 KB (added by danielkza, 15 years ago)

WinAPI_GetLastErrorMessageW

Line 
1#include <WinAPI.au3>
2
3;Global Const $__WINAPCONSTANT_FORMAT_MESSAGE_FROM_SYSTEM = 0x1000
4Global Const $__WINAPCONSTANT_FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x100
5
6Func _WinAPI_GetLastErrorMessageW()
7    Local Const $iFlags = BitOR($__WINAPCONSTANT_FORMAT_MESSAGE_ALLOCATE_BUFFER, $__WINAPCONSTANT_FORMAT_MESSAGE_FROM_SYSTEM)
8    Local $tBufferPtr = DllStructCreate("ptr")
9
10    Local $aResult = DllCall("Kernel32.dll", "int", "FormatMessageW", _
11        "dword", $iFlags, "ptr", 0, "dword", _WinAPI_GetLastError(), "dword", 0, _
12        "ptr", DllStructGetPtr($tBufferPtr), "dword", 0, "ptr", 0)
13    If @error Then Return SetError(@error, 0, "")
14
15    Local $charCount = $aResult[0], $Text = ""
16    Local $tBuffer, $pBuffer = DllStructGetData($tBufferPtr, 1)
17    If $pBuffer Then
18        If $charCount > 0 Then
19            $tBuffer = DllStructCreate("wchar[" & ($charCount+1) & "]", $pBuffer)
20            $Text = DllStructGetData($tBuffer, 1)
21        EndIf
22        _WinAPI_LocalFree($pBuffer)
23    EndIf
24
25    Return $Text
26EndFunc