Generate Unique String IDs

Generate Unique String IDs

If you need unique string IDs and don’t have a sure-fire way of either generating or guaranteeing the generated ID is unique, then you need a Universally Unique ID (UUID) or Globally Unique ID (GUID) as Microsoft calls them. A UUID is a 128-bit number that’s generated based on a time value and your computer’s network interface card (NIC) and is guaranteed to be unique (at least within your network and until about the year 3400).

This routine generates UUIDs and converts them into 36-byte strings. Just paste this code into a module:

 Option ExplicitPrivate Declare Function UuidCreate Lib _	"rpcrt4.dll" (pId As UUID) As LongPrivate Declare Function UuidToString Lib _	"rpcrt4.dll" Alias "UuidToStringA" (uuidID _	As UUID, ppUuid As Long) As LongPrivate Declare Function RpcStringFree Lib _	"rpcrt4.dll" Alias "RpcStringFreeA" _	(ppStringUuid As Long) As LongPrivate Declare Function CopyMemory Lib _	"kernel32.dll" Alias "RtlMoveMemory" (pDst _	As Any, pSrc As Any, ByVal nSize As Long) _	As LongPrivate Type UUID	Data1 As Long	Data2 As Long	Data3 As Long	Data4(8) As ByteEnd TypePublic Function GenUuid(sUuid As String) As _	Boolean	Const RPC_S_OK As Long = 0	Const SZ_UUID_LEN As Long = 36	Dim uuidID As UUID	Dim sUid As String	Dim ppUuid As Long	sUid = String(SZ_UUID_LEN, 0)	If UuidCreate(uuidID) = RPC_S_OK Then		If UuidToString(uuidID, ppUuid) = _			RPC_S_OK Then			CopyMemory ByVal sUid, ByVal ppUuid, _				SZ_UUID_LEN			If RpcStringFree(ppUuid) = RPC_S_OK Then				sUuid = sUid				GenUuid = True			End If		End If	End IfEnd Function

Use the function like this:

 Dim sId As StringCall GenUuid(sId)MsgBox "Id is " & sId 

I’m sure you can find better uses for the ID than just displaying it. But be aware that these numbers have the potential to uniquely identify the machine on which they were generated, raising security concerns. Depending on your customers and how you use the ID, this might not be an issue for you.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as