Office中国论坛/Access中国论坛
标题:
【源码通用函数】一个生成同步复制ID的函数(即GUID,全球同步唯一标识符)
[打印本页]
作者:
红尘如烟
时间:
2010-12-11 19:27
标题:
【源码通用函数】一个生成同步复制ID的函数(即GUID,全球同步唯一标识符)
同步复制ID,即GUID(全球同步唯一标识符),通过特定的算法,生成的一个128位的字符串,在纳秒级的时间内,任意两台电脑生成的GUID都不会相同(理论上在公元3400年以前如此),从而确保标识的唯一性。
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Declare Function CoCreateGuid Lib "ole32.dll" (pguid As GUID) As Long
'生成GUID(同步复制ID) ===红尘如烟=======
Public Function GetGUID() As String
Dim typGUID As GUID
If (CoCreateGuid(typGUID) = 0) Then
GetGUID = GetGUID & String(8 - Len(Hex$(typGUID.Data1)), "0") & Hex$(typGUID.Data1) & "-"
GetGUID = GetGUID & String(4 - Len(Hex$(typGUID.Data2)), "0") & Hex$(typGUID.Data2) & "-"
GetGUID = GetGUID & String(4 - Len(Hex$(typGUID.Data3)), "0") & Hex$(typGUID.Data3) & "-"
GetGUID = GetGUID & IIf((typGUID.Data4(0) < &H10), "0", "") & Hex$(typGUID.Data4(0))
GetGUID = GetGUID & IIf((typGUID.Data4(1) < &H10), "0", "") & Hex$(typGUID.Data4(1)) & "-"
GetGUID = GetGUID & IIf((typGUID.Data4(2) < &H10), "0", "") & Hex$(typGUID.Data4(2))
GetGUID = GetGUID & IIf((typGUID.Data4(3) < &H10), "0", "") & Hex$(typGUID.Data4(3))
GetGUID = GetGUID & IIf((typGUID.Data4(4) < &H10), "0", "") & Hex$(typGUID.Data4(4))
GetGUID = GetGUID & IIf((typGUID.Data4(5) < &H10), "0", "") & Hex$(typGUID.Data4(5))
GetGUID = GetGUID & IIf((typGUID.Data4(6) < &H10), "0", "") & Hex$(typGUID.Data4(6))
GetGUID = GetGUID & IIf((typGUID.Data4(7) < &H10), "0", "") & Hex$(typGUID.Data4(7))
End If
End Function
复制代码
[attach]44349[/attach]
作者:
tmtony
时间:
2010-12-11 21:25
我也用一个,与红尘差不多,也凑个热闹
Private Type GUID
Data1 As Long
Data2 As Long
Data3 As Long
Data4(8) As Byte
End Type
Private Declare Function CoCreateGuid Lib "ole32.dll" ( _
pguid As GUID) As Long
Private Declare Function StringFromGUID2 Lib "ole32.dll" ( _
rguid As Any, _
ByVal lpstrClsId As Long, _
ByVal cbMax As Long) As Long
Public Function gt_GetGuid() As String
Dim g As GUID
Dim b() As Byte
Dim lSize As Long
Dim lR As Long
CoCreateGuid g
lSize = 40
ReDim b(0 To (lSize * 2) - 1) As Byte
lR = StringFromGUID2(g, VarPtr(b(0)), lSize)
gt_GetGuid = Left$(b, lR - 1)
End Function
作者:
82077802
时间:
2010-12-12 08:23
不错,收藏
作者:
红尘如烟
时间:
2010-12-12 15:48
tmtony 发表于 2010-12-11 21:25
我也用一个,与红尘差不多,也凑个热闹
赞一个,老大这个比我用的效率要高的多
作者:
tmtony
时间:
2010-12-13 12:00
GUID就是太长了点, 红尘常用吗? 我一般是需要同步的才用它, 但内部操作还是用长整型
作者:
t小宝
时间:
2010-12-14 15:55
暂时没有用到,收藏起来
欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/)
Powered by Discuz! X3.3