HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Using sfmpq.dll in C++

04-09-2008, 02:44 AM#1
midiway
I'm trying to achieve the use of sfmpq.dll in C++, but there is some weird problem that has me completely stuck.

I think that the problem is with memory allocation, but I'm doing all right using new BYTE[size]. I've made a simple program to read files from War3x.mpq, it can reads a small file with some KB and a big one with 2 MB. The program only works if I have this line uncommented inside the read function:
char this_reserve_space_in_data_segmen_and_now_the_program_works[1000]; (found at the end of the code below)

If you comment this line it crash, if you have it uncommented it leaks the buffer memory allocated every time you press a button to read file inside the .mpq (you can see it trough the task mananger).

So, if someone could help me with a better implementation to read files inside mpqs or solve my problem, I've atached the source and the sample program.
Here is where leaves the problem (I think):
PHP Code:
void LoadFilebool whatfile ) {
    if( !
loadedDll )
    {
        
MessageBoxNULL"DLL wasn't loaded""ERROR"MB_OK|MB_ICONEXCLAMATION );
        return;
    }
    
    
    
    
char mpqpath[255] = {0xff};
    
SendMessagehwndEditWarPath, (UINT)EM_GETLINE0, (LPARAM)mpqpath );
    
strcatmpqpathmpqfile );
    
    
    
    
MPQHANDLE hMpq;
    if( !
fpOpenArchive(mpqpath00, &hMpq) )
    {
        
char msg[255] = "Couldn't load ";
        
strcatmsgmpqpath );
        
MessageBoxNULLmsg"ERROR"MB_OK|MB_ICONEXCLAMATION );
        return;
    }



    
MPQHANDLE hFile;
    const 
char *filepath whatfile?bigfile:smallfile;
    if( !
fpOpenFile(hMpqfilepath0, &hFile) )
    {
        
char msg[255] = "Couldn't load ";
        
strcatmsgfilepath );
        
MessageBoxNULLmsg"ERROR"MB_OK|MB_ICONEXCLAMATION );
        return;
    }
    
    
    
    
DWORD size fpGetFileSizehFileNULL );
    
BYTE *buffer = new BYTE[size];
    if( !
buffer )
    {
        
MessageBoxNULL"Couldn't allocate memory""ERROR"MB_OK|MB_ICONEXCLAMATION );
        return;
    }
    
    
    
    if( !
fpReadFile(hFilebuffersizeNULLNULL) )
    {
        
MessageBoxNULL"Couldn't read the file inside mpq""ERROR"MB_OK|MB_ICONEXCLAMATION );
        
char error[10];
        
itoaGetLastError(), error10 );
        
char msg[100] = "GetLastError: ";
        
strcatmsgerror );
        
MessageBoxNULLmsg"ERROR"MB_OK|MB_ICONEXCLAMATION );
        
        
fpCloseFilehFile );
        
fpCloseArchivehMpq );
        
delete [] buffer;
        return;
    }
    
SendMessagehwndEditBox, (UINT)WM_SETTEXT0, (LPARAM)buffer );
    
fpCloseFilehFile );
    
fpCloseArchivehMpq );
    
delete [] buffer;
    
    
// !!!!
        
char this_reserve_space_in_data_segmen_and_now_the_program_works[30];
        
// also only works with this line inside this function
    // !!!!

Attached Files
File type: rarDLLlink.rar (86.6 KB)
04-09-2008, 10:00 AM#2
PitzerMike
I can't spot the error, but here's my version of a library that can extract files from mpq archives.
Attached Files
File type: zipcode.zip (3.7 KB)
04-09-2008, 10:03 AM#3
PitzerMike
Oh, here's my sfmpq function pointer initialization, in case you wanna use that.
It's the default implementation that comes with sfmpq anyway.
Attached Files
File type: zipsfmpq-init.zip (7.8 KB)
04-09-2008, 12:16 PM#4
Vexorian
Quote:
Originally Posted by PitzerMike
I can't spot the error, but here's my version of a library that can extract files from mpq archives.
Gosh pitzermike, can I get one that can add files as well?
04-11-2008, 03:35 PM#5
PitzerMike
Quote:
Originally Posted by Vexorian
Gosh pitzermike, can I get one that can add files as well?

Well in the one I've posted there's also a function that adds a file/buffer to an archive. But the archive needs to be opened without the read-only flag. Other than that I don't have anything.
I also have an mpqrewriter class that copies an existing archive to a new one with adjusted max hashtable size.
04-11-2008, 03:36 PM#6
Vexorian
Quote:
Well in the one I've posted there's also a function that adds a file/buffer to an archive.
I guess I skipped it.

Perhaps I can port it to Linux g++ when I get time.
06-01-2009, 07:18 AM#7
shae marks
You will need either Visual C++ 2003, 2005 or 2008. Visual C++ Express 2008 works fine and is freely available. If you are using a version older than 2008 (or 2005/2003 Pro), you will need the Windows Platform SDK installed and properly configured (use 2008 if you are unsure how).