Solved

CRC32 + VC++ .Dll

Posted on 2004-08-30
28
580 Views
Last Modified: 2013-11-20
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 :)
0
Comment
Question by:Coolkill
  • 11
  • 9
  • 4
  • +1
28 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11931523
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
 

Author Comment

by:Coolkill
ID: 11931810
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
 
LVL 86

Expert Comment

by:jkr
ID: 11931857
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
 

Author Comment

by:Coolkill
ID: 11931908
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
 
LVL 16

Expert Comment

by:nonubik
ID: 11931953
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
 
LVL 86

Expert Comment

by:jkr
ID: 11931993
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
 

Author Comment

by:Coolkill
ID: 11932075
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
 
LVL 16

Expert Comment

by:nonubik
ID: 11932163
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
 

Author Comment

by:Coolkill
ID: 11932262
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
 
LVL 16

Expert Comment

by:nonubik
ID: 11932421
Try changing to 'Use MFC in a shared Dll' for your newly created 'win32 dll'
0
 
LVL 86

Expert Comment

by:jkr
ID: 11932514
Yup, that's what I meant with 'Using MFC in a dynamic library'.
0
 

Author Comment

by:Coolkill
ID: 11932661
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
 

Author Comment

by:Coolkill
ID: 11932781
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11933254
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11933265
Sorry last lines must be:

    crc ^= 0xFFFFFFFF;
    fclose(fp);         <---- add this line
    return true;
0
 

Author Comment

by:Coolkill
ID: 11933366
How would i return the "output" as, a const char * instead of an unsigned long, can i?

0
 
LVL 16

Expert Comment

by:nonubik
ID: 11933417
Use _ultoa(..)
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 11933477
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11933547
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
 

Author Comment

by:Coolkill
ID: 11933556
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11933584
Please follow my sample test.
0
 

Author Comment

by:Coolkill
ID: 11933877
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11933983
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11934890
It's all OK now?
0
 

Author Comment

by:Coolkill
ID: 11935271
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11935335
What are yor comiple options?
0
 

Author Comment

by:Coolkill
ID: 11935507
/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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11935671
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now