//VS2005、VC6下編譯通過,CRC校正的函數及相關內容是網上找的,自己稍微改動下,寫了個校正檔案用的調用樣本</p><p>#include<windows.h></p><p>#define CRC32_POLYNOMIAL 0xEDB88320</p><p>unsigned long tableCRC32[256];</p><p>void InitializeTableCRC32();<br />unsigned long GetCRC32(PTCHAR buffer, unsigned long length);<br />unsigned long GetFileCRC32(const PTCHAR szFileName);</p><p>int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PTCHAR szCmdLine,int iCmdShow)<br />{<br /> TCHAR szTemp[256];<br /> InitializeTableCRC32();</p><p> wsprintf(szTemp,TEXT("%08X"),GetFileCRC32(TEXT(".//測試檔案.txt")));<br /> MessageBox(NULL,szTemp,TEXT("標題"),0);<br />}</p><p>void InitializeTableCRC32()<br />{<br /> int i, j;<br /> unsigned long h = 1;<br /> tableCRC32[0] = 0;<br /> for (i = 128; i; i >>= 1)<br /> {<br /> h = (h >> 1) ^ ((h & 1) ? CRC32_POLYNOMIAL : 0);<br /> for (j = 0; j < 256; j += 2 * i)<br /> {<br /> tableCRC32[i + j] = tableCRC32[j] ^ h;<br /> }<br /> }<br />}</p><p>unsigned long GetCRC32(char *buffer, unsigned long length)<br />{<br /> unsigned long crc;<br /> crc = 0xFFFFFFFF;<br /> while (length--)<br /> {<br /> crc = (crc >> 8) ^ tableCRC32[(crc ^ *buffer++) & 0xFF];<br /> }<br /> return crc ^ 0xFFFFFFFF;<br />}</p><p>unsigned long GetFileCRC32(const PTCHAR szFileName)<br />{<br /> HANDLE hFile;<br /> char *buffer;<br /> unsigned long length,bytesReadWrite,crc;</p><p> hFile=CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);//開啟檔案<br /> if(hFile==INVALID_HANDLE_VALUE)<br /> {<br /> return -1;<br /> }<br /> length=GetFileSize(hFile,NULL);<br /> buffer=(char *)VirtualAlloc(NULL,length,MEM_RESERVE|MEM_COMMIT,PAGE_EXECUTE_READWRITE);<br /> if(!ReadFile(hFile,buffer,length,&bytesReadWrite,NULL))<br /> {<br /> return -1;<br /> }<br /> CloseHandle(hFile);//關閉檔案<br /> crc=GetCRC32(buffer,length);<br /> VirtualFree(buffer,0,MEM_RELEASE);</p><p> return crc;<br />}