The MASM Forum

General => The Workshop => Windows API => Topic started by: NoCforMe on June 30, 2024, 04:45:17 PM

Title: A GUID getter for assembly language
Post by: NoCforMe on June 30, 2024, 04:45:17 PM
We were discussing GUIDs in another thread (https://masm32.com/board/index.php?topic=12071.0). I took a look at the little Microsoft utility to get GUIDs, GUIDGEN.EXE. After figuring out how to get a GUID (easy peasy, just call UuidCreate()), I thought it might be nice to have a utility like that more useful to us assembly-language programmers.

Here it is, attached to this post. It displays the GUID in three forms: as the standard XXXXXXXX-XXXX-XXXX ... form, as a set of DB statements, and as a GUID structure declaration.

It works fine. However, before anyone starts using this, I wonder if someone could check my work here. Specifically, I'm really not sure about byte (and nybble) order. The top display field shows the data returned in the GUID structure as a sequence of bytes, left to right. What I'm not sure about is the values in the GUID structure (last field). Are those values correct? They're both byte- and nybble-swapped. (Dontcha just love little-endianness?)

So where the first part (4 bytes, 8 hex characters) is FCFFD8CD, the DWORD value in the structure is 0DC8DFFCFh. Izzat correct?

(https://i.postimg.cc/CZbgHxhT/GUID4ASM.jpg) (https://postimg.cc/CZbgHxhT)
Title: Re: A GUID getter for assembly language
Post by: sinsi on June 30, 2024, 05:25:17 PM
Just use UuidToString to see how the bytes are formatted.

edit: The program works OK
Title: Re: A GUID getter for assembly language
Post by: TimoVJL on June 30, 2024, 06:22:38 PM
Also a string for register would be useful, as it might be needed for later usage.
Title: Re: A GUID getter for assembly language
Post by: NoCforMe on June 30, 2024, 06:55:39 PM
Quote from: TimoVJL on June 30, 2024, 06:22:38 PMAlso a string for register would be useful, as it might be needed for later usage.
You mean for the registry? I thought that was the XXXXXXXX-XXXX-XXXX ... form. What format exactly is it?
Title: Re: A GUID getter for assembly language
Post by: NoCforMe on June 30, 2024, 07:03:37 PM
Quote from: sinsi on June 30, 2024, 05:25:17 PMedit: The program works OK
Welllll ... there was a pretty basic error in there (I left out a character from my hex conversion table). Fixed that, but I'm still very confused. Check out screen shot:

(https://i.postimg.cc/G8X525Y4/GUID4-ASM-2.jpg) (https://postimg.cc/G8X525Y4)

The output from UuidToString() is shown in the status bar at bottom, so we can assume that's correct, right? So what's the correct byte order for things? I guess my GUID string (1st one at top) should match that one, right? Which means they're using the binary order (little-endian), not the byte order. Aaaaargh.
Title: Re: A GUID getter for assembly language
Post by: sinsi on June 30, 2024, 07:07:14 PM
Quote from: NoCforMe on June 30, 2024, 07:03:37 PMThe output from UuidToString() is shown in the status bar at bottom
Not here.
Title: Re: A GUID getter for assembly language
Post by: NoCforMe on June 30, 2024, 07:09:26 PM
Sorry, a later revision. See screenshot. Well, might as well attach it here.
Title: Re: A GUID getter for assembly language
Post by: sinsi on June 30, 2024, 07:42:52 PM
Here's what I get
e4 4c 54 8a 70 77 18 4d-83 70 0a e4 8a 29 78 52
8a544ce4-7770-4d18-8370-0ae48a297852
Typical MS, the format doesn't match the structure layout.
Title: Re: A GUID getter for assembly language
Post by: NoCforMe on June 30, 2024, 08:09:45 PM
So it looks like my mistake was reversing nybbles within each byte.

In your example, it looks like it does match the structure. First 4 bytes:
e4 4c 54 8a
which makes a DWORD (Data1) of
8a544ce4
Right?
Title: Re: A GUID getter for assembly language
Post by: sinsi on June 30, 2024, 08:18:24 PM
GUID STRUCT
 Data1 DWORD ?
 Data2 WORD  ?
 Data3 WORD  ?
 Data4 BYTE  8 DUP (?)
GUID ENDS
Doesn't match the grouping
 Data1 dd 8a544ce4h
 Data2 dw 7770h
 Data3 dw 4d18h
 Data? db 83h,70h
 Data? db 0ah,0e4h,8ah,29h,78h,52h
[/code]
Title: Re: A GUID getter for assembly language
Post by: NoCforMe on June 30, 2024, 08:26:47 PM
Wonder if there's a good (and accurate) description of the actual layout somewhere online?
(Besides the Micro$oft docs!)
Title: Re: A GUID getter for assembly language
Post by: C3 on June 30, 2024, 08:30:33 PM
Quote from: NoCforMe on June 30, 2024, 08:26:47 PMWonder if there's a good (and accurate) description of the actual layout somewhere online?

Sinsi's structure is ok. Windows Learn: https://learn.microsoft.com/en-us/windows/win32/api/guiddef/ns-guiddef-guid
Title: Re: A GUID getter for assembly language
Post by: NoCforMe on June 30, 2024, 08:37:22 PM
Yes, we know the structure is correct. But what about the byte order of the data? That's the issue here.
Title: Re: A GUID getter for assembly language
Post by: sinsi on June 30, 2024, 08:46:55 PM
If you look at the layout, it's xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 8-4-4-4-12 digits
but the structure is 8-4-4-16, and the last 8 bytes (4-12 16 digits) are split but in a weird way.
Title: Re: A GUID getter for assembly language
Post by: NoCforMe on June 30, 2024, 08:58:42 PM
How about this: let's take a completely empirical approach:
That should show us the correct order of things.
Title: Re: A GUID getter for assembly language
Post by: sinsi on June 30, 2024, 09:32:12 PM
Quote from: NoCforMe on June 30, 2024, 08:58:42 PMHow about this: let's take a completely empirical approach:
  • Use UuidCreate() to get a GUID
  • Look at the data in the GUID structure, byte by byte
  • Use UuidToString() to create the string
  • Compare the data layout to the byte order in the string
That should show us the correct order of things.
Already did that.
e4 4c 54 8a 70 77 18 4d-83 70 0a e4 8a 29 78 52
8a544ce4-7770-4d18-8370-0ae48a297852
The top line is the buffer from UuidCreate
The next line is from UuidToString
Title: Re: A GUID getter for assembly language
Post by: TimoVJL on July 01, 2024, 03:24:16 AM
What is a GUID? (https://guid.one/guid)
A Universally Unique IDentifier (UUID) URN Namespace (https://www.ietf.org/rfc/rfc4122.txt)
Title: Re: A GUID getter for assembly language
Post by: TimoVJL on July 01, 2024, 03:40:42 AM
Quote from: NoCforMe on June 30, 2024, 06:55:39 PM
Quote from: TimoVJL on June 30, 2024, 06:22:38 PMAlso a string for register would be useful, as it might be needed for later usage.
You mean for the registry? I thought that was the XXXXXXXX-XXXX-XXXX ... form. What format exactly is it?
string like "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Title: Re: A GUID getter for assembly language
Post by: NoCforMe on July 01, 2024, 06:38:36 AM
So all I need to do is enclose it in {}. Easy peasy.
Title: Re: A GUID getter for assembly language
Post by: NoCforMe on July 01, 2024, 06:40:43 AM
Quote from: sinsi on June 30, 2024, 09:32:12 PM
Quote from: NoCforMe on June 30, 2024, 08:58:42 PMHow about this: let's take a completely empirical approach:
  • Use UuidCreate() to get a GUID
  • Look at the data in the GUID structure, byte by byte
  • Use UuidToString() to create the string
  • Compare the data layout to the byte order in the string
That should show us the correct order of things.
Already did that.
e4 4c 54 8a 70 77 18 4d-83 70 0a e4 8a 29 78 52
8a544ce4-7770-4d18-8370-0ae48a297852
The top line is the buffer from UuidCreate
The next line is from UuidToString
This is my guide, then, to fixing my utility so it shows the value properly. Probably be done later today ...
Title: Re: A GUID getter for assembly language
Post by: TimoVJL on July 01, 2024, 07:47:29 AM
Quote from: NoCforMe on July 01, 2024, 06:38:36 AMSo all I need to do is enclose it in {}. Easy peasy.
Windows API function StringFromGUID2() make it in UNICODE form for reference.
Title: Re: A GUID getter for assembly language
Post by: NoCforMe on July 01, 2024, 07:51:28 AM
Quote from: TimoVJL on July 01, 2024, 07:47:29 AM
Quote from: NoCforMe on July 01, 2024, 06:38:36 AMSo all I need to do is enclose it in {}. Easy peasy.
Windows API function StringFromGUID2() make it in UNICODE form for reference.
Unicode, schmunicode; all that's needed are plain ASCII characters here.
Title: Re: A GUID getter for assembly language
Post by: NoCforMe on July 01, 2024, 07:58:00 AM
OK, new version attached. It puts the GUID in the same format (byte-order wise) as UuidToString(), as shown in the status bar).

Timo, I added "{}" to the GUID string shown at the top: happy? It ain't Unicode, but you can certainly copy and paste into anything.

So it turns out that the GUID format is:

Weird.

Last question: I'm using uppercase hex characters here, but UuidToString() renders them in lowercase. Probly doesn't matter, but I could put in an option for case here; anyone think that would be a good thing?
Title: Re: A GUID getter for assembly language
Post by: TimoVJL on July 01, 2024, 08:15:45 AM
In windows register GUIDs are mostly uppercase, like CLSIDs, but not necessarily.
Title: Re: A GUID getter for assembly language
Post by: NoCforMe on July 01, 2024, 08:19:18 AM
Quote from: TimoVJL on July 01, 2024, 08:15:45 AMIn windows register registry GUIDs are mostly uppercase, like CLSIDs, but not necessarily.
Title: Re: A GUID getter for assembly language
Post by: NoCforMe on July 01, 2024, 08:38:19 AM
New version gives you the choice of uppercase or not.