Skip to content

Instantly share code, notes, and snippets.

@ijprest
Created October 6, 2012 20:00
Show Gist options
  • Save ijprest/3845947 to your computer and use it in GitHub Desktop.
Save ijprest/3845947 to your computer and use it in GitHub Desktop.
AutoHotkey (AHK) script to generate & paste a new GUID
; Exposes two hotkeys:
; - Win+G generates & pastes a new lowercase guid
; - Win+Shift+G generates & pastes a new UPPERCASE guid
; In both cases, the guid is left on the clipboard so you can easily paste it more than once.
;
GUID()
{
format = %A_FormatInteger% ; save original integer format
SetFormat Integer, Hex ; for converting bytes to hex
VarSetCapacity(A,16)
DllCall("rpcrt4\UuidCreate","Str",A)
Address := &A
Loop 16
{
x := 256 + *Address ; get byte in hex, set 17th bit
StringTrimLeft x, x, 3 ; remove 0x1
h = %x%%h% ; in memory: LS byte first
Address++
}
SetFormat Integer, %format% ; restore original format
h := SubStr(h,1,8) . "-" . SubStr(h,9,4) . "-" . SubStr(h,13,4) . "-" . SubStr(h,17,4) . "-" . SubStr(h,21,12)
return h
}
; Win+G - Generate and paste a GUID
#g::
guid := GUID()
StringLower, guid, guid
Clipboard := guid
SendInput,^v
return
; Win+Shift+G - Generate an UPPERCASE GUID
#+g::
guid := GUID()
StringUpper, guid, guid
Clipboard := guid
SendInput,^v
return
@Jordan466
Copy link

Neat. exactly what I'm looking for

@Flowgun
Copy link

Flowgun commented Feb 25, 2024

Thank you!

@mlhDevelopment
Copy link

mlhDevelopment commented Nov 1, 2024

Great idea - I converted this to v2 format and just generated a GUID 32 random characters and formatted as a GUID without referencing DLLs - https://gist.github.com/mlhDevelopment/6ef44209afbccbcd3a80fcf182b17b2a

@ijprest
Copy link
Author

ijprest commented Nov 1, 2024

@mlhDevelopment -- I'm glad you were inspired by this... however, note that generating a "proper" GUID/UUID is more involved than simply generating random numbers. See RFC 9562. UuidCreate generates a GUID using one of the approved algorithms.

@mlhDevelopment
Copy link

Great call. I knew it couldn't be that easy. I realized my needs were not as stringent to require a compliant GUID but still valued the the lack of external dependencies so I updated the documentation & expectation. Thanks for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment