用 VC 写 CRC32 DLL 给VB调用

建立 crc32.cpp

代码如下:

---------------------------------------------------------------------------------

//kwanhong Young 2004-9-1

unsigned long CRCTable[256];

/*
int main(int argc, char* argv[])
{
 Init();
 unsigned long r=Update((unsigned char *)("ABCDEFG"), 7,0xFFFFFFFF);
 printf("result is:%d",r);
 return 0;
}
*/

//void Init()
extern "C" void __stdcall Init()
{
 unsigned long crc, i, j;
 unsigned long poly = 0xEDB88320;

 for(i=0; i<256; i++)
 {
  crc = i;
  for(j=8; j>0; j--)
  {
   if(crc & 1)
    crc = (crc >> 1) ^ poly;
   else
    crc >>= 1;
  }
  CRCTable[i] = crc;
 }
}

extern "C" unsigned long __stdcall Update(unsigned char *buffer,long length, unsigned long crc)
{
 //unsigned long crc = 0xFFFFFFFF;
 unsigned char *ptr = buffer;

 while(length > 0)
 {
  crc = ((crc >> 8) & 0x00FFFFFF) ^ CRCTable[(crc ^ *ptr) & 0xFF];
  ptr++;
  length--;
 }
 return crc;
}

创建 crc32.def

代码如下:

---------------------------------------------------------------------------

EXPORTS
 Init
 Update

然后就可以编译成 DLL 了。

现在用VB新建一个 Class。代码如下:

-------------------------------------------------------------------------

 

Private Declare Sub CRC32Init Lib "kw_crc32.dll" Alias "Init" ()
Private Declare Function CRC32Update Lib "kw_crc32.dll" Alias "Update" (db As Any, ByVal length As Long, ByVal crcvalue As Long) As Long

Private m_lValue     As Long

Private Sub Class_Initialize()
    '//make crc32 table
    CRC32Init
    m_lValue = &HFFFFFFFF
End Sub

Public Sub Reset()
    m_lValue = &HFFFFFFFF
End Sub

Public Sub Encode(bytData() As Byte, ByVal iLen As Long)
    m_lValue = CRC32Update(bytData(0), iLen, m_lValue)
End Sub

Public Sub EncodePtr(ByVal ptbytData As Long, ByVal iLen As Long)
'//use data pointer
    m_lValue = CRC32Update(ByVal ptbytData, iLen, m_lValue)
End Sub

Public Function GetValue() As String
    GetValue = Right("00000000" & Hex$(m_lValue Xor &HFFFFFFFF), 8)
End Function

Public Function EncodeString(SourceString As String) As String
'// Function to digest a text string and output the result as a string
'// of hexadecimal characters.
   
    Dim bytData() As Byte
   
    m_lValue = &HFFFFFFFF
   
    If Len(SourceString) = 0 Then
        ReDim bytData(0)
        EncodeString = "00000000"
    Else
        bytData = StrConv(SourceString, vbFromUnicode)
        m_lValue = CRC32Update(bytData(0), UBound(bytData) + 1, m_lValue)
    End If
   
    EncodeString = Right("00000000" & Hex$(m_lValue Xor &HFFFFFFFF), 8)
   
End Function

Public Function SaveState() As String
'//save the state
    SaveState = m_lValue
End Function

Public Function LoadState(s As String) As Boolean
'//load the state
    m_lValue = s
    LoadState = True
End Function

 

现在就可以在VB里面实现高速的CRC32 运算。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值