Jump to content

An implementation of (WinInet)InternetCrackUrl


CatchFish
 Share

Recommended Posts

Two functions: _CrackUrl() and _GetDataFromPtr()

Updated: Size of extra info changed to 2k.

Updated: Error returning fixed, which wouldn't work as planned due to a silly mistake. (Thanks w0uter)

;Just a test
Dim $a_Cracked = _CrackUrl('http://www.autoitscript.com/forum/index.php?act=post&do=new_post&f=9'), $i
For $i = 0 To UBound($a_Cracked) - 1
    ConsoleWrite($a_Cracked[$i] & @LF)
Next

#region - _GetDataFromPtr($vPtr, $vStrc = 'char[1]')

;===============================================================================
;
; Function Name:    _GetDataFromPtr()
; Description:      Retrieve data from a pointer.
; Parameter(s):     $vPtr = The pointer to the wanted data.
;                   $vStrc = A string that representing the structure for retrieving data. Default is 'char[1]'.
; Requirement:      DllStructCreate/GetData
; Return Value(s):  On Success - Returns the data.
;                   On Failure - 0 and set
;                       @error = 1 when StructCreate error, @extended = StructCreate's @error
;                       @error = 2 when StructGetData error, @extended = StructGetData's @error
; Author(s):        Valik, integrated by CatchFish
; Note(s):          Because a pointer is supplied, the created struct DOES NOT need to be freed with DllStructDelete().
;
;===============================================================================

Func _GetDataFromPtr($vPtr, $vStrc = 'char[1]')
    Local $tDataStrc, $tData
    $tDataStrc = DllStructCreate($vStrc, $vPtr);Creates the structure using the pointer
    If @error = 0 Then;Structure created successfully
        $tData = DllStructGetData($tDataStrc, 1)
        If @error = 0 Then;Data got successfully
            Return $tData
        Else;Data got failed
            SetError(@error, 2)
            Return 0
        EndIf
    Else;Structure created failed
        SetError(@error, 1)
        Return 0
    EndIf
EndFunc
#endregion


#region - _CrackUrl($s_Url, $h_Dll = "wininet.dll")

;===============================================================================
;
; Function Name:    _CrackUrl()
; Description:      Cracks a given URL into its component parts.
; Parameter(s):     $s_Url - URL to be cracked
;                   $h_Dll - Handle of the wininet.dll. If omitted, a filename of 'wininet.dll' will be used.
; Requirement:      _GetDataFromPtr(), DllStructCreate/Delete/GetData/SetData(), DllCall(), wininet.dll
; Return Value(s):  On Success - Returns an array of the component parts of the given URL, of which elements are
;                               0 = INTERNET_SCHEME value(integer, see notes)
;                               1 = Protocol scheme name
;                               2 = Host name
;                               3 = Port number(integer)
;                               4 = User name
;                               5 = Password
;                               6 = URL path
;                               7 = Extra information
;                   On Failure - Returns 0 and sets
;                               @error = 1 when an error on DllCall(), @extended =  DllCall's @error
;                               @error = 2 when an error on (WinINet)function InternetCrackUrl, @extended = System GetLastError()
; Author(s):        CatchFish
; Note(s):          Structure URL_COMPONENTS reference -
;                       http://msdn.microsoft.com/library/en-us/wininet/wininet/url_components.asp
;                   Type INTERNET_SCHEME reference -
;                       http://msdn.microsoft.com/library/en-us/wininet/wininet/internet_scheme_enumerated_type.asp
;                       Frequently used values: 1 = FTP, 3 = HTTP, 4 = HTTPS
;                   Function (WinINet)InternetCrackUrl reference -
;                       http://msdn.microsoft.com/library/en-us/wininet/wininet/internetcrackurl.asp
;
;===============================================================================

