The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Ehtyar on February 20, 2007, 12:04:07 PM

Title: Problem generating GUIDs
Post by: Ehtyar on February 20, 2007, 12:04:07 PM
Hi all.
I've spent half of the night trying to generate a proper GUID. For some reason, i get zeros in strange places all throughout the GUID, for no apparent reason. The format is XXXXXXXX-XXXX-XXXX0000-XX000000XXXX000000XX when it should be XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. My code is below, i would appreciate any help on this subject.
Thanks, Ehtyar.
P.S. I am aware the size and type of szGuid is incorrect, changing these makes no difference. I have tried many many different format strings i found on google, none of which work, if you suggest a change to this, please be sure it works. Thanks again.

.DATA
szFmt CHAR "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", 0
.DATA?
szGuid CHAR MAX_PATH dup(?)
sGUID GUID <?>
.CODE
code:
restart:
invoke CoCreateGuid, addr sGUID
invoke wsprintf, addr szGuid, addr szFmt, sGUID.Data1, sGUID.Data2, sGUID.Data3, sGUID.Data4[0], sGUID.Data4[1], sGUID.Data4[2], sGUID.Data4[3], sGUID.Data4[4], sGUID.Data4[5], sGUID.Data4[6], sGUID.Data4[7]
invoke MessageBox, HWND_DESKTOP, addr szGuid, NULL, MB_YESNO
.IF eax == IDYES
jmp restart
.ENDIF
invoke ExitProcess, 0
END code
Title: Re: Problem generating GUIDs
Post by: Tedd on February 20, 2007, 01:58:06 PM
wsprintf is messing you up -- "%02x" means at least 2 digits (pad with zeroes if necessary), not just the bottom two digits (if there are more non-zero digits it will also print them)

.586
.model flat, stdcall
option casemap:none
include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
include ole32.inc
includelib ole32.lib

;***************************************************************************************************

.data
hmm         db "What's your number?",0
guid_fmt    db "{%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x}",0

.data?
gooie       GUID <?>
buff        db 64 dup (?)

.code
start:
    invoke CoCreateGuid, ADDR gooie

    xor eax,eax

    mov al,BYTE PTR[gooie.Data4+7]
    push eax                        ;2
    mov al,BYTE PTR[gooie.Data4+6]
    push eax                        ;2
    mov al,BYTE PTR[gooie.Data4+5]
    push eax                        ;2
    mov al,BYTE PTR[gooie.Data4+4]
    push eax                        ;2

    mov al,BYTE PTR[gooie.Data4+3]
    push eax                        ;2
    mov al,BYTE PTR[gooie.Data4+2]
    push eax                        ;2

    mov ax,WORD PTR[gooie.Data4]
    push eax                        ;4

    mov ax,[gooie.Data3]
    push eax                        ;4

    mov ax,[gooie.Data2]
    push eax                        ;4

    push DWORD PTR[gooie.Data1]     ;8

    push OFFSET guid_fmt
    push OFFSET buff
    call wsprintf
    add esp,(4*12)
    invoke MessageBox, NULL,ADDR buff,ADDR hmm,MB_OK

    invoke ExitProcess, NULL
end start

Title: Re: Problem generating GUIDs
Post by: jdoe on February 20, 2007, 06:38:43 PM

To display the generated guid...


.DATA

sGUID GUID <?>
wzGUID WORD 40 DUP (0)

.CODE

Main:

   invoke CoCreateGuid, ADDR sGUID
   invoke StringFromGUID2, addr sGUID, addr wzGUID, 40

   invoke MessageBoxW, NULL, addr wzGUID, NULL, NULL
   invoke ExitProcess, NULL

END Main


Title: Re: Problem generating GUIDs
Post by: Ehtyar on February 20, 2007, 08:03:30 PM
Thanks so much guys for all your work, both methods work perfectly :U :U. jdoe, can't believe i couldnt find that api, perhaps it will give me an excuse to use some unicode :)

Ehtyar.
Title: Re: Problem generating GUIDs
Post by: jdoe on February 20, 2007, 09:07:36 PM
Quote from: Ehtyar on February 20, 2007, 08:03:30 PM
Thanks so much guys for all your work, both methods work perfectly :U :U. jdoe, can't believe i couldnt find that api, perhaps it will give me an excuse to use some unicode :)

Ehtyar.

You can use ANSI either with a call to WideCharToMultiByte.


.DATA

sGUID GUID <?>
wzGUID WORD 40 DUP (0)
szGUID BYTE 40 DUP (0)


.CODE

Main:

   invoke CoCreateGuid, ADDR sGUID
   invoke StringFromGUID2, addr sGUID, addr wzGUID, 40
   invoke WideCharToMultiByte, CP_ACP, 0, addr wzGUID, -1, addr szGUID , 40, NULL, NULL

   invoke MessageBoxA, NULL, addr szGUID, NULL, NULL
   invoke ExitProcess, NULL

END Main


:thumbu
Title: Re: Problem generating GUIDs
Post by: Ehtyar on February 20, 2007, 10:29:03 PM
Thanks very much for the tip jdoe, but i will use Tedd's version for ansi, as i don't want those bleeping brackets. That api also sounds fun though, will play with it today a little i think, thanks :)

Ehtyar.
Title: Re: Problem generating GUIDs
Post by: Tedd on February 21, 2007, 01:39:30 PM
I did it the 'long' way there just to demonstrate, but you could make it a little shorter..


.data
    guid_fmt    db "{%08x-%04x-%04x-%04x-%04x%08x}",0

.code
    mov eax,DWORD PTR[gooie.Data4+4]
    bswap eax
    push eax                        ;2222 (8)

    xor eax,eax
    mov ax,WORD PTR[gooie.Data4+2]
    xchg ah,al
    push eax                        ;22 (4)

    .
    .
    .
Title: Re: Problem generating GUIDs
Post by: Ehtyar on February 22, 2007, 05:57:45 AM
Thanks Tedd. But i think I'd better stick to the version i understand :(

Ehtyar.