MPQ Archives

The LMPQAPI library



The LMPQAPI library

This library was written by a russian programmer Andrey Lelikov (called also Lelik). It is a small DLL, giving interface to both Storm.dll and StarEdit.exe functions. This library can be downloaded from the Downloads section.

What has StarEdit.exe with a MPQ to do ?

Everyone, who has played the game of StarCraft will tell you, that StarEdit.exe is a map and campaign editor for the game. These maps are stored as the SCM or SCX files. If you open a SCM or SCX file, you will find the three letters "MPQ" at the begin of the file. This means that a map in StarCraft is a MPQ archive. This also means that StarEdit.exe is able to create and edit MPQ archives. What cannot Storm.dll do, can the StarEdit.

Using StarEdit

Before we compile the first application, editing MPQ archives using StarEdit, it is necessary to solve a problem. StarEdit is not a DLL. So it is impossible to load StarEdit.exe using LoadLibrary and get address of a function with GetProcAddress and simply call it. Using StarEdit functions requires a dirty hacking, based on:

The following contitions must be true to make this method successfull:


MpqInitialize

Function MpqInitialize prepares all necessary things to use StarEdit's functions for editing MPQ archives. This function must be called before any other LMPQAPI method will be used.

BOOL WINAPI MpqInitialize();
Parameters

None

Return value

When the function succeeds, it returns nonzero. If fails, returns FALSE and function GetLastError() returns the detail error code. Except for stardard Windows error codes, function can return the following values:

Remarks

For success, it is necessary:


MpqOpenArchiveForUpdate

Function MpqOpenArchiveForUpdate opens a MPQ archive for editing.

HANDLE WINAPI MpqOpenArchiveForUpdate(
  LPCSTR lpFileName,              // Archive name
  DWORD dwCreationDisposition,    // To open or to create
  DWORD dwHashTableSize           // Hash table size
);
Parameters
hFile
[in] Pointer to archive name which is to be created/open. This parameter may not be NULL, otherwise the function fails.
dwCreationDisposition
[in] Value telling how to open the archive
dwHashTableSize
[in] When a new file will be created, this parameter contains hash table size. This value must be between 16 and 262 144. When opening an existing archive, this parameter is ignored.
Return value

When the function succeeds, it returns a handle to the opened/created MPQ arhive. When the function fails, it returns NULL or INVALID_HANDLE_VALUE. Error code is then returned by GetLastError().


MpqCloseUpdatedArchive

Function MpqCloseUpdatedArchive closes an archive.

BOOL WINAPI MpqCloseUpdatedArchive(
  HANDLE hMPQ,            // Archive handle
  DWORD dwUnknown         // Unknown, must be NULL
);
Parameters
hMPQ
[in] Handle of archive, which has to be closed. Must be a handle obtained by a call of MpqOpenArchiveForUpdate
dwUnknown
[in] Unknown, set it to zero.
Return value

When the function succeeds, it returns nonzero. If fails, returns FALSE and GetLastError() return an error code.


MpqAddFileToArchive

Function MpqAddFileToArchive adds a file into the archive.

BOOL WINAPI MpqAddFileToArchive(
  HANDLE hMPQ,            // Archive handle
  LPCSTR lpSourceFileName,// Name of added file
  LPCSTR lpDestFileName,  // Name of archived file
  DWORD dwFlags           // How to store
);
Parameters
hMPQ
[in] A handle of an archive, where the file will be added. Must be a handle obtained by a call of MpqOpenArchiveForUpdate
lpSourceFileName
[in] A name of a disk file, which has to be added to the archive. Must not be NULL.
lpDestFileName
[in] Name under which the file will be stored in the archive. Must be NULL.
dwFlags
[in] Bit mask which controls the way of storing file in the archive.
Return value

When the function succeeds, it returns nonzero. If fails, returns FALSE and GetLastError() returns the error code.


MpqAddWAVToArchive

Function MpqAddWAVToArchive adds a file into the archive. Unlike MpqAddFileToArchive, this function is designed to add sound files (WAVes) into the archive.

BOOL WINAPI MpqAddWAVToArchive(
  HANDLE hMPQ,            // Archive handle
  LPCSTR lpSourceFileName,// Name of added file
  LPCSTR lpDestFileName,  // Name of archived file
  DWORD dwFlags,          // How to store the file
  DWORD dwQuality         // Quality
);
Parameters
hMPQ
[in] A handle of an archive, where the file will be added. Must be a handle obtained by a call of MpqOpenArchiveForUpdate
lpSourceFileName
[in] A name of a disk file, which has to be added to the archive. Must not be NULL.
lpDestFileName
[in] Name under which the file will be stored in the archive. Must be NULL.
dwFlags
[in] Bit mask which tells how to store the file into the archive.
dwQuality
[in] A value which specifies the quality and compression level of the archived file.
Return value

When the function succeeds, it returns nonzero. If fails, returns FALSE and GetLastError() returns the error code.


MpqDeleteFile

Function MpqDeleteFile removes a file from the archive.

BOOL WINAPI MpqDeleteFile(
  HANDLE hMPQ,            // Archive handle
  LPCSTR lpFileName       // The name of deleted file
);
Parameters
hMPQ
[in] Handle of archive, from where a file will be removed. Must be a handle obtained by a call of MpqOpenArchiveForUpdate
lpFileName
[in] Name of file to remove. Must not be NULL.
Return value

When the function succeeds, it returns nonzero. If fails, returns FALSE and GetLastError() returns the error code.


MpqRenameFile

Function MpqRenameFile renames a file within the archive.

BOOL WINAPI MpqRenameFile(
  HANDLE hMPQ,            // Archive handle
  LPCSTR lpOldFileName,   // Original file name
  LPCSTR lpNewFileName    // New file name
);
Parameters
hMPQ
[in] Handle to an archive, where the file will be renamed. Must be a handle obtained by a call of MpqOpenArchiveForUpdate
lpOldFileName
[in] Original name of the file to be renamed. Must not be NULL.
lpNewFileName
[in] New file name within the archive. Must not be NULL.
Return value

When the function succeeds, it returns nonzero. If fails, returns FALSE and GetLastError() returns the error code.


MpqCompactArchive

Function MpqCompactArchive is useful, when a big number of file additions and/or deletions were done on an archive. The function will rearrange the complete archive from scratch (similar to disk defragmentation).

BOOL WINAPI MpqCompactArchive(
  HANDLE hMPQ,            // Archive handle
  BOOL bFailOnBadFile     // Set to FALSE
);
Parameters
hMPQ
[in] Handle of an archive, which will be rebuilt. Must be a handle obtained by a call of MpqOpenArchiveForUpdate
bFailOnBadFile
[in] ???
Return value

When the function succeeds, it returns nonzero. If fails, returns FALSE and GetLastError() returns the error code.


SFileGetFileInfo

Function SFileGetFileInfo returns informations about a MPQ archive or about a file stored within an archive. This function is not implemented by either Storm.dll or StarEdit.exe

DWORD WINAPI SFileGetFileInfo(
  HANDLE hMPQorFile,      // Archive handle or file handle
  DWORD dwInfoType        // Kind of information
);
Parameters
hMPQorFile
[in] Archive/File handle. This must be a handle obtained by a call of SFileOpenArchive or SFileOpenFileEx.
dwInfoType
[in] Requested information to retrieve.
Return value

Then the function succeeds, it returns the required information. in the case of an error, function returns 0xFFFFFFFF and GetLastError() returns the error code.


Copyright (c) Ladislav Zezula 2003 - 2006