Func _CrackUrl($s_Url, $h_Dll = "wininet.dll")
    Local $strc_UC, $strc_Scheme, $strc_Host, $strc_User, $strc_Passwd, $strc_Path, $strc_Extra, $ai_ICU
    Local $i_Err = 0, $i_ErrExt = 0, $ReturnVal = 1
    $strc_UC = DllStructCreate( _;Creates an URL_COMPONENTS structure. See comments on top of this section.
                            'int;' & _;1  - Size of this structure, in bytes.
                            'ptr;' & _;2  - Pointer to a string that contains the scheme name.
                            'int;' & _;3  - Size of the scheme name, in TCHARs.
                            'int;' & _;4  - INTERNET_SCHEME value that indicates the Internet protocol scheme.
                            'ptr;' & _;5  - Pointer to a string that contains the host name.
                            'int;' & _;6  - Size of the host name, in TCHARs.
                            'int;' & _;7  - Converted port number.
                            'ptr;' & _;8  - Pointer to a string value that contains the user name.
                            'int;' & _;9  - Size of the user name, in TCHARs.
                            'ptr;' & _;10 - Pointer to a string that contains the password.
                            'int;' & _;11 - Size of the password, in TCHARs.
                            'ptr;' & _;12 - Pointer to a string that contains the URL path.
                            'int;' & _;13 - Size of the URL path, in TCHARs.
                            'ptr;' & _;14 - Pointer to a string that contains the extra information (for example, ?something or #something).
                            'int' ) ;15 - Size of the extra information, in TCHARs.
    $strc_Scheme = DllStructCreate('char[32]')  ;Buffer for the protocol scheme name
    $strc_Host   = DllStructCreate('char[256]') ;Buffer for the host name
    $strc_User   = DllStructCreate('char[128]') ;Buffer for the user name
    $strc_Passwd = DllStructCreate('char[128]') ;Buffer for the password
    $strc_Path   = DllStructCreate('char[2048]');Buffer for the URL path
    $strc_Extra  = DllStructCreate('char[2048]');Buffer for the extra information
    DllStructSetData($strc_UC, 1,  DllStructGetSize($strc_UC))  ;Sets size of the URL_COMPONENTS structure
    DllStructSetData($strc_UC, 2,  DllStructGetPtr($strc_Scheme));Sets the pointer to the protocol scheme name buffer
    DllStructSetData($strc_UC, 3,  DllStructGetSize($strc_Scheme));Sets size of the buffer
    DllStructSetData($strc_UC, 5,  DllStructGetPtr($strc_Host)) ;Sets the pointer to the host name buffer
    DllStructSetData($strc_UC, 6,  DllStructGetSize($strc_Host));Sets size of the buffer
    DllStructSetData($strc_UC, 8,  DllStructGetPtr($strc_User)) ;Sets the pointer to the user name buffer
    DllStructSetData($strc_UC, 9,  DllStructGetSize($strc_User));Sets size of the buffer
    DllStructSetData($strc_UC, 10, DllStructGetPtr($strc_Passwd));Sets the pointer to the password buffer
    DllStructSetData($strc_UC, 11, DllStructGetSize($strc_Passwd));Sets size of the buffer
    DllStructSetData($strc_UC, 12, DllStructGetPtr($strc_Path)) ;Sets the pointer to the URL path buffer
    DllStructSetData($strc_UC, 13, DllStructGetSize($strc_Path));Sets size of the buffer
    DllStructSetData($strc_UC, 14, DllStructGetPtr($strc_Extra));Sets the pointer to the extra information buffer
    DllStructSetData($strc_UC, 15, DllStructGetSize($strc_Extra));Sets size of the buffer
    $ai_ICU = DllCall($h_DLL, _                 ;Handle of WinINet DLL
                    'int', 'InternetCrackUrl', _    ;Function that cracks a URL into its component parts
                    'str', $s_URL, _                ;String that contains the canonical URL to be cracked.
                    'int', 0, _                     ;Size of the url string, in TCHARs, or zero if url is an ASCIIZ string.
                    'int', 0, _                     ;Flag that controls the operation, can be one of the following values: ICU_DECODE = 0x10000000, ICU_ESCAPE = 0x80000000
                    'int', DllStructGetPtr($strc_UC) );Pointer to a URL_COMPONENTS structure that receives the URL components
    If @error <> 0 Then     ;ERROR on DllCall
        $i_Err = 1
        $i_ErrExt = @error      ;Passes the DllCall @error to extended error
        $ReturnVal = 0
    ElseIf $ai_ICU[0] = 0 Then;ERROR on (WinINet)function InternetCrackUrl
        Local $tError = DLLCall("kernel32.dll","int","GetLastError");Gets the system error code
        $i_Err = 2
        $i_ErrExt = $tError                                         ;Passes the system error code to extended error
        $ReturnVal = 0
    Else                    ;Everything's OK
        Local $ReturnVal[8]
        $ReturnVal[0] = DllStructGetData($strc_UC, 4)                                                                   ;INTERNET_SCHEME value
        $ReturnVal[1] = _GetDataFromPtr(DllStructGetData($strc_UC, 2 ), 'char[' & DllStructGetData($strc_UC, 3 ) & ']') ;Protocol scheme name
        $ReturnVal[2] = _GetDataFromPtr(DllStructGetData($strc_UC, 5 ), 'char[' & DllStructGetData($strc_UC, 5 ) & ']') ;Host name
        $ReturnVal[3] = DllStructGetData($strc_UC, 7)                                                                   ;Port number
        $ReturnVal[4] = _GetDataFromPtr(DllStructGetData($strc_UC, 8 ), 'char[' & DllStructGetData($strc_UC, 8 ) & ']') ;User name
        $ReturnVal[5] = _GetDataFromPtr(DllStructGetData($strc_UC, 10), 'char[' & DllStructGetData($strc_UC, 10) & ']') ;Password
        $ReturnVal[6] = _GetDataFromPtr(DllStructGetData($strc_UC, 12), 'char[' & DllStructGetData($strc_UC, 12) & ']') ;URL path
        $ReturnVal[7] = _GetDataFromPtr(DllStructGetData($strc_UC, 14), 'char[' & DllStructGetData($strc_UC, 14) & ']') ;Extra information
    EndIf
;Destroys the structures
    DllStructDelete($strc_Extra)
    DllStructDelete($strc_Path)
    DllStructDelete($strc_Passwd)
    DllStructDelete($strc_User)
    DllStructDelete($strc_Host)
    DllStructDelete($strc_Scheme)
    DllStructDelete($strc_UC)
;Sets errors and returns
    SetError($i_Err, $i_ErrExt)
    Return $ReturnVal
EndFunc ;==>_CrackUrl()

#endregion
Edited by CatchFish
Link to comment
Share on other sites

  • 5 months later...
  • Developers

It seems that this implementation doesn't work anymore. Is DllStructCreate discontinued?

Nope... just DllStructDelete()

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

what is this for???

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

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...