Jump to content

Windows GUIDs


jaberwacky
 Share

Recommended Posts

Here's a function for loading a GUID string into a _GUID struct. Currently only accepts GUID strings formatted like this: 6B29FC40-CA47-1067-B31D-00DD010662DA. Hope it's usefull.

CreateGUIDStruct("D16A55E8-1059-11D2-8FFD-00A0C9A06D32")
ConsoleWrite("@ERROR: " & @error & @LF)

CreateGUIDStruct("D16A55E8-1059-11D2-8FFD-00A0C9A06D325")
ConsoleWrite("@ERROR: " & @error & @LF)

CreateGUIDStruct('')
ConsoleWrite("@ERROR: " & @error & @LF)

CreateGUIDStruct(' ')
ConsoleWrite("@ERROR: " & @error & @LF)

; = Function ==========================================================================================================
; Name .....: CreateGUIDStruct
; Syntax ...: CreateGUIDStruct($GUIDstr)
; Param(s) .: $GUIDstr -- String containing a GUID. Cannot be longer or shorter than 36 characters.
; Success ..: A _GUID struct containing the GUID string.
; Fail .....: 1 & @error: -1: String was formatted incorrectly.
; Desc. ....: http://msdn.microsoft.com/en-us/library/aa373931%28VS.85%29.aspx
Func CreateGUIDStruct(Const $GUIDstr)
    Switch (IsString($GUIDstr)) And ($GUIDstr <> '') And ($GUIDstr <> ' ') And (StringLen($GUIDstr) = 36)
        Case 1
            Local Const $tmpGUIDstr = StringStripWS($GUIDstr, 8)

            Local Const $GUIDArr = StringSplit($tmpGUIDstr, '-', 2)

            Local Const $dllGUID = DllStructCreate("dword Data1; word Data2; word Data3; byte Data4[8]")

            If StringLen($GUIDArr[0]) = 8 Then
                DllStructSetData($dllGUID, "Data1", $GUIDArr[0])
            Else
                Return SetError(-1, 0, 1)
            EndIf

            If StringLen($GUIDArr[1]) = 4 Then
                DllStructSetData($dllGUID, "Data2", $GUIDArr[1])
            Else
                Return SetError(-1, 0, 1)
            EndIf

            If StringLen($GUIDArr[2]) = 4 Then
                DllStructSetData($dllGUID, "Data3", $GUIDArr[2])
            Else
                Return SetError(-1, 0, 1)
            EndIf

            If StringLen($GUIDArr[3]) = 4 Then
                DllStructSetData($dllGUID, "Data4", StringLeft($GUIDArr[3], 2), 1)
                DllStructSetData($dllGUID, "Data4", StringRight($GUIDArr[3], 2), 2)
            Else
                Return SetError(-1, 0, 1)
            EndIf

            If StringLen($GUIDArr[4]) = 12 Then
                DllStructSetData($dllGUID, "Data4", StringLeft($GUIDArr[4], 2), 3)
                DllStructSetData($dllGUID, "Data4", StringTrimLeft(StringTrimRight($GUIDArr[4], 2), 8), 4)
                DllStructSetData($dllGUID, "Data4", StringTrimLeft(StringTrimRight($GUIDArr[4], 4), 6), 5)
                DllStructSetData($dllGUID, "Data4", StringTrimLeft(StringTrimRight($GUIDArr[4], 6), 4), 6)
                DllStructSetData($dllGUID, "Data4", StringTrimLeft(StringTrimRight($GUIDArr[4], 8), 2), 7)
                DllStructSetData($dllGUID, "Data4", StringRight($GUIDArr[4], 2), 8)
            Else
                Return SetError(-1, 0, 1)
            EndIf

            Return $dllGUID
        Case 0
            Return SetError(-1, 0, 1)
    EndSwitch
EndFunc ;==>CreateGUIDStruct
Edited by jaberwocky6669
Link to comment
Share on other sites

not bad, but what about _WinAPI_GUIDFromString ?

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

not bad, but what about _WinAPI_GUIDFromString ?

Oh gosh, you know what, I saw that but I have no idea what's meant by binary form. So _WinAPI_GUIDFromString() does the same thing as my function?

Link to comment
Share on other sites

Oh gosh, you know what, I saw that but I have no idea what's meant by binary form. So _WinAPI_GUIDFromString() does the same thing as my function?

Yes, but I did create a custom function for objbase (sort of predecessor of AutoItObject), too ;) (in the meantime it has been replaced )

#include<WinAPI.au3>
$tGUID = _WinAPI_GUIDFromString("{6B29FC40-CA47-1067-B31D-00DD010662DA}")
MsgBox(0, 'Result: $tagGUID', _
    StringFormat("%04X-%02X-%02X-%s", _
        DllStructGetData($tGUID, 1), _
        DllStructGetData($tGUID, 2), _
        DllStructGetData($tGUID, 3), _
        Hex(DllStructGetData($tGUID, 4)) _
    ) _
)
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

#include <WinAPI.au3>
$tGUID = _WinAPI_GUIDFromString("{6B29FC40-CA47-1067-B31D-00DD010662DA}")
MsgBox(0, 'Result: $tagGUID', _
    StringFormat("%04X-%02X-%02X-%s", _
    DllStructGetData($tGUID, 1), _
    DllStructGetData($tGUID, 2), _
    DllStructGetData($tGUID, 3), _
    Hex(DllStructGetData($tGUID, 4)) _
    ) _
)

OK, I see.

I'll pack my bags and be out by this afternoon.

Trancexx, why is it bad to return a ptr to a struct from a function?

Edited by jaberwocky6669
Link to comment
Share on other sites

This creates a string from data taken from a struct right?

Right.

PS: Your function won't work. You return the pointer to the struct, but the struct is a local variable, so it will be released when he funciton returns. You have to return the whole struct.

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Right.

PS: Your function won't work. You return the pointer to the struct, but the struct is a local variable, so it will be released when he funciton returns. You have to return the whole struct.

But if the struct is still destroyed when the function returns it doesn't matter either way. Unless I am returning a copy of the struct?

Ha, I'm beginning to think that I should have posted this in the help section!

So all of my code boils down to this:

Func _WinAPI_GUIDFromString($sGUID)
    Local $tagGUID = "dword Data1; word Data2; word Data3; byte Data4[8]"
    Local $tGUID = DllStructCreate($tagGUID)
    Local $pGUID = DllStructGetPtr($tGUID)
    DllCall("ole32.dll", "long", "CLSIDFromString", "wstr", $sGUID, "ptr", $pGUID)
    If @error Then Return SetError(@error, @extended, 0)
    Return $tGUID
EndFunc ;==>_WinAPI_GUIDFromString
Edited by jaberwocky6669
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...