Microsoft KB Archive/269387

= How To Obtain a GUID in Visual FoxPro =

Article ID: 269387

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q269387



SUMMARY
In many application scenarios, it is necessary to generate a unique value for primary and candidate keys in tables. A Globally Unique Identifier (GUID) serves this purpose. This article illustrates how to obtain a GUID from Visual FoxPro, using the CoCreateGuid API function.



MORE INFORMATION
A GUID is a globally unique 128-bit integer value that identifies objects, interfaces, manager entry-point vectors, and client objects.

The CoCreateGuid function calls the Remote Procedure Call (RPC) UuidCreate function, which in turn creates a GUID. The CoCreateGuid function may be used when it is necessary to obtain an absolutely unique number that will be used as a persistent identifier in a distributed environment. To a very high degree of certainty, this function returns a unique value: that is, no other invocation, on the same or any other system (networked or not), should return the same value.

The following sample illustrates how to call the CoCreateGuid API to return a GUID that will be used as a primary key in a Visual FoxPro table: DECLARE INTEGER CoCreateGuid IN OLE32.DLL STRING @pGuid IF !FILE('GUIDS.DBF') *!* Create a Database Container CREATE DATABASE GUIDTEST *!* Create a table with a C(38) field that is a primary key *!* The format of a GUID is '{nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn}' CREATE TABLE GUIDS (GUID C(38) PRIMARY KEY) ENDIF FOR number_of_guids=1 TO 15000 *!* Initialize the buffer that will hold the GUID pGuid=REPLICATE(CHR(0),17) *!* Call CoCreateGuid lretval=CoCreateGuid(@pGuid) IF lretval=s_ok *!* Store the first eight characters of the GUID in data1 cData1=RIGHT(TRANSFORM(strtolong(LEFT(pGuid,4)),&quot;@0&quot;),8) *!* Store the first group of four characters of the GUID in data2 cData2=RIGHT(TRANSFORM(strtolong(SUBSTR(pGuid,5,2)),&quot;@0&quot;),4) *!* Store the second group of four characters of the GUID in data3 cData3=RIGHT(TRANSFORM(strtolong(SUBSTR(pGuid,7,2)),&quot;@0&quot;),4) *!* Store the third group of four characters of the GUID in data4 cData4=RIGHT(TRANSFORM(strtolong(SUBSTR(pGuid,9,1)),&quot;@0&quot;),2) + ; RIGHT(TRANSFORM(strtolong(SUBSTR(pGuid,10,1)),&quot;@0&quot;),2) *!* Initialize data5 to a null string cData5=&quot;&quot; *!* Convert the final 12 characters of the GUID and store in data5 FOR nGuidLen=1 TO 6 cData5=cData5+RIGHT(TRANSFORM(strtolong(SUBSTR(pGuid,10+nGuidLen,1))),2) ENDFOR *!* Check the length of data5. If less than 12, the final 12-len(data5) *!* characters are '0' IF LEN(cData5)<12 cData5=cData5+REPLICATE(&quot;0&quot;,12-LEN(cData5)) ENDIF *!* Assemble the GUID into a string data_all=&quot;{&quot;+cData1+&quot;-&quot;+cData2+&quot;-&quot;+cData3+&quot;-&quot;+cData4+&quot;-&quot;+cData5+&quot;}&quot; *!* Insert the GUID into the table INSERT INTO GUIDS VALUES (data_all) ENDIF ENDFOR CLEAR DLLS SELECT GUID, COUNT(GUID) AS hits FROM GUIDS GROUP BY GUID INTO CURSOR testa SELECT * FROM testa WHERE hits>1 INTO CURSOR testb BROWSE
 * 1) DEFINE s_ok 0
 * !* Declare the API that will be called
 * !* Done with the call to CoCreateGuid, so clear the DLLs from memory
 * !* Check for duplicate values in the GUIDS table

FUNCTION strtolong PARAMETERS lcLongstr PRIVATE I, lnRetval lnRetval = 0 FOR I = 0 TO 24 STEP 8 lnRetval = lnRetval + (ASC(lcLongstr) * (2^I)) lcLongstr = RIGHT(lcLongstr, LEN(lcLongstr) - 1) NEXT RETURN lnRetval
 * !* Passed: 4-byte character string (lcLongstr) in low-high ASCII format
 * !* Returns: long integer value
 * !* Example:
 * !* m.longstr = &quot;1111&quot;
 * !* m.longval = strtolong(m.longstr)

