Generate GUIDs With One API Call

I read a great advanced tip on how to create a GUID in “Generate Unique String IDs” [“101 Tech Tips for VB Developers,” Supplement to VBPJ, August 1999]. However, you can use only one API call instead of four. The OLE32.dll contains a function called WinCoCreateGUID that does all the math for you. I have included a second function called PadZeros to format the GUID:

 Option ExplicitPrivate Type GUIDType	D1       As Long	D2       As Integer	D3       As Integer	D4(8)    As ByteEnd TypePrivate Declare Function WinCoCreateGuid 		Lib "OLE32.DLL" 	Alias "CoCreateGuid" (g As GUIDType) As LongPublic Function CreateGUIDString() As String	Dim g As GUIDType	Dim sBuf As String	Call WinCoCreateGuid(g)	sBuf = PadZeros(Hex$(g.D1), 8, True) & _		PadZeros(Hex$(g.D2), 4, True) & _		PadZeros(Hex$(g.D3), 4, True) & _		PadZeros(Hex$(g.D4(0)), 2) & _		PadZeros(Hex$(g.D4(1)), 2, True) & _		PadZeros(Hex$(g.D4(2)), 2) & _		PadZeros(Hex$(g.D4(3)), 2) & _		PadZeros(Hex$(g.D4(4)), 2) & _		PadZeros(Hex$(g.D4(5)), 2) & _		PadZeros(Hex$(g.D4(6)), 2) & _		PadZeros(Hex$(g.D4(7)), 2)	CreateGUIDString = sBufEnd FunctionPrivate Function PadZeros(ByVal sBit As String, _	ByVal iStrLen As Integer, Optional bHyphen _	As Boolean) As String	If iStrLen > Len(sBit) Then		sBit = Right$(String$((iStrLen - Len(sBit)), _			"0") & sBit, iStrLen)	End If	If bHyphen Then sBit = sBit & "-"	PadZeros = sBitEnd Function
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS