UUID generator (windows only)

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
spodhaje
Posts: 38
Joined: Oct 25, 2007 14:05

UUID generator (windows only)

Post by spodhaje »

Someone else may find this useful too.

It's a port of the c file found at http://www.tramontana.co.hu/wix/downloa ... pe=text/xc


usage: uuidgen <number of uuid's to generate>

Code: Select all

' http://www.tramontana.co.hu/wix/download.php?file=uuidgen.c&type=text/xc

#Include "windows.bi"
#Include "win/uuids.bi"
#Include "win/ole2.bi"


Declare Function main (ByVal argc As Integer, ByVal argv As ZString Ptr Ptr) As Integer
End main( __FB_ARGC__, __FB_ARGV__ )

Private Function main(ByVal argc As Integer, ByVal argv As ZString Ptr Ptr) As Integer
    Dim guid As GUID
    Dim wstrGUID As LPOLESTR 
    Dim strGUID As ZString * 260
    Dim As Integer count

        If (argc <> 2)Then 
            Open Err For Input As #1
                Print #1,"SYNTAX: UUIDGEN <number-of-GUIDs-to-generate>"
            Close #1
            Return 1
        EndIf
   
      count = CInt(*argv[1])
      While (count > 0)
        CoCreateGuid (@guid)
        If(StringFromCLSID (@guid, @wstrGUID) = S_OK) THEN
            WideCharToMultiByte (CP_ACP, 0, Cast(WString Ptr,wstrGUID), -1, strGUID, MAX_PATH, NULL, NULL)
            Print strGUID
        EndIf
        CoTaskMemFree(@wstrGUID)
        count -= 1
      Wend 
      Return 0
End Function
SmOke_N
Posts: 15
Joined: Aug 27, 2007 4:12

Re: UUID generator (windows only)

Post by SmOke_N »

spodhaje wrote:Someone else may find this useful too.

It's a port of the c file found at http://www.tramontana.co.hu/wix/downloa ... pe=text/xc


usage: uuidgen <number of uuid's to generate>

Code: Select all

' http://www.tramontana.co.hu/wix/download.php?file=uuidgen.c&type=text/xc

#Include "windows.bi"
#Include "win/uuids.bi"
#Include "win/ole2.bi"


Declare Function main (ByVal argc As Integer, ByVal argv As ZString Ptr Ptr) As Integer
End main( __FB_ARGC__, __FB_ARGV__ )

Private Function main(ByVal argc As Integer, ByVal argv As ZString Ptr Ptr) As Integer
    Dim guid As GUID
    Dim wstrGUID As LPOLESTR 
    Dim strGUID As ZString * 260
    Dim As Integer count

        If (argc <> 2)Then 
            Open Err For Input As #1
                Print #1,"SYNTAX: UUIDGEN <number-of-GUIDs-to-generate>"
            Close #1
            Return 1
        EndIf
   
      count = CInt(*argv[1])
      While (count > 0)
        CoCreateGuid (@guid)
        If(StringFromCLSID (@guid, @wstrGUID) = S_OK) THEN
            WideCharToMultiByte (CP_ACP, 0, Cast(WString Ptr,wstrGUID), -1, strGUID, MAX_PATH, NULL, NULL)
            Print strGUID
        EndIf
        CoTaskMemFree(@wstrGUID)
        count -= 1
      Wend 
      Return 0
End Function
Do you have an example of use with this?
raph ael
Posts: 1
Joined: Aug 05, 2006 13:39
Contact:

Post by raph ael »

You need a GUID for most installers because it's needed to register programs in the Registry so you can uninstall them using Add& Remove Programs. See http://en.wikipedia.org/wiki/Guid
spodhaje
Posts: 38
Joined: Oct 25, 2007 14:05

Re: UUID generator (windows only)

Post by spodhaje »

SmOke_N wrote: Do you have an example of use with this?
usage:
uuidgen <number of uuid's to generate>

