用 VC 写 CRC32 DLL 给VB调用

王朝vb·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

建立 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 运算。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航