Generate GUID

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

MSDN:

$SAMPLECODE$

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

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.