INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Tips -N- Tricks

How do I obtain a GUID? by foxdev
Posted: 19 Sep 00

A GUID, or globally unique identifier, is a number that Microsoft guarantees is unique throughout the entire world.  It is best used when you need a primary key that is absolutely, positively unique, and you have distributed systems in which data is entered.

For example, if you have a mobile sales force that uses laptop computers to enter sales orders and customers in the field, then uploads that data to a central system, simply assigning incremental integer values in the field will cause multiple laptops submitting identical key values.  If, instead, the laptop programs generated a GUID, then you can be assured that even when the data is combined with data from other users from around the world each record's key (the GUID) will be unique.

Downside to using GUIDs: besides making an API call, GUIDs consume 36 bytes of storage - much larger than an integer.  Therefore, they should only be used where you need to ensure unique values across a distributed system, or believe that someday you may be joining data from currently separate systems.


This code is based on a Microsoft article located at:  http://support.microsoft.com/support/kb/articles/Q269/3/87.asp
article (c) 2000 Microsoft Corp.




* -- returns a Globally Unique IDentifier (GUID)

local cData1, cData2, cData3, cData4, cData5, cDataAll
private PGuid

store "" to cData1, cData2, cData3, cData4, cData5, cDataAll

* -- declare external function
DECLARE INTEGER CoCreateGuid IN OLE32.DLL STRING @pGuid

* -- Initialize the buffer that will hold the GUID with nulls
pGuid = REPLICATE(CHR(0),17)

* -- Call CoCreateGuid

IF CoCreateGuid(@pGuid) = 0   && success

   * -- Store the first eight characters of the GUID in data1
   cData1 = RIGHT(TRANSFORM(strtolong(LEFT(pGuid,4)),"@0"),8)

   * -- Store the first group of four characters of the GUID in data2
   cData2 = RIGHT(TRANSFORM(strtolong(SUBSTR(pGuid,5,2)),"@0"),4)

   * -- Store the second group of four characters of the GUID in data3
   cData3 = RIGHT(TRANSFORM(strtolong(SUBSTR(pGuid,7,2)),"@0"),4)

   * -- Store the third group of four characters of the GUID in data4
   cData4 = RIGHT(TRANSFORM(strtolong(SUBSTR(pGuid,9,1)),"@0"),2) + ;
      RIGHT(TRANSFORM(strtolong(SUBSTR(pGuid,10,1)),"@0"),2)

   * -- Initialize data5 to a null string
   cData5 = ""

   * -- 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("0",12-LEN(cData5))
   ENDIF

   * -- Assemble the GUID into a string
   cDataAll = cData1+"-"+cData2+"-"+cData3+"-"+cData4+"-"+cData5

ENDIF


* -- Done with the call to CoCreateGuid, so clear the DLLs from memory
CLEAR DLLS


return cDataAll


*******************

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

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close