vb的GUID生成算法

博客提供了VB的GUID生成算法的源代码。该函数若成功则返回GUID,否则返回空字符串,且返回的GUID无“-”字符,同时给出了添加“-”的修改说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gif源代码推荐:vb的GUID生成算法
None.gif
None.gif
'RETURNS:  GUID if successful; blank string otherwise. 
None.gif'
Unlike the GUIDS in the registry, this function returns GUID 
None.gif'
without "-" characters.  See comments for how to modify if you 
None.gif'
want the dash. 
None.gif

None.gif
Public Function GUID() As String 
None.gif    
Dim lRetVal As Long 
None.gif    
Dim udtGuid As GUID 
None.gif     
None.gif    
Dim sPartOne As String 
None.gif    
Dim sPartTwo As String 
None.gif    
Dim sPartThree As String 
None.gif    
Dim sPartFour As String 
None.gif    
Dim iDataLen As Integer 
None.gif    
Dim iStrLen As Integer 
None.gif    
Dim iCtr As Integer 
None.gif    
Dim sAns As String 
None.gif    
None.gif    
On Error GoTo errorhandler 
None.gif    sAns 
= "" 
None.gif     
None.gif    lRetVal 
= CoCreateGuid(udtGuid) 
None.gif     
None.gif    
If lRetVal = 0 Then 
None.gif     
None.gif       
'First 8 chars 
None.gif
        sPartOne = Hex$(udtGuid.PartOne) 
None.gif        iStrLen 
= Len(sPartOne) 
None.gif        iDataLen 
= Len(udtGuid.PartOne) 
None.gif        sPartOne 
= String((iDataLen * 2- iStrLen, "0") _ 
None.gif        
& Trim$(sPartOne) 
None.gif         
None.gif        
'Next 4 Chars 
None.gif
        sPartTwo = Hex$(udtGuid.PartTwo) 
None.gif        iStrLen 
= Len(sPartTwo) 
None.gif        iDataLen 
= Len(udtGuid.PartTwo) 
None.gif        sPartTwo 
= String((iDataLen * 2- iStrLen, "0") _ 
None.gif        
& Trim$(sPartTwo) 
None.gif            
None.gif        
'Next 4 Chars 
None.gif
        sPartThree = Hex$(udtGuid.PartThree) 
None.gif        iStrLen 
= Len(sPartThree) 
None.gif        iDataLen 
= Len(udtGuid.PartThree) 
None.gif        sPartThree 
= String((iDataLen * 2- iStrLen, "0") _ 
None.gif        
& Trim$(sPartThree)   'Next 2 bytes (4 hex digits) 
None.gif
            
None.gif        
'Final 16 chars 
None.gif
        For iCtr = 0 To 7 
None.gif            sPartFour 
= sPartFour & _ 
None.gif            Format$(
Hex$(udtGuid.PartFour(iCtr)), "00"
None.gif        
Next 
None.gif
None.gif     
'To create GUID with "-", change line below to: 
None.gif
     'sAns = sPartOne & "-" & sPartTwo & "-" & sPartThree _ 
None.gif
     '& "-" & sPartFour 
None.gif
        
None.gif       sAns 
= sPartOne & sPartTwo & sPartThree & sPartFour 
None.gif             
None.gif        
End If 
None.gif         
None.gif        GUID 
= sAns 
None.gif
Exit Function 
None.gif
None.gif
None.giferrorhandler: 
None.gif
'return a blank string if there's an error 
None.gif
Exit Function 
None.gif
End Function 
None.gif

转载于:https://www.cnblogs.com/todd/archive/2005/02/15/104550.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值