Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. CRC32

CRC32

Scheduled Pinned Locked Moved Managed C++/CLI
c++questionannouncement
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 1002737
    wrote on last edited by
    #1

    Hi, Please see following codes on CRC 32:- CRC32.h #ifndef _CRC_32_H_EA6C0EE0_BC30_11d5_B625_A58C4DF45B22_INCLUDED #define _CRC_32_H_EA6C0EE0_BC30_11d5_B625_A58C4DF45B22_INCLUDED #if MSCVER > 1000 #pragma once #endif #ifndef WINDOWS #include #endif #define WM_CRC_THREAD_DONE WM_APP + 0X2DB1 class CRC_32 { typedef struct tag_CRCstruct { CRC_32 *pCRC_32; TCHAR FileName[MaxPath]; LPBYTE pByte; UINT size; HWND hWnd; HANDLE Thread; } CRCStruct, *LPCRCSTRUCT; public: CRC_32(); DWORD CalcCRC(LPCTSTR FileName, HWND ProgressWnd = NULL); DWORD CalcCRC(LPVOID buffer, UINT Size, HWND ProgressWnd = NULL); private: static DWORD WINAPI CRC32ThreadProc (LPVOID lpVoid); void Calculate (const LPBYTE buffer, UINT Size, ULONG &crc); ULONG Reflect (ULONG ref, char ch); ULONG Table[256]; }; #endif ****************************************************************************** CRC32.cpp #include "CRC2.h" #include #include #include #include #include #define BUFFERSIZE 102400 DWORD WINAPI CRC_32::CRC32ThreadProc(LPVOID lpVoid) { LPCRCStruct pCRCSt = (LPCRCStruct)lpVoid; ULONG CRC = 0xFFFFFFFF; HWND Progress = NULL; if (::IsWindow(pCRCSt->hWnd)) { Progress = pCRCSt->hWnd; ::PostMessage(Progress, PBM_SETPOS, 0,0); ::PostMessage(Progress, PBM_SETRANGE32, 0, 100); } if (pCRCSt->pByte) { for (UINT offset = 0; offset < pCRCSt->size; offset += BUFFERSIZE) { pCRCSt->pCRC_32->Calculate(pCRCSt->pByte + offset, (pCRCSt->size - offset > BUFFERSIZE) ? BUFFERSIZE : (pCRCSt->size - offset), CRC); if (::IsWindow(Progress)) { int percent = offset > pCRCSt->size ? 100 : (int)(((double) offset / (double)pCRCSt->size) * 100); ::PostMessage(Progress, PBM_SETPOS, PERCENT, 0); } } } else if (pCRCSt->FileName) { LONGLONG done = 0; UINT size = BUFFERSIZE; BYTE buffer [BUFFERSIZE]; int theFile = _tsopen(pCRCSt->FileName, _O_RDONLY | _O_SEQUENTIAL | _O_BINARY, _SH_DENYWR); if (theFile != -1) { _lseekie64 (theFile, 0L, SEEK_SET); while (size == BUFFERSIZE) { size = _read(theFile, buffer, BUFFERSIZE); if (size) { pCRCSt->pCRC_32->Calculate(buffer, size, CRC); if (::IsWindow(Progress)) { //update progcess bar done += size; int percent = (int) (((long double)done / (long double)length) * 100); ::PostMessage(Progress,

    M 1 Reply Last reply
    0
    • U User 1002737

      Hi, Please see following codes on CRC 32:- CRC32.h #ifndef _CRC_32_H_EA6C0EE0_BC30_11d5_B625_A58C4DF45B22_INCLUDED #define _CRC_32_H_EA6C0EE0_BC30_11d5_B625_A58C4DF45B22_INCLUDED #if MSCVER > 1000 #pragma once #endif #ifndef WINDOWS #include #endif #define WM_CRC_THREAD_DONE WM_APP + 0X2DB1 class CRC_32 { typedef struct tag_CRCstruct { CRC_32 *pCRC_32; TCHAR FileName[MaxPath]; LPBYTE pByte; UINT size; HWND hWnd; HANDLE Thread; } CRCStruct, *LPCRCSTRUCT; public: CRC_32(); DWORD CalcCRC(LPCTSTR FileName, HWND ProgressWnd = NULL); DWORD CalcCRC(LPVOID buffer, UINT Size, HWND ProgressWnd = NULL); private: static DWORD WINAPI CRC32ThreadProc (LPVOID lpVoid); void Calculate (const LPBYTE buffer, UINT Size, ULONG &crc); ULONG Reflect (ULONG ref, char ch); ULONG Table[256]; }; #endif ****************************************************************************** CRC32.cpp #include "CRC2.h" #include #include #include #include #include #define BUFFERSIZE 102400 DWORD WINAPI CRC_32::CRC32ThreadProc(LPVOID lpVoid) { LPCRCStruct pCRCSt = (LPCRCStruct)lpVoid; ULONG CRC = 0xFFFFFFFF; HWND Progress = NULL; if (::IsWindow(pCRCSt->hWnd)) { Progress = pCRCSt->hWnd; ::PostMessage(Progress, PBM_SETPOS, 0,0); ::PostMessage(Progress, PBM_SETRANGE32, 0, 100); } if (pCRCSt->pByte) { for (UINT offset = 0; offset < pCRCSt->size; offset += BUFFERSIZE) { pCRCSt->pCRC_32->Calculate(pCRCSt->pByte + offset, (pCRCSt->size - offset > BUFFERSIZE) ? BUFFERSIZE : (pCRCSt->size - offset), CRC); if (::IsWindow(Progress)) { int percent = offset > pCRCSt->size ? 100 : (int)(((double) offset / (double)pCRCSt->size) * 100); ::PostMessage(Progress, PBM_SETPOS, PERCENT, 0); } } } else if (pCRCSt->FileName) { LONGLONG done = 0; UINT size = BUFFERSIZE; BYTE buffer [BUFFERSIZE]; int theFile = _tsopen(pCRCSt->FileName, _O_RDONLY | _O_SEQUENTIAL | _O_BINARY, _SH_DENYWR); if (theFile != -1) { _lseekie64 (theFile, 0L, SEEK_SET); while (size == BUFFERSIZE) { size = _read(theFile, buffer, BUFFERSIZE); if (size) { pCRCSt->pCRC_32->Calculate(buffer, size, CRC); if (::IsWindow(Progress)) { //update progcess bar done += size; int percent = (int) (((long double)done / (long double)length) * 100); ::PostMessage(Progress,

      M Offline
      M Offline
      Mikko Puonti
      wrote on last edited by
      #2

      This code doesn't seem to be Managed C++ - just normal Visual C++ code (so proper forum would be Visual C++ forum). But anyway... Here is some quick notes: 1) #include "CRC2.h" --> #include "CRC32.h" ? 2) Also some other includes seem to be missing. I guess that they didn't copy to your message. PBM_SETPOS, PBM_SETRANGE32 --> CommCtrl.h PERCENT --> Unkonwn include _O_RDONLY, _O_SEQUENTIAL, _O_BINARY, _SH_DENYWR --> File constants from fcntl.h _SH_DENYWR --> Sharing constant from share.h _tsopen --> Routine mapping from TCHAR.H _sopen --> open file for sharing from io.h 3) LPCRCStruct --> LPCRCSTRUCT (C++ is case sensitive) 4) MaxPath --> Normally MAX_PATH 5) close(theFile) --> close(theFile); 6) _lseekie64 --> _lseeki64 7) ::PostMessage(Progress, PBM_SETPOS, PERCENT, 0); --> ::PostMessage(Progress, PBM_SETPOS, percent, 0); 8) int percent = (int) (((long double)done / (long double)length) * 100); --> You need to calculate length from somewhere. 9) pCRCSt->hWND --> pCRCSt->hWnd 10) if (Handle --> if (Handle) 11) void Calculate (const LPBYTE buffer, UINT Size, ULONG &crc); --> Where is method definition? My include list in stdafx.h: #include <io.h> #include <tchar.h> #include <windows.h> #include <CommCtrl.h> #include <fcntl.h> #include <share.h> #include <TCHAR.H> Some includes might be unnecessary. These changes should compile files - but I don't have any idea is that doing what it is supposed to do. There is also some ready CRC32 function implementations here in code project. For example: CRC32: Generating a checksum for a file[^]. If you want to develope managed code, you should check out Cyclic Redundancy Check (CRC32) HashAlgorithm[^]. There is also more implementations and examples - just try search.

      A 1 Reply Last reply
      0
      • M Mikko Puonti

        This code doesn't seem to be Managed C++ - just normal Visual C++ code (so proper forum would be Visual C++ forum). But anyway... Here is some quick notes: 1) #include "CRC2.h" --> #include "CRC32.h" ? 2) Also some other includes seem to be missing. I guess that they didn't copy to your message. PBM_SETPOS, PBM_SETRANGE32 --> CommCtrl.h PERCENT --> Unkonwn include _O_RDONLY, _O_SEQUENTIAL, _O_BINARY, _SH_DENYWR --> File constants from fcntl.h _SH_DENYWR --> Sharing constant from share.h _tsopen --> Routine mapping from TCHAR.H _sopen --> open file for sharing from io.h 3) LPCRCStruct --> LPCRCSTRUCT (C++ is case sensitive) 4) MaxPath --> Normally MAX_PATH 5) close(theFile) --> close(theFile); 6) _lseekie64 --> _lseeki64 7) ::PostMessage(Progress, PBM_SETPOS, PERCENT, 0); --> ::PostMessage(Progress, PBM_SETPOS, percent, 0); 8) int percent = (int) (((long double)done / (long double)length) * 100); --> You need to calculate length from somewhere. 9) pCRCSt->hWND --> pCRCSt->hWnd 10) if (Handle --> if (Handle) 11) void Calculate (const LPBYTE buffer, UINT Size, ULONG &crc); --> Where is method definition? My include list in stdafx.h: #include <io.h> #include <tchar.h> #include <windows.h> #include <CommCtrl.h> #include <fcntl.h> #include <share.h> #include <TCHAR.H> Some includes might be unnecessary. These changes should compile files - but I don't have any idea is that doing what it is supposed to do. There is also some ready CRC32 function implementations here in code project. For example: CRC32: Generating a checksum for a file[^]. If you want to develope managed code, you should check out Cyclic Redundancy Check (CRC32) HashAlgorithm[^]. There is also more implementations and examples - just try search.

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Hi, Thanks for the reply. I had implement my codes as per your advise. Few more questions: what do you mean by:- 8) int percent = (int) (((long double)done / (long double)length) * 100); --> You need to calculate length from somewhere. 11) void Calculate (const LPBYTE buffer, UINT Size, ULONG &crc); --> Where is method definition? (any more clear example to explain above two?) After debugging(implemented code as follwoing), I have an error now which is as I underline in my code (prompt me erroe 2065, 'lenght': undeclared identifier, error executing cl.exe). How can I solve this? //header file remain the same as previsous sent #include "CRC32.h" #include #include #include #include #include #include #include #define BUFFERSIZE 102400 DWORD WINAPI CRC_32::CRC32ThreadProc(LPVOID lpVoid) { LPCRCSTRUCT pCRCSt = (LPCRCSTRUCT)lpVoid; ULONG CRC = 0xFFFFFFFF; HWND Progress = NULL; if (::IsWindow(pCRCSt->hWnd)) { Progress = pCRCSt->hWnd; ::PostMessage(Progress, PBM_SETPOS, 0,0); ::PostMessage(Progress, PBM_SETRANGE32, 0, 100); } if (pCRCSt->pByte) { for (UINT offset = 0; offset < pCRCSt->size; offset += BUFFERSIZE) { pCRCSt->pCRC_32->Calculate(pCRCSt->pByte + offset, (pCRCSt->size - offset > BUFFERSIZE) ? BUFFERSIZE : (pCRCSt->size - offset), CRC); if (::IsWindow(Progress)) { int percent = offset > pCRCSt->size ? 100 : (int)(((double) offset / (double)pCRCSt->size) * 100); ::PostMessage(Progress, PBM_SETPOS, percent, 0); } } } else if (pCRCSt->FileName) { LONGLONG done = 0; UINT size = BUFFERSIZE; BYTE buffer [BUFFERSIZE]; int theFile = _tsopen(pCRCSt->FileName, _O_RDONLY | _O_SEQUENTIAL | _O_BINARY, _SH_DENYWR); if (theFile != -1) { _lseeki64 (theFile, 0L, SEEK_SET); while (size == BUFFERSIZE) { size = _read(theFile, buffer, BUFFERSIZE); if (size) { pCRCSt->pCRC_32->Calculate(buffer, size, CRC); if (::IsWindow(Progress)) { //update progcess bar done += size; int percent = (int) (((long double)done / (long double)length) * 100); ::PostMessage(Progress, PBM_SETPOS, percent, 0); } } } close(theFile); } } CRC ^= 0xFFFFFFFF; if (IsWindow(Progress)) ::PostMessage(::GetParent(Progress), WM_CRC_THREAD_DONE, (WPARAM)pCRCSt->Thread, CRC); delete pCRCSt->pByte; delete pCRCSt

        M 1 Reply Last reply
        0
        • A Anonymous

          Hi, Thanks for the reply. I had implement my codes as per your advise. Few more questions: what do you mean by:- 8) int percent = (int) (((long double)done / (long double)length) * 100); --> You need to calculate length from somewhere. 11) void Calculate (const LPBYTE buffer, UINT Size, ULONG &crc); --> Where is method definition? (any more clear example to explain above two?) After debugging(implemented code as follwoing), I have an error now which is as I underline in my code (prompt me erroe 2065, 'lenght': undeclared identifier, error executing cl.exe). How can I solve this? //header file remain the same as previsous sent #include "CRC32.h" #include #include #include #include #include #include #include #define BUFFERSIZE 102400 DWORD WINAPI CRC_32::CRC32ThreadProc(LPVOID lpVoid) { LPCRCSTRUCT pCRCSt = (LPCRCSTRUCT)lpVoid; ULONG CRC = 0xFFFFFFFF; HWND Progress = NULL; if (::IsWindow(pCRCSt->hWnd)) { Progress = pCRCSt->hWnd; ::PostMessage(Progress, PBM_SETPOS, 0,0); ::PostMessage(Progress, PBM_SETRANGE32, 0, 100); } if (pCRCSt->pByte) { for (UINT offset = 0; offset < pCRCSt->size; offset += BUFFERSIZE) { pCRCSt->pCRC_32->Calculate(pCRCSt->pByte + offset, (pCRCSt->size - offset > BUFFERSIZE) ? BUFFERSIZE : (pCRCSt->size - offset), CRC); if (::IsWindow(Progress)) { int percent = offset > pCRCSt->size ? 100 : (int)(((double) offset / (double)pCRCSt->size) * 100); ::PostMessage(Progress, PBM_SETPOS, percent, 0); } } } else if (pCRCSt->FileName) { LONGLONG done = 0; UINT size = BUFFERSIZE; BYTE buffer [BUFFERSIZE]; int theFile = _tsopen(pCRCSt->FileName, _O_RDONLY | _O_SEQUENTIAL | _O_BINARY, _SH_DENYWR); if (theFile != -1) { _lseeki64 (theFile, 0L, SEEK_SET); while (size == BUFFERSIZE) { size = _read(theFile, buffer, BUFFERSIZE); if (size) { pCRCSt->pCRC_32->Calculate(buffer, size, CRC); if (::IsWindow(Progress)) { //update progcess bar done += size; int percent = (int) (((long double)done / (long double)length) * 100); ::PostMessage(Progress, PBM_SETPOS, percent, 0); } } } close(theFile); } } CRC ^= 0xFFFFFFFF; if (IsWindow(Progress)) ::PostMessage(::GetParent(Progress), WM_CRC_THREAD_DONE, (WPARAM)pCRCSt->Thread, CRC); delete pCRCSt->pByte; delete pCRCSt

          M Offline
          M Offline
          Mikko Puonti
          wrote on last edited by
          #4
          1. You don't declare and give value for "length". This is why you get error C2065. For example:

            int main()
            {
            // This causes error C2065, because compiler doesn't know
            // identifier "x".
            int i = 100 * x;
            }

          To solve problem:

          int main()
          {
          	// Here we declare (tell compiler that identifier "x" means integer value) and 
          	// define (tell compiler that identifier "x" value is 1) identifier "x"
          	int x = 1;
          	// Now this doesn't cause any problems, because compiler a) knows identifier x and
          	// b) there is assigned value for identifier "x" (actually C++ compiler doesn't know 
          	// this, and forgetting to give actuall value for identifier "x" would cause random 	// runtime behaviour a.k.a. bugs)
          	int i = 100 * x; 
          }
          

          Here is one possible way to calculate length.

          LONGLONG done = 0;
          UINT size = BUFFERSIZE;
          BYTE buffer[BUFFERSIZE];
          
          int theFile = _tsopen(pCRCSt->FileName, _O_RDONLY | _O_SEQUENTIAL | _O_BINARY, _SH_DENYWR);
          if (theFile != -1)
          { 
          	// Getting file length and storing it to length value. 
          	// 
          	// SEEK_END sets position to the end of file. 
          	// 
          	// _lseek returns the offset in a 64-bit integer, in bytes, of the new position 
          	// from the beginning of the file (in this case it returns length of file, 
          	// because we set seek position to END OF FILE).
          	// 
          	long length = _lseeki64( theFile, 0L, SEEK_END );
          	if( length == -1L )
          	{
          		// The function returns –1L to indicate an error and sets errno.
          
          		// Avoid file processing, but close file
          		size = 0L;
          
          		// Specific error handlers
          		switch( errno )
          		{
          			case EBADF:
          				// File descriptor is invalid
          				// TODO: ADD EXTRA ERROR HANDLING
          				// For example: throw exception or show message box for user.
          				break;
          			
          			case EINVAL:
          				// Value for origin is invalid or the position
          				// specified by offset is before the beginning of the file.
          				// TODO: ADD EXTRA ERROR HANDLING
          				// For example: throw exception or show message box for user.
          				break;
          			
          			default:
          				// Unknown error code				
          				// TODO: ADD EXTRA ERROR HANDLING
          				// For example: throw exception or show message box for user.
          				break;			
          		}
          	}
          	if( length <= 0 )
          	{
          		// On devices incapable of 
          		// seeking (such as terminals and printers), the return value 
          		// is undefined.
          		length = 1L;
          	}
          
          
          	// Move file reading position to beginning of the file. 
          	_lseeki64 (theFile, 0L, SEEK_SET); 
          	while (size == BUFFERSIZE)
          	{
          		s
          
          PJ ArendsP 1 Reply Last reply
          0
          • M Mikko Puonti
            1. You don't declare and give value for "length". This is why you get error C2065. For example:

              int main()
              {
              // This causes error C2065, because compiler doesn't know
              // identifier "x".
              int i = 100 * x;
              }

            To solve problem:

            int main()
            {
            	// Here we declare (tell compiler that identifier "x" means integer value) and 
            	// define (tell compiler that identifier "x" value is 1) identifier "x"
            	int x = 1;
            	// Now this doesn't cause any problems, because compiler a) knows identifier x and
            	// b) there is assigned value for identifier "x" (actually C++ compiler doesn't know 
            	// this, and forgetting to give actuall value for identifier "x" would cause random 	// runtime behaviour a.k.a. bugs)
            	int i = 100 * x; 
            }
            

            Here is one possible way to calculate length.

            LONGLONG done = 0;
            UINT size = BUFFERSIZE;
            BYTE buffer[BUFFERSIZE];
            
            int theFile = _tsopen(pCRCSt->FileName, _O_RDONLY | _O_SEQUENTIAL | _O_BINARY, _SH_DENYWR);
            if (theFile != -1)
            { 
            	// Getting file length and storing it to length value. 
            	// 
            	// SEEK_END sets position to the end of file. 
            	// 
            	// _lseek returns the offset in a 64-bit integer, in bytes, of the new position 
            	// from the beginning of the file (in this case it returns length of file, 
            	// because we set seek position to END OF FILE).
            	// 
            	long length = _lseeki64( theFile, 0L, SEEK_END );
            	if( length == -1L )
            	{
            		// The function returns –1L to indicate an error and sets errno.
            
            		// Avoid file processing, but close file
            		size = 0L;
            
            		// Specific error handlers
            		switch( errno )
            		{
            			case EBADF:
            				// File descriptor is invalid
            				// TODO: ADD EXTRA ERROR HANDLING
            				// For example: throw exception or show message box for user.
            				break;
            			
            			case EINVAL:
            				// Value for origin is invalid or the position
            				// specified by offset is before the beginning of the file.
            				// TODO: ADD EXTRA ERROR HANDLING
            				// For example: throw exception or show message box for user.
            				break;
            			
            			default:
            				// Unknown error code				
            				// TODO: ADD EXTRA ERROR HANDLING
            				// For example: throw exception or show message box for user.
            				break;			
            		}
            	}
            	if( length <= 0 )
            	{
            		// On devices incapable of 
            		// seeking (such as terminals and printers), the return value 
            		// is undefined.
            		length = 1L;
            	}
            
            
            	// Move file reading position to beginning of the file. 
            	_lseeki64 (theFile, 0L, SEEK_SET); 
            	while (size == BUFFERSIZE)
            	{
            		s
            
            PJ ArendsP Offline
            PJ ArendsP Offline
            PJ Arends
            wrote on last edited by
            #5

            Hegot the code from http://www.codeproject.com/cpp/crc32_large.asp[^] I do not know why he didn't ask his question in the article forum there, instead of posting partial code here:confused:


            [

            ](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!

            Within you lies the power for good; Use it!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups