Generate GUID

UDF below generates 128-bit GUID and returns it as a string in the format 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.

MSDN:

This is sample code. Add error handling and adjust to your requirements as necessary.

FUNCTION GenerateGUID
	LOCAL lcGUID, lcStrucGUID, lnSize
	lcStrucGUID = SPACE (16)
	lcGUID      = SPACE (78)
	lnSize      = LEN(lcGUID)/2
 
	IF CoCreateGuid(@lcStrucGUID) <> 0
	   RETURN ""
	ENDIF
 
	IF StringFromGUID2(lcStrucGUID, @lcGuid, lnSize) = 0
	  RETURN ""
	ENDIF
 
	* VFP 8.0 and later
	RETURN STREXTRACT(STRCONV(lcGUID,6), "{", "}")
	* Previous versions
	*RETURN CHRTRAN(STRCONV(lcGUID, 6), "{}" + CHR(0), "")
ENDFUNC
 
FUNCTION CoCreateGuid(tcStrucGUID)
	DECLARE INTEGER CoCreateGuid IN Ole32.dll STRING @cGUIDStruc
	RETURN CoCreateGuid(@tcStrucGUID)
ENDFUNC	
 
 
FUNCTION StringFromGUID2(tcStrucGUID, tcGuid, tnSize)
	DECLARE INTEGER StringFromGUID2 IN Ole32.dll STRING cGUIDStruc, STRING @cGUID, LONG nSize
	RETURN StringFromGUID2(tcStrucGUID, @tcGuid, tnSize)
ENDFUNC	

Alternatively, WSH object for constructing type libraries for scriptlets (Scriptlet.Typelib) can be used

FUNCTION GenerateGUID
	LOCAL loObj
	loObj = CREATEOBJECT("Scriptlet.TypeLib")
	* VFP 8.0 and later
	RETURN STREXTRACT(loObj.GUID, "{", "}")
	* Previous versions
	*RETURN CHRTRAN(loObj.GUID, "{}", "")
ENDFUNC

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <java>, <powershell>, <tsql>, <visualfoxpro>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.