>c:\uuidgen 3
{56B07B75-D6CE-45CF-9EF5-734429AF4F57}
{AB2FB6F9-C19C-4BF6-A25E-34730186977D}
{6BB57518-1813-47C7-A9F5-6AD438D17F5B}
littlebigman
Posts: 62
Joined: May 21, 2010 11:47

Re: UUID generator (windows only)

Post by littlebigman »

This is a newbie question.

I'd like to remove the { and } characters from the output:

Code: Select all

{8421CBA2-6730-4A72-AB1D-7E53556BBA96}
The reason is that I'd like to use the code above to generate unique filenames.

Is there a simple way to do this in Freebasic?

Thank you.
Munair
Posts: 1289
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: UUID generator (windows only)

Post by Munair »

littlebigman wrote:This is a newbie question.

I'd like to remove the { and } characters from the output:

Code: Select all

{8421CBA2-6730-4A72-AB1D-7E53556BBA96}
I cannot directly run the code on Linux, but here is a way to do it:

Code: Select all

dim s as string
s = "{8421CBA2-6730-4A72-AB1D-7E53556BBA96}"
s = mid(s, 2, len(s) - 2)
littlebigman
Posts: 62
Joined: May 21, 2010 11:47

Re: UUID generator (windows only)

Post by littlebigman »

Worked fine. Thank you.
PaulSquires
Posts: 1010
Joined: Jul 14, 2005 23:41

Re: UUID generator (windows only)

Post by PaulSquires »

Munair wrote:
littlebigman wrote:This is a newbie question.

I'd like to remove the { and } characters from the output:

Code: Select all

{8421CBA2-6730-4A72-AB1D-7E53556BBA96}
I cannot directly run the code on Linux, but here is a way to do it:

Code: Select all

dim s as string
s = "{8421CBA2-6730-4A72-AB1D-7E53556BBA96}"
s = mid(s, 2, len(s) - 2)
...or, even easier:

Code: Select all

s = "{8421CBA2-6730-4A72-AB1D-7E53556BBA96}"
s = trim(s, Any "{}")
Munair
Posts: 1289
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: UUID generator (windows only)

Post by Munair »

Code: Select all

s = mid(s, 2, len(s) - 2)
is faster
Munair
Posts: 1289
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: UUID generator (windows only)

Post by Munair »

A benchmark between the two on my machine shows that with 80M iterations:

Code: Select all

s = mid(s, 2, len(s) - 2)    ' takes ~ 18s
s = trim(s, Any "{}")        ' takes ~ 22s
It may not be much, but it's always good to know which code is more optimized. Here's the little test:

Code: Select all

dim s as string
dim t as double

t = timer

for i as ulong = 0 to 80000000
	s = "{8421CBA2-6730-4A72-AB1D-7E53556BBA96}"
	s = mid(s, 2, len(s) - 2)
	's = trim(s, Any "{}")
next

t = timer - t
print t

sleep
end
;)
marcov
Posts: 3503
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: UUID generator (windows only)

Post by marcov »

If you want it optimized, work on the binary representation after CoCreateGuid (@guid), and don't convert it to string, and then take convert it back again.
PaulSquires
Posts: 1010
Joined: Jul 14, 2005 23:41

Re: UUID generator (windows only)

Post by PaulSquires »

Munair wrote:A benchmark between the two on my machine shows that with 80M iterations:

Code: Select all

s = mid(s, 2, len(s) - 2)    ' takes ~ 18s
s = trim(s, Any "{}")        ' takes ~ 22s
It may not be much, but it's always good to know which code is more optimized. Here's the little test:

Code: Select all

dim s as string
dim t as double

t = timer

for i as ulong = 0 to 80000000
	s = "{8421CBA2-6730-4A72-AB1D-7E53556BBA96}"
	s = mid(s, 2, len(s) - 2)
	's = trim(s, Any "{}")
next

t = timer - t
print t

sleep
end
;)
LOL, Munair, fair enough. I'll use the less intuitive syntax the next time I need to save those 4 seconds on 80 million iterations should such a real world example ever present itself.

;-) ;-)
Post Reply