Jump to content

Windows Error Message


Ejoc
 Share

Recommended Posts

needs beta for dllstruct funcs...

Pulled this out of another UDF to make it its own UDF.

#include-once

;===============================================
;   _GetLastErrorMessage($DisplayMsgBox="")
;   Format the last windows error as a string and return it
;   if $DisplayMsgBox <> "" Then it will display a message box w/ the error
;   Return      Window's error as a string
;===============================================
Func _GetLastErrorMessage($DisplayMsgBox="")
    Local $ret,$s
    Local $p    = DllStructCreate("char[4096]")
    Local Const $FORMAT_MESSAGE_FROM_SYSTEM     = 0x00001000

    If @error Then Return ""

    $ret    = DllCall("Kernel32.dll","int","GetLastError")

    $ret    = DllCall("kernel32.dll","int","FormatMessage",_
                        "int",$FORMAT_MESSAGE_FROM_SYSTEM,_
                        "ptr",0,_
                        "int",$ret[0],_
                        "int",0,_
                        "ptr",DllStructGetPtr($p),_
                        "int",4096,_
                        "ptr",0)
    $s  = DllStructGetData($p,1)
    DllStructDelete($p)
    If $DisplayMsgBox <> "" Then MsgBox(0,"_GetLastErrorMessage",$DisplayMsgBox & @CRLF & $s)
    return $s
EndFunc
Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs
Link to comment
Share on other sites

  • 2 years later...

Good work, although im experiencing some problems, I used it for testing my "InternetConnect" call, the call is 100% tested, but it doesn't matter if i want to connect a real ftp server or a non-existing one, your function always returns "Der Vorgang wurde erfolgreich beendet" (german, something like 'The action has completed successfully').

EDIT: Do you know where the bug is? Is it WinApi GetLastError or is it your function? I'd be grateful for help

Func _FTPConnect($server, $port, $user, $pw)
    $dll = DllOpen("wininet.dll")
    ;Local $h_inet= DllCall('wininet.dll', 'long', 'InternetOpen', 'str', $s_Agent, 'long', $l_AccessType, 'str', $s_ProxyName, 'str', $s_ProxyBypass, 'long', $l_Flags)
    Local $h_inet= DllCall('wininet.dll', 'long', 'InternetOpen', 'str', 'FTP.exe', 'long', 1, 'str', '', 'str', '', 'long', 0)

    ;Local $ai_InternetConnect = DllCall('wininet.dll', 'long', 'InternetConnect', 'long', $l_InternetSession, 'str', $s_ServerName, 'int', $i_ServerPort, 'str', $s_Username, 'str', $s_Password, 'long', $l_Service, 'long', $l_Flags, 'long', $l_Context)
    Local $result = DllCall('wininet.dll', 'long', 'InternetConnect', 'long', $h_inet[0], 'str', $server, 'int', 0, 'str', $user, 'str', $pw, 'long', 1, 'long', 0, 'long', 0)
    _GetLastErrorMessage("test")
    If $result[0] = 0 Then
        SetError(1)
        Return 0
    Else
        Return $result[0]
    EndIf
EndFunc
Edited by tannerli
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...