Current News
Archived News
Search News
Discussion Forum


Old Forum
Install Programs More Downloads...
Troubleshooting
Source Code
Format Specs.
Misc. Information
Non-SF Stuff
Links




1 Attribute VB_Name = "SFmpqapi"
2 Option Explicit
4 '  ShadowFlare MPQ API Library. (c) ShadowFlare Software 2002
6 '  All functions below are actual functions that are part of this
7 '  library and do not need any additional dll files.  It does not
8 '  even require Storm to be able to decompress or compress files.
10 '  This library emulates the interface of Lmpqapi and Storm MPQ
11 '  functions, so it may be used as a replacement for them in
12 '  MPQ extractors/archivers without even needing to recompile
13 '  the program that uses Lmpqapi or Storm.  It has a few features
14 '  not included in Lmpqapi and Storm, such as extra flags for some
15 '  functions, setting the locale ID of existing files, and adding
16 '  files without having to write them somewhere else first.  Also,
17 '  MPQ handles used by functions prefixed with "SFile" and "Mpq"
18 '  can be used interchangably; all functions use the same type
19 '  of MPQ handles.  You cannot, however, use handles from this
20 '  library with storm or lmpqapi or vice-versa.  Doing so will
21 '  most likely result in a crash.
23 '  Revision History:
24 '  20/10/2002 1.07 (ShadowFlare)
25 '  - No longer requires Storm.dll to compress or decompress
26 '    Warcraft III files
27 '  - Added SFileListFiles for getting names and information
28 '    about all of the files in an archive
29 '  - Fixed a bug with renaming and deleting files
31 '  05/09/2002 1.06 (ShadowFlare)
32 '  - Compresses files without Storm.dll!
33 '  - If Warcraft III is installed, this library will be able to
34 '    find Storm.dll on its own. (Storm.dll is needed to
35 '    decompress Warcraft III files)
36 '  - Fixed a bug where an embedded archive and the file that
37 '    contains it would be corrupted if the archive was modified
38 '  - Able to open all .w3m maps now
40 '  29/06/2002 1.05 (ShadowFlare)
41 '  - Supports decompressing files from Warcraft III MPQ archives
42 '    if using Storm.dll from Warcraft III
43 '  - Added MpqAddFileToArchiveEx and MpqAddFileFromBufferEx for
44 '    using extra compression types
46 '  29/05/2002 1.04 (ShadowFlare)
47 '  - Files can be compressed now!
48 '  - Fixed a bug in SFileReadFile when reading data not aligned
49 '    to the block size
50 '  - Optimized some of SFileReadFile's code.  It can read files
51 '    faster now
52 '  - SFile functions may now be used to access files not in mpq
53 '    archives as you can with the real storm functions
54 '  - MpqCompactArchive will no longer corrupt files with the
55 '    MODCRYPTKEY flag as long as the file is either compressed,
56 '    listed in "(listfile)", is "(listfile)", or is located in
57 '    the same place in the compacted archive; so it is safe
58 '    enough to use it on almost any archive
59 '  - Added MpqAddWaveFromBuffer
60 '  - Better handling of archives with no files
61 '  - Fixed compression with COMPRESS2 flag
63 '  15/05/2002 1.03 (ShadowFlare)
64 '  - Supports adding files with the compression attribute (does
65 '    not actually compress files).  Now archives created with
66 '    this dll can have files added to them through lmpqapi
67 '    without causing staredit to crash
68 '  - SFileGetBasePath and SFileSetBasePath work more like their
69 '    Storm equivalents now
70 '  - Implemented MpqCompactArchive, but it is not finished yet.
71 '    In its current state, I would recommend against using it
72 '    on archives that contain files with the MODCRYPTKEY flag,
73 '    since it will corrupt any files with that flag
74 '  - Added SFMpqGetVersionString2 which may be used in Visual
75 '    Basic to get the version string
77 '  07/05/2002 1.02 (ShadowFlare)
78 '  - SFileReadFile no longer passes the lpOverlapped parameter it
79 '    receives to ReadFile.  This is what was causing the function
80 '    to fail when used in Visual Basic
81 '  - Added support for more Storm MPQ functions
82 '  - GetLastError may now be used to get information about why a
83 '    function failed
85 '  01/05/2002 1.01 (ShadowFlare)
86 '  - Added ordinals for Storm MPQ functions
87 '  - Fixed MPQ searching functionality of SFileOpenFileEx
88 '  - Added a check for whether a valid handle is given when
89 '    SFileCloseArchive is called
90 '  - Fixed functionality of SFileSetArchivePriority when multiple
91 '    files are open
92 '  - File renaming works for all filenames now
93 '  - SFileReadFile no longer reallocates the buffer for each block
94 '    that is decompressed.  This should make SFileReadFile at least
95 '    a little faster
97 '  30/04/2002 1.00 (ShadowFlare)
98 '  - First version.
99 '  - Compression not yet supported
100 '  - Does not use SetLastError yet, so GetLastError will not return any
101 '    errors that have to do with this library
102 '  - MpqCompactArchive not implemented
104 '  This library is freeware, you can do anything you want with it but with
105 '  one exception.  If you use it in your program, you must specify this fact
106 '  in Help|About box or in similar way.  You can obtain version string using
107 '  SFMpqGetVersionString call.
109 '  THIS LIBRARY IS DISTRIBUTED "AS IS".  NO WARRANTY OF ANY KIND IS EXPRESSED
110 '  OR IMPLIED. YOU USE AT YOUR OWN RISK. THE AUTHOR WILL NOT BE LIABLE FOR
111 '  DATA LOSS, DAMAGES, LOSS OF PROFITS OR ANY OTHER KIND OF LOSS WHILE USING
112 '  OR MISUSING THIS SOFTWARE.
114 '  Any comments or suggestions are accepted at blakflare@hotmail.com (ShadowFlare)
116 Type SFMPQVERSION
117     Major As Integer
118     Minor As Integer
119     Revision As Integer
120     Subrevision As Integer
121 End Type
123 ' MpqInitialize does nothing.  It is only provided for
124 ' compatibility with MPQ archivers that use lmpqapi.
125 Declare Function MpqInitialize Lib "SFmpq.dll" () As Boolean
127 Declare Function MpqGetVersionString Lib "SFmpq.dll" () As String
128 Declare Function MpqGetVersion Lib "SFmpq.dll" () As Single
130 Declare Sub SFMpqDestroy Lib "SFmpq.dll" () ' This no longer needs to be called.  It is only provided for compatibility with older versions
132 ' SFMpqGetVersionString2's return value is the required length of the buffer plus
133 ' the terminating null, so use SFMpqGetVersionString2(ByVal 0&, 0) to get the length.
134 Declare Function SFMpqGetVersionString Lib "SFmpq.dll" () As String
135 Declare Function SFMpqGetVersionString2 Lib "SFmpq.dll" (ByVal lpBuffer As String, ByVal dwBufferLength As Long) As Long
136 Declare Function SFMpqGetVersion Lib "SFmpq.dll" () As SFMPQVERSION
138 ' General error codes
139 Public Const MPQ_ERROR_MPQ_INVALID As Long = &H85200065
140 Public Const MPQ_ERROR_FILE_NOT_FOUND As Long = &H85200066
141 Public Const MPQ_ERROR_DISK_FULL As Long = &H85200068 'Physical write file to MPQ failed. Not sure of exact meaning
142 Public Const MPQ_ERROR_HASH_TABLE_FULL As Long = &H85200069
143 Public Const MPQ_ERROR_ALREADY_EXISTS As Long = &H8520006A
144 Public Const MPQ_ERROR_BAD_OPEN_MODE As Long = &H8520006C 'When MOAU_READ_ONLY is used without MOAU_OPEN_EXISTING
146 Public Const MPQ_ERROR_COMPACT_ERROR As Long = &H85300001
148 ' MpqOpenArchiveForUpdate flags
149 Public Const MOAU_CREATE_NEW As Long = &H0
150 Public Const MOAU_CREATE_ALWAYS As Long = &H8 'Was wrongly named MOAU_CREATE_NEW
151 Public Const MOAU_OPEN_EXISTING As Long = &H4
152 Public Const MOAU_OPEN_ALWAYS As Long = &H20
153 Public Const MOAU_READ_ONLY As Long = &H10 'Must be used with MOAU_OPEN_EXISTING
154 Public Const MOAU_MAINTAIN_LISTFILE As Long = &H1
156 ' MpqAddFileToArchive flags
157 Public Const MAFA_EXISTS As Long = &H80000000 'Will be added if not present
158 Public Const MAFA_UNKNOWN40000000 As Long = &H40000000
159 Public Const MAFA_MODCRYPTKEY As Long = &H20000
160 Public Const MAFA_ENCRYPT As Long = &H10000
161 Public Const MAFA_COMPRESS As Long = &H200
162 Public Const MAFA_COMPRESS2 As Long = &H100
163 Public Const MAFA_REPLACE_EXISTING As Long = &H1
165 ' MpqAddFileToArchiveEx compression flags
166 Public Const MAFA_COMPRESS_STANDARD As Long = &H8 'Standard PKWare DCL compression
167 Public Const MAFA_COMPRESS_DEFLATE  As Long = &H2 'ZLib's deflate compression
168 Public Const MAFA_COMPRESS_WAVE    As Long = &H81 'Standard wave compression
169 Public Const MAFA_COMPRESS_WAVE2   As Long = &H41 'Unused wave compression
171 ' Flags for individual compression types used for wave compression
172 Public Const MAFA_COMPRESS_WAVECOMP1 As Long = &H80 'Main compressor for standard wave compression
173 Public Const MAFA_COMPRESS_WAVECOMP2 As Long = &H40 'Main compressor for unused wave compression
174 Public Const MAFA_COMPRESS_WAVECOMP3  As Long = &H1 'Secondary compressor for wave compression
176 ' ZLib deflate compression level constants (used with MpqAddFileToArchiveEx and MpqAddFileFromBufferEx)
177 Public Const Z_NO_COMPRESSION         As Long = 0
178 Public Const Z_BEST_SPEED             As Long = 1
179 Public Const Z_BEST_COMPRESSION       As Long = 9
180 Public Const Z_DEFAULT_COMPRESSION  As Long = (-1)
182 ' MpqAddWAVToArchive quality flags
183 Public Const MAWA_QUALITY_HIGH As Long = 1
184 Public Const MAWA_QUALITY_MEDIUM As Long = 0
185 Public Const MAWA_QUALITY_LOW As Long = 2
187 ' SFileGetFileInfo flags
188 Public Const SFILE_INFO_BLOCK_SIZE As Long = &H1 'Block size in MPQ
189 Public Const SFILE_INFO_HASH_TABLE_SIZE As Long = &H2 'Hash table size in MPQ
190 Public Const SFILE_INFO_NUM_FILES As Long = &H3 'Number of files in MPQ
191 Public Const SFILE_INFO_TYPE As Long = &H4 'Is Long a file or an MPQ?
192 Public Const SFILE_INFO_SIZE As Long = &H5 'Size of MPQ or uncompressed file
193 Public Const SFILE_INFO_COMPRESSED_SIZE As Long = &H6 'Size of compressed file
194 Public Const SFILE_INFO_FLAGS As Long = &H7 'File flags (compressed, etc.), file attributes if a file not in an archive
195 Public Const SFILE_INFO_PARENT As Long = &H8 'Handle of MPQ that file is in
196 Public Const SFILE_INFO_POSITION As Long = &H9 'Position of file pointer in files
197 Public Const SFILE_INFO_LOCALEID As Long = &HA 'Locale ID of file in MPQ
198 Public Const SFILE_INFO_PRIORITY As Long = &HB 'Priority of open MPQ
199 Public Const SFILE_INFO_HASH_INDEX As Long = &HC 'Hash index of file in MPQ
201 ' SFileListFiles flags
202 Public Const SFILE_LIST_MEMORY_LIST  As Long = &H1 ' Specifies that lpFilelists is a file list from memory, rather than being a list of file lists
203 Public Const SFILE_LIST_ONLY_KNOWN   As Long = &H2 ' Only list files that the function finds a name for
204 Public Const SFILE_LIST_ONLY_UNKNOWN As Long = &H4 ' Only list files that the function does not find a name for
206 Public Const SFILE_TYPE_MPQ As Long = &H1
207 Public Const SFILE_TYPE_FILE As Long = &H2
209 Public Const INVALID_HANDLE_VALUE As Long = -1
211 Public Const FILE_BEGIN   As Long = 0
212 Public Const FILE_CURRENT As Long = 1
213 Public Const FILE_END     As Long = 2
215 Public Const SFILE_OPEN_HARD_DISK_FILE As Long = &H0 'Open archive without regard to the drive type it resides on
216 Public Const SFILE_OPEN_CD_ROM_FILE As Long = &H1 'Open the archive only if it is on a CD-ROM
217 Public Const SFILE_OPEN_ALLOW_WRITE As Long = &H8000 'Open file with write access
219 Public Const SFILE_SEARCH_CURRENT_ONLY As Long = &H0 'Used with SFileOpenFileEx; only the archive with the handle specified will be searched for the file
220 Public Const SFILE_SEARCH_ALL_OPEN As Long = &H1 'SFileOpenFileEx will look through all open archives for the file
222 Type FILELISTENTRY
223     dwFileExists As Long ' Nonzero if this entry is used
224     lcLocale As Long ' Locale ID of file
225     dwCompressedSize As Long ' Compressed size of file
226     dwFullSize As Long ' Uncompressed size of file
227     dwFlags As Long ' Flags for file
228     szFileName(259) As Byte
229 End Type
231 ' Storm functions implemented by this library
232 Declare Function SFileOpenArchive Lib "SFmpq.dll" (ByVal lpFileName As String, ByVal dwPriority As Long, ByVal dwFlags As Long, ByRef hMPQ As Long) As Boolean
233 Declare Function SFileCloseArchive Lib "SFmpq.dll" (ByVal hMPQ As Long) As Boolean
234 Declare Function SFileGetArchiveName Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpBuffer As String, ByVal dwBufferLength As Long) As Boolean
235 Declare Function SFileOpenFile Lib "SFmpq.dll" (ByVal lpFileName As String, ByRef hFile As Long) As Boolean
236 Declare Function SFileOpenFileEx Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpFileName As String, ByVal dwSearchScope As Long, ByRef hFile As Long) As Boolean
237 Declare Function SFileCloseFile Lib "SFmpq.dll" (ByVal hFile As Long) As Boolean
238 Declare Function SFileGetFileSize Lib "SFmpq.dll" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
239 Declare Function SFileGetFileArchive Lib "SFmpq.dll" (ByVal hFile As Long, ByRef hMPQ As Long) As Boolean
240 Declare Function SFileGetFileName Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpBuffer As String, ByVal dwBufferLength As Long) As Boolean
241 Declare Function SFileSetFilePointer Lib "SFmpq.dll" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lplDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
242 Declare Function SFileReadFile Lib "SFmpq.dll" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Boolean
243 Declare Function SFileSetLocale Lib "SFmpq.dll" (ByVal nNewLocale As Long) As Long
244 Declare Function SFileGetBasePath Lib "SFmpq.dll" (ByVal lpBuffer As String, ByVal dwBufferLength As Long) As Boolean
245 Declare Function SFileSetBasePath Lib "SFmpq.dll" (ByVal lpNewBasePath As String) As Boolean
247 ' Extra storm-related functions
248 Declare Function SFileGetFileInfo Lib "SFmpq.dll" (ByVal hFile As Long, ByVal dwInfoType As Long) As Long
249 Declare Function SFileSetArchivePriority Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal dwPriority As Long) As Boolean
250 Declare Function SFileFindMpqHeader Lib "SFmpq.dll" (ByVal hFile As Long) As Long
251 Declare Function SFileListFiles Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpFileLists As String, ByRef lpListBuffer As FILELISTENTRY, ByVal dwFlags As Long) As Boolean
253 ' Archive editing functions implemented by this library
254 Declare Function MpqOpenArchiveForUpdate Lib "SFmpq.dll" (ByVal lpFileName As String, ByVal dwFlags As Long, ByVal dwMaximumFilesInArchive As Long) As Long
255 Declare Function MpqCloseUpdatedArchive Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal dwUnknown2 As Long) As Long
256 Declare Function MpqAddFileToArchive Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpSourceFileName As String, ByVal lpDestFileName As String, ByVal dwFlags As Long) As Boolean
257 Declare Function MpqAddWaveToArchive Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpSourceFileName As String, ByVal lpDestFileName As String, ByVal dwFlags As Long, ByVal dwQuality As Long) As Boolean
258 Declare Function MpqRenameFile Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpcOldFileName As String, ByVal lpcNewFileName As String) As Boolean
259 Declare Function MpqDeleteFile Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpFileName As String) As Boolean
260 Declare Function MpqCompactArchive Lib "SFmpq.dll" (ByVal hMPQ As Long) As Boolean
262 ' Extra archive editing functions
263 Declare Function MpqAddFileToArchiveEx Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpSourceFileName As String, ByVal lpDestFileName As String, ByVal dwFlags As Long, ByVal dwCompressionType As Long, ByVal dwCompressLevel As Long) As Boolean
264 Declare Function MpqAddFileFromBufferEx Lib "SFmpq.dll" (ByVal hMPQ As Long, lpBuffer As Any, ByVal dwLength As Long, ByVal lpFileName As String, ByVal dwFlags As Long, ByVal dwCompressionType As Long, ByVal dwCompressLevel As Long) As Boolean
265 Declare Function MpqAddFileFromBuffer Lib "SFmpq.dll" (ByVal hMPQ As Long, lpBuffer As Any, ByVal dwLength As Long, ByVal lpFileName As String, ByVal dwFlags As Long) As Boolean
266 Declare Function MpqAddWaveFromBuffer Lib "SFmpq.dll" (ByVal hMPQ As Long, lpBuffer As Any, ByVal dwLength As Long, ByVal lpFileName As String, ByVal dwFlags As Long, ByVal dwQuality As Long) As Boolean
267 Declare Function MpqSetFileLocale Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpFileName As String, ByVal nOldLocale As Long, ByVal nNewLocale As Long) As Boolean
269 ' These functions do nothing.  They are only provided for
270 ' compatibility with MPQ extractors that use storm.
271 Declare Function SFileDestroy Lib "SFmpq.dll" () As Boolean
272 Declare Sub StormDestroy Lib "SFmpq.dll" ()
274 ' Returns 0 if the dll version is equal to the version your program was compiled
275 ' with, 1 if the dll is newer, -1 if the dll is older.
276 Function SFMpqCompareVersion() As Long
277     Dim ExeVersion As SFMPQVERSION, DllVersion As SFMPQVERSION
278     With ExeVersion
279         .Major = 1
280         .Minor = 0
281         .Revision = 7
282         .Subrevision = 3
283     End With
284     DllVersion = SFMpqGetVersion()
285     If DllVersion.Major > ExeVersion.Major Then
286         SFMpqCompareVersion = 1
287         Exit Function
288     ElseIf DllVersion.Major < ExeVersion.Major Then
289         SFMpqCompareVersion = -1
290         Exit Function
291     End If
292     If DllVersion.Minor > ExeVersion.Minor Then
293         SFMpqCompareVersion = 1
294         Exit Function
295     ElseIf DllVersion.Minor < ExeVersion.Minor Then
296         SFMpqCompareVersion = -1
297         Exit Function
298     End If
299     If DllVersion.Revision > ExeVersion.Revision Then
300         SFMpqCompareVersion = 1
301         Exit Function
302     ElseIf DllVersion.Revision < ExeVersion.Revision Then
303         SFMpqCompareVersion = -1
304         Exit Function
305     End If
306     If DllVersion.Subrevision > ExeVersion.Subrevision Then
307         SFMpqCompareVersion = 1
308         Exit Function
309     ElseIf DllVersion.Subrevision < ExeVersion.Subrevision Then
310         SFMpqCompareVersion = -1
311         Exit Function
312     End If
313     SFMpqCompareVersion = 0
314 End Function