CRC32 + VC++ .Dll

I'm currently in the process of coding a Dll in VC++ that amongest other things, will accept a parameter "filename" and output its CRCCheck, however, after searching numerous times over google. all the sample projects (of which there are few..), are Win32 based, i'm looking for a simple procedure that accepts 1 parameter a filename, and returns 1 parameter, a CRC32 value..

Thanks :)
CoolkillAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
bool get_crc_32(char *filename, char *strCrc)   // reserve proper space for StrCrc before calling (10 bytes)
{
    static unsigned long crc_table[256];
    static bool generated = false;
    unsigned long crc;
    FILE *fp;
    int c;
   
    if (!generated) {
        unsigned long crc, p;
        int    i, j;
   
        for (i = 0; i < 256; i++) {
            crc = i;
            for (j = 8; j > 0; j--) {
                if (crc & 1)
                    crc = (crc >> 1) ^ 0xEDB88320L;
                else
                    crc >>= 1;
            }
            crc_table[i] = crc;
        }
        generated = true;
    }

    if ( !(fp=fopen(filename, "rb")))
        return false;

    crc = 0xFFFFFFFF;
    while ((c = getc(fp)) != EOF)
        crc = (crc>>8) ^ crc_table[ (crc^c) & 0xFF ];
    crc ^= 0xFFFFFFFF;

    fclose(fp);
    sprintf(strCrc, "%08X", crc);   // <--- generate an 8 digits hexadecimal string

    return true;
}
0
 
jkrCommented:
Check out http://www.codeproject.com/cpp/crc32.asp ("CRC32: Generating a checksum for a file"). Using it is as simple as

// Using the static class is as easy as one line of code
dwErrorCode = CCrc32Static::FileCrc32Assembly(m_strFilename, dwCrc32);

// Whereas there is more involved when using the dynamic class
CCrc32Dynamic *pobCrc32Dynamic = new CCrc32Dynamic;
pobCrc32Dynamic->Init();
dwErrorCode = pobCrc32Dynamic->FileCrc32Assembly(m_strFilename, dwCrc32);
pobCrc32Dynamic->Free();
delete pobCrc32Dynamic;
0
 
CoolkillAuthor Commented:
Do I just require;

CRC32Dynamic.h
CRC32Dynamic.cpp
Common.h
CRC32.cpp

As i dont require any of the Win32 Coding thats in the source.

Thanks
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jkrCommented:
Yes, that's all you need. BTW, why are you reluctant to use Win32 code? That's what e.g. MFC is based on...
0
 
CoolkillAuthor Commented:
In an attempt to keep filesize down, I'm trying to herd out any unrequired coding, as all i require is a very basic .DLL to return the data.

Any idea, how to "bypass" LINK : fatal error LNK1104: cannot open file "nafxcwd.lib".

Google doesnt seem to know.

Eamonn.
0
 
nonubikCommented:
nafxcwd.lib is the MFC Static-Link Library, Debug version. Try to link dynamicaly with MFC.
Or you can find it under <MSVC path>\vc98\mfc\lib for VC++ 6.0
0
 
jkrCommented:
If that file is not present, your compiler installation is somewhat screwed up. nonubik is right, this is part of the 'static' MFC setup. Go to your project settings, pick the 'MFC' tab and set 'Using MFC in a dynamic library'.
0
 
CoolkillAuthor Commented:
There is no nafxcwd.lib in MSVC/vc98/mfc/lib/ - although there are other files, so dunno whats gone wrong there, as for Dynamic Library, i dont even have an MFC Tab so I dont know whats gone wrong here..

Would it be better to create a Win32 Dynamic-link library, or would I have the same issues.
0
 
nonubikCommented:
If you're using VC++6.0 the MFC settigns are under Project settings->'General' tab -> 'Microsoft foundation classes' combo.
And it's not good that you don't have nafxcwd.lib under MSVC/vc98/mfc/lib/
0
 
CoolkillAuthor Commented:
The odd thing is that if I download the sourcecode from the above url^, and 'Rebuild all' it compiles wonderfully, if i then open a new workspace as a 'Win32 DLL' and import all the source files in and compile that, then i get the above errors, as i'm not sure if I can directly change the source download to a win32dll without creating a new project.

Thanks.
0
 
nonubikCommented:
Try changing to 'Use MFC in a shared Dll' for your newly created 'win32 dll'
0
 
jkrCommented:
Yup, that's what I meant with 'Using MFC in a dynamic library'.
0
 
CoolkillAuthor Commented:
Well for some reason it did say Using MFC in the first place, but deselecting/reselecting it seemed to have done the trick, now all the source files compile without error, the Question is how do i get it to act like a .dll not an .exe, since the CRC32.cpp contains the calls to open the dialog/ect still, what files do i need and what files can i do without so i can jus call the CRC32 via;

extern "C" {

      typedef struct {
            DWORD  mVersion;
            HWND   mHwnd;
            BOOL   mKeep;
      } LOADINFO;

      int WINAPI version(HWND mWnd,HWND aWnd,char *data,char*,BOOL,BOOL)
{
....HERE...
      return 3;
}
 
0
 
CoolkillAuthor Commented:
Just to eleaborate, i've got;

CString      m_strFilename;
DWORD dwCrc32, dwErrorCode = NO_ERROR;
dwErrorCode = CCrc32Static::FileCrc32Assembly("C:\\backupinvest.zip", dwCrc32);

 lstrcpy(data,dwErrorCode);

Which, I assume would work, if only I didnt get a "cannot convert parameter 2 from 'unsigned long' to 'const char *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast" - what would be a better way to output it?
0
 
Jaime OlivaresSoftware ArchitectCommented:
Hi Coolkill,
Here is an all-in-one crc32 calculator. It is not MFC dependant. Just use as:

unsigned long crc;
bool result = get_crc_32("yourfilenamehere", crc);

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

bool get_crc_32(char *filename, unsigned long &crc)
{
    static unsigned long crc_table[256];
    static bool generated = false;
    FILE *fp;
    int c;
   
    if (!generated) {
        unsigned long crc, p;
        int    i, j;
   
        for (i = 0; i < 256; i++) {
            crc = i;
            for (j = 8; j > 0; j--) {
                if (crc & 1)
                    crc = (crc >> 1) ^ 0xEDB88320L;
                else
                    crc >>= 1;
            }
            crc_table[i] = crc;
        }
        generated = true;
    }

    if ( !(fp=fopen(filename, "rb")))
        return false;

    crc = 0xFFFFFFFF;
    while ((c = getc(fp)) != EOF)
        crc = (crc>>8) ^ crc_table[ (crc^c) & 0xFF ];

    crc ^= 0xFFFFFFFF;
    return true;
}
0
 
Jaime OlivaresSoftware ArchitectCommented:
Sorry last lines must be:

    crc ^= 0xFFFFFFFF;
    fclose(fp);         <---- add this line
    return true;
0
 
CoolkillAuthor Commented:
How would i return the "output" as, a const char * instead of an unsigned long, can i?

0
 
nonubikCommented:
Use _ultoa(..)
0
 
Jaime OlivaresSoftware ArchitectCommented:
Test it this way:

char crc[10];
bool result = get_crc_32("yourfilenamehere", crc);
if (!result)
    printf("Error openning file\n");
else
    printf("CRC: %s\n", crc);

0
 
CoolkillAuthor Commented:
Sowwies, still getting,

Error C2664: 'lstrcpyA' : cannot convert parameter 2 from 'bool' to 'const char *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
0
 
Jaime OlivaresSoftware ArchitectCommented:
Please follow my sample test.
0
 
CoolkillAuthor Commented:
That works WONDERFULLY. ! :) ty.

Just a final Q, what is the best method of making the dll as small as possible.

Settings -> c/c++ - Optimization is all greyed out.

Thanks.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Don't know why optimizations are grayed. Maybe you have a "Debug" configuration. First of all build a "Release" configuration (use Build->Set active configuration...). Also try to use as few different object classes as possible.
0
 
Jaime OlivaresSoftware ArchitectCommented:
It's all OK now?
0
 
CoolkillAuthor Commented:
No Optimize is still greyed out, if you have anymore ideas i'd be greatful but I'll award the points now since you got what i requested, thanks very much.
0
 
Jaime OlivaresSoftware ArchitectCommented:
What are yor comiple options?
0
 
CoolkillAuthor Commented:
/nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "NEWDLL_EXPORTS" /D "_WINDLL" /D "_AFXDLL" /Fp"Release/NewDll.pch" /YX /Fo"Release/" /Fd"Release/" /FD /c
0
 
Jaime OlivaresSoftware ArchitectCommented:
I guess your optimizations are appearing grayed because you have "Default optimizations" selected.
Don't worry about that, go to "General" compiler options (the page where "Warning level" appears). To the left you will find an "Optimizations" combobox. Choose "Minimize size".

Or simple replace the /O2 with /O1
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.