Archive: Create a GUID, a Globally Unique Identifier


Create a GUID, a Globally Unique Identifier
Creates a GUID, or Globally Unique Identifier. Thanks to everyone who helped with the hex and character array issues.

Supports: Win9x, WINNT, WIN2K and WINXP
Requires: System Plugin
Archive: http://nsis.sourceforge.net/archive/...74&instances=0


;Call CreateGUID
;Pop $0 ;contains GUID
Function CreateGUID
Push $R0
Push $R1
Push $R2
Push $R3
Push $R4
;allocate space for character array
System::Alloc 15
;get pointer to new space
Pop $R1
;call the CoCreateGuid api in the ole32.dll
System::Call 'ole32::CoCreateGuid(i R1) i .R2'
;if 0 then continue
IntCmp $R2 0 0 lbl_done
;set counter to 0
StrCpy $R3 0
lbl_loop:
;increment counter
IntOp $R3 $R3 + 1
;if less than 16 then continue
IntCmp $R3 16 lbl_done 0 lbl_done
;retrieve item in character array
System::Call "*$R1(&t$R3-1, &i1 .R2)"
;convert to hex
IntFmt $R4 "%X" $R2
StrCpy $R4 "00$R4"
StrLen $R2 $R4
IntOp $R2 $R2 - 2
StrCpy $R4 $R4 2 $R2
StrCpy $R0 "$R0$R4"
Goto lbl_loop
lbl_done:
;cleanup
System::Free $R1
;format guid
StrCpy $R1 $R0
StrCpy $R0 ""
StrCpy $R2 $R1 8
StrCpy $R0 "$R0$R2-"
StrCpy $R2 $R1 4 8
StrCpy $R0 "$R0$R2-"
StrCpy $R2 $R1 4 12
StrCpy $R0 "$R0$R2-"
StrCpy $R2 $R1 4 16
StrCpy $R0 "$R0$R2-"
StrCpy $R2 $R1 12 20
StrCpy $R0 "$R0$R2"
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Exch $R0
FunctionEnd

Works great!
Could you post it on the NSIS Archive for others.

Thanks

-Stu :)


He already did.


You actually need 16 bytes to be allocated for GUID (128 bit) (I meaning your system::alloc). The alternative way completely thru ole and kernel functions (string guid at $3):
System::Alloc 16
Pop $1
System::Alloc 80
Pop $2
System::Call 'ole32::CoCreateGuid(i r1) i'
System::Call 'ole32::StringFromGUID2(i r1, i r2, i 80) i'
System::Call 'kernel32::WideCharToMultiByte(i 0, i 0, i r2, i 80, t .r3, i ${NSIS_MAX_STRLEN}, i 0, i 0) i'
System::Free $1
System::Free $2


Oh :) A bit better look :)

System::Alloc 80
System::Alloc 16
System::Call 'ole32::CoCreateGuid(i sr1) i'
System::Call 'ole32::StringFromGUID2(i r1, i sr2, i 80) i'
System::Call 'kernel32::WideCharToMultiByte(i 0, i 0, i r2, i 80, t .r3, i ${NSIS_MAX_STRLEN}, i 0, i 0) i'
System::Free $1
System::Free $2


Thanks for the help brainsucker. I actually was going to use the stringfromguid2 api but I was under the impression that it wasn't support under Win9x. I've updated the archive entree with your better version. Thanks again.


So will this function work with Win 9x machines? Does the archive need to be updated? :D


I've tested it on 98, ME, NT, 2K and XP without any problems. I have already updated the archive with the new code.

Thanks


:up: good code dselkirk :up: :)


the new code is thanks to brainsucker. thanks anyway.


I had the same question with regard to 9x support, basically from microsoft site stringfromguid2 does seem to be supported but it's WideCharToMultiByte that might not be fully supported with Windows 95 unless you have the Microsoft Layer for Unicode software installed. Just thought I would post this in case someone else looks this up.

My question is, how safe is it to switch and not block out users that might not have this installed?

StringFromGUID2:
http://msdn.microsoft.com/library/de...f_m2z_2jzm.asp

WideCharToMultiByte:
http://msdn.microsoft.com/library/de...icode_2bj9.asp


It's not fully supported, but the support it does give is sufficient. The translated text is plain US-ASCII, only English letters and numbers. There shouldn't be any problem using unicows.dll, but there is no reason to.