Forum: PC-Programmierung ZIP32.dll example - es bleiben Handles stehen


von max .. (vbc2011)


Lesenswert?

Hallo,
ich habe mir eine ZIP-DLL besorgt und habe das mitgelieferte Beispiel 
mal laufen lassen - dabei ist mir aufgefallen, dass nach jedem 
ZIp-Vorgang ein Hanlde stehen bleibt (d.h. im Taskmanager erhöhen sich 
die Handles von meinem Programm immer um eins!

Hat jemand eine Idee - woran das liegen kann - es wird doch alles wieder 
geclosed bzw. freigegeben!
1
#ifndef WIN32
2
#  define WIN32
3
#endif
4
#define API
5
6
/* Tell Microsoft Visual C++ 2005 to leave us alone and
7
 * let us use standard C functions the way we're supposed to.
8
 */
9
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
10
#  ifndef _CRT_SECURE_NO_DEPRECATE
11
#    define _CRT_SECURE_NO_DEPRECATE
12
#  endif
13
#  ifndef _CRT_NONSTDC_NO_DEPRECATE
14
#    define _CRT_NONSTDC_NO_DEPRECATE
15
#  endif
16
#endif
17
18
#include <sys/types.h>
19
#include <sys/stat.h>
20
#include <time.h>
21
#include <string.h>
22
#ifdef __BORLANDC__
23
#include <dir.h>
24
#else
25
#include <direct.h>
26
#endif
27
#include "example.h"
28
#include "../../revision.h"
29
/* printf get redirected by a macro define in api.h !!! */
30
#ifdef printf
31
# undef printf
32
#endif
33
34
#ifdef WIN32
35
#include <commctrl.h>
36
#include <winver.h>
37
#else
38
#include <ver.h>
39
#endif
40
41
#ifdef WIN32
42
#define ZIP_DLL_NAME "ZIP32Z64.DLL\0"
43
#else
44
#define ZIP_DLL_NAME "ZIP16.DLL\0"
45
#endif
46
47
#define DLL_WARNING "Cannot find %s."\
48
            " The Dll must be in the application directory, the path, "\
49
            "the Windows directory or the Windows System directory."
50
#define DLL_VERSION_WARNING "%s has the wrong version number."\
51
            " Insure that you have the correct dll's installed, and that "\
52
            "an older dll is not in your path or Windows System directory."
53
54
int hFile;              /* file handle */
55
56
ZCL ZpZCL;
57
LPZIPUSERFUNCTIONS lpZipUserFunctions;
58
HANDLE hZUF = (HANDLE)NULL;
59
HINSTANCE hUnzipDll;
60
HANDLE hFileList;
61
ZPOPT ZpOpt;
62
#ifdef WIN32
63
DWORD dwPlatformId = 0xFFFFFFFF;
64
#endif
65
HINSTANCE hZipDll;
66
67
68
/* Forward References */
69
_DLL_ZIP ZipArchive;
70
_ZIP_USER_FUNCTIONS ZipInit;
71
72
void FreeUpMemory(void);
73
int WINAPI DummyPassword(LPSTR, int, LPCSTR, LPCSTR);
74
int WINAPI DummyPrint(char far *, unsigned long);
75
int WINAPI WINAPI DummyComment(char far *);
76
77
#ifdef WIN32
78
BOOL IsNT(VOID);
79
#endif
80
81
/****************************************************************************
82
83
    FUNCTION: Main(int argc, char **argv)
84
85
****************************************************************************/
86
#ifdef __BORLANDC__
87
#  ifdef WIN32
88
#pragma argsused
89
#  endif
90
#endif
91
int main(int argc, char **argv)
92
{
93
LPSTR szFileList;
94
char **index, *sz;
95
int retcode, i, cc;
96
DWORD dwVerInfoSize;
97
DWORD dwVerHnd;
98
char szFullPath[PATH_MAX];
99
#ifdef WIN32
100
char *ptr;
101
#else
102
HFILE hfile;
103
OFSTRUCT ofs;
104
#endif
105
HANDLE  hMem;         /* handle to mem alloc'ed */
106
107
if (argc < 3)
108
   {
109
   printf("usage: %s [-options] <zipfile> [entry1 [entry2 [...]]] [-xi list]",
110
          "example");
111
   return 0;           /* Exits if not proper number of arguments */
112
   }
113
114
hZUF = GlobalAlloc( GPTR, (DWORD)sizeof(ZIPUSERFUNCTIONS));
115
if (!hZUF)
116
   {
117
   return 0;
118
   }
119
lpZipUserFunctions = (LPZIPUSERFUNCTIONS)GlobalLock(hZUF);
120
121
if (!lpZipUserFunctions)
122
   {
123
   GlobalFree(hZUF);
124
   return 0;
125
   }
126
127
lpZipUserFunctions->print = DummyPrint;
128
lpZipUserFunctions->password = DummyPassword;
129
lpZipUserFunctions->comment = DummyComment;
130
131
/* Let's go find the dll */
132
#ifdef WIN32
133
if (SearchPath(
134
    NULL,               /* address of search path               */
135
    ZIP_DLL_NAME,       /* address of filename                  */
136
    NULL,               /* address of extension                 */
137
    PATH_MAX,           /* size, in characters, of buffer       */
138
    szFullPath,         /* address of buffer for found filename */
139
    &ptr                /* address of pointer to file component */
140
   ) == 0)
141
#else
142
hfile = OpenFile(ZIP_DLL_NAME,  &ofs, OF_SEARCH);
143
if (hfile == HFILE_ERROR)
144
#endif
145
   {
146
   char str[256];
147
   wsprintf (str, DLL_WARNING, ZIP_DLL_NAME);
148
   printf("%s\n", str);
149
   FreeUpMemory();
150
   return 0;
151
   }
152
#ifndef WIN32
153
else
154
   lstrcpy(szFullPath, ofs.szPathName);
155
_lclose(hfile);
156
#endif
157
158
/* Now we'll check the zip dll version information */
159
dwVerInfoSize =
160
    GetFileVersionInfoSize(szFullPath, &dwVerHnd);
161
162
if (dwVerInfoSize)
163
   {
164
   BOOL  fRet, fRetName;
165
   char str[256];
166
   LPSTR   lpstrVffInfo; /* Pointer to block to hold info */
167
   LPSTR lszVer = NULL;
168
   LPSTR lszVerName = NULL;
169
   UINT  cchVer = 0;
170
171
   /* Get a block big enough to hold the version information */
172
   hMem          = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
173
   lpstrVffInfo  = GlobalLock(hMem);
174
175
   /* Get the version information */
176
   GetFileVersionInfo(szFullPath, 0L, dwVerInfoSize, lpstrVffInfo);
177
   fRet = VerQueryValue(lpstrVffInfo,
178
              TEXT("\\StringFileInfo\\040904E4\\FileVersion"),
179
               (LPVOID)&lszVer,
180
               &cchVer);
181
   fRetName = VerQueryValue(lpstrVffInfo,
182
               TEXT("\\StringFileInfo\\040904E4\\CompanyName"),
183
              (LPVOID)&lszVerName,
184
              &cchVer);
185
   if (!fRet || !fRetName ||
186
      (lstrcmpi(lszVer, ZIP_DLL_VERSION) != 0) ||
187
      (lstrcmpi(lszVerName, IZ_COMPANY_NAME) != 0))
188
      {
189
      wsprintf (str, DLL_VERSION_WARNING, ZIP_DLL_NAME);
190
      printf("%s\n", str);
191
      FreeUpMemory();
192
      return 0;
193
      }
194
   /* free memory */
195
   GlobalUnlock(hMem);
196
   GlobalFree(hMem);
197
   }
198
else
199
   {
200
   char str[256];
201
   wsprintf (str, DLL_VERSION_WARNING, ZIP_DLL_NAME);
202
   printf("%s\n", str);
203
   FreeUpMemory();
204
   return 0;
205
   }
206
/* Okay, now we know that the dll exists, and has the proper version
207
 * information in it. We can go ahead and load it.
208
 */
209
hZipDll = LoadLibrary(ZIP_DLL_NAME);
210
#ifndef WIN32
211
if (hZipDll > HINSTANCE_ERROR)
212
#else
213
if (hZipDll != NULL)
214
#endif
215
   {
216
   (_DLL_ZIP)ZipArchive = (_DLL_ZIP)GetProcAddress(hZipDll, "ZpArchive");
217
   if (!ZipArchive)
218
      {
219
      char str[256];
220
      wsprintf (str, "Could not get entry point to %s", ZIP_DLL_NAME);
221
      MessageBox((HWND)NULL, str, "Info-ZIP Example", MB_ICONSTOP | MB_OK);
222
      FreeUpMemory();
223
      return 0;
224
      }
225
   }
226
else
227
   {
228
   char str[256];
229
   wsprintf (str, "Could not load %s", ZIP_DLL_NAME);
230
   printf("%s\n", str);
231
   FreeUpMemory();
232
   return 0;
233
   }
234
235
(_ZIP_USER_FUNCTIONS)ZipInit = (_ZIP_USER_FUNCTIONS)GetProcAddress(hZipDll, "ZpInit");
236
if (!ZipInit)
237
   {
238
   printf("Cannot get address of ZpInit in Zip dll. Terminating...");
239
   FreeLibrary(hZipDll);
240
   FreeUpMemory();
241
   return 0;
242
   }
243
if (!(*ZipInit)(lpZipUserFunctions))
244
   {
245
   printf("Application functions not set up properly. Terminating...");
246
   FreeLibrary(hZipDll);
247
   FreeUpMemory();
248
   return 0;
249
   }
250
251
/* Here is where the action starts */
252
memset(&ZpOpt, 0, sizeof(ZpOpt));
253
ZpOpt.ExcludeBeforeDate = NULL;    /* set to valid Zip date, or NULL */
254
ZpOpt.IncludeBeforeDate = NULL;    /* set to valid Zip date, or NULL */
255
ZpOpt.szRootDir = szFullPath;      /* set to root dir (will cd to), or NULL */
256
ZpOpt.szTempDir = NULL;            /* set to dir for temp files, or NULL */
257
ZpOpt.fUnicode = 0;                /* Unicode flag */
258
ZpOpt.fEncrypt = FALSE;            /* Encrytion flag */
259
ZpOpt.fSystem = FALSE;             /* true to include system/hidden files */
260
ZpOpt.fVolume = FALSE;             /* true if storing volume label */
261
ZpOpt.fExtra = FALSE;              /* true if including extra attributes */
262
ZpOpt.fNoDirEntries = FALSE;       /* true if ignoring directory entries */
263
ZpOpt.fVerbose = FALSE;            /* true if full messages wanted */
264
ZpOpt.fQuiet = FALSE;              /* true if minimum messages wanted */
265
ZpOpt.fCRLF_LF = FALSE;            /* true if translate CR/LF to LF */
266
ZpOpt.fLF_CRLF = FALSE;            /* true if translate LF to CR/LF */
267
ZpOpt.fJunkDir = FALSE;            /* true if junking directory names */
268
ZpOpt.fGrow = FALSE;               /* true if allow appending to zip file */
269
ZpOpt.fForce = FALSE;              /* true if making entries using DOS names */
270
ZpOpt.fMove = FALSE;               /* true if deleting files added or updated */
271
ZpOpt.fDeleteEntries = FALSE;      /* true if deleting files from archive */
272
ZpOpt.fUpdate = FALSE;             /* true if updating zip file--overwrite only
273
                                        if newer */
274
ZpOpt.fFreshen = FALSE;            /* true if freshening zip file--overwrite only */
275
ZpOpt.fJunkSFX = FALSE;            /* true if junking sfx prefix*/
276
ZpOpt.fLatestTime = FALSE;         /* true if setting zip file time to time of
277
                                       latest file in archive */
278
ZpOpt.fComment = FALSE;            /* true if putting comment in zip file */
279
ZpOpt.fOffsets = FALSE;            /* true if updating archive offsets for sfx
280
                                       files */
281
ZpOpt.fPrivilege = 0;
282
ZpOpt.fEncryption = 0;
283
ZpOpt.szSplitSize = NULL;
284
285
ZpOpt.szIncludeList = NULL;
286
ZpOpt.IncludeListCount = 0;
287
ZpOpt.IncludeList = NULL;
288
ZpOpt.szExcludeList = NULL;
289
ZpOpt.ExcludeListCount = 0;
290
ZpOpt.ExcludeList = NULL;
291
292
ZpOpt.fRecurse = 0;           /* subdir recursing mode: 1 = "-r", 2 = "-R" */
293
ZpOpt.fRepair = 0;            /* archive repair mode: 1 = "-F", 2 = "-FF" */
294
ZpOpt.fLevel = '6';           /* Default deflate compression level */
295
ZpOpt.szCompMethod = NULL;
296
for (i = 0; i < 8; i++) {
297
  ZpOpt.fluff[i] = 0;
298
}
299
getcwd(szFullPath, PATH_MAX); /* Set directory to current directory */
300
301
ZpZCL.argc = argc - 2;        /* number of files to archive - adjust for the
302
                                  actual number of file names to be added */
303
ZpZCL.lpszZipFN = argv[1];    /* archive to be created/updated */
304
305
/* Copy over the appropriate portions of argv, basically stripping out argv[0]
306
   (name of the executable) and argv[1] (name of the archive file)
307
 */
308
hFileList = GlobalAlloc( GPTR, 0x10000L);
309
if ( hFileList )
310
   {
311
   szFileList = (char far *)GlobalLock(hFileList);
312
   }
313
index = (char **)szFileList;
314
cc = (sizeof(char *) * ZpZCL.argc);
315
sz = szFileList + cc;
316
317
for (i = 0; i < ZpZCL.argc; i++)
318
    {
319
    cc = lstrlen(argv[i+2]);
320
    lstrcpy(sz, argv[i+2]);
321
    index[i] = sz;
322
    sz += (cc + 1);
323
    }
324
ZpZCL.FNV = (char **)szFileList;  /* list of files to archive */
325
326
/* Go zip 'em up */
327
retcode = ZipArchive(ZpZCL, &ZpOpt);
328
if (retcode != 0)
329
   printf("Error in archiving\n");
330
331
GlobalUnlock(hFileList);
332
GlobalFree(hFileList);
333
FreeUpMemory();
334
FreeLibrary(hZipDll);
335
return 1;
336
}
337
338
void FreeUpMemory(void)
339
{
340
if (hZUF)
341
   {
342
   GlobalUnlock(hZUF);
343
   GlobalFree(hZUF);
344
   }
345
}
346
347
#ifdef WIN32
348
/* This simply determines if we are running on NT */
349
BOOL IsNT(VOID)
350
{
351
if(dwPlatformId != 0xFFFFFFFF)
352
   return dwPlatformId;
353
else
354
/* note: GetVersionEx() doesn't exist on WinNT 3.1 */
355
   {
356
   if(GetVersion() < 0x80000000)
357
      {
358
      dwPlatformId = TRUE;
359
      }
360
   else
361
      {
362
      dwPlatformId = FALSE;
363
      }
364
    }
365
return dwPlatformId;
366
}
367
#endif
368
369
/* Password entry routine - see password.c in the wiz directory for how
370
   this is actually implemented in Wiz. If you have an encrypted file,
371
   this will probably give you great pain. Note that none of the
372
   parameters are being used here, and this will give you warnings.
373
 */
374
int WINAPI DummyPassword(LPSTR p, int n, LPCSTR m, LPCSTR name)
375
{
376
return 1;
377
}
378
379
/* Dummy "print" routine that simply outputs what is sent from the dll */
380
int WINAPI DummyPrint(char far *buf, unsigned long size)
381
{
382
printf("%s", buf);
383
return (unsigned int) size;
384
}
385
386
387
/* Dummy "comment" routine. See comment.c in the wiz directory for how
388
   this is actually implemented in Wiz. This will probably cause you
389
   great pain if you ever actually make a call into it.
390
 */
391
int WINAPI DummyComment(char far *szBuf)
392
{
393
szBuf[0] = '\0';
394
return TRUE;
395
}
(Sorry für den vieln Code - denk der ist aber einfach zu verstehen:)
danke

von Rufus Τ. F. (rufus) Benutzerseite


Lesenswert?

Du könntest das ganze Gestrüpp entwirren, indem Du mit der Importlibrary 
linkst, die zur DLL gehören dürfte. Damit entfällt schon mal der ganze 
LoadLibrary/GetProcAddress-Kram.

Das ganze Geraffel mit der "Userfunctions"-Struktur entfällt dann auch.

Den Versionstest kannst Du auch erstmal weglassen, um einzugrenzen, wo 
da ein Handle offenbleibt.

Obendrein könntest Du den Code lesbarer organisieren, indem Du die 
16-Bit-Unterstützung entfernst. Die braucht man in diesem Jahrtausend 
nun wirklich nicht mehr.

Im übrigens könntest Du mit dem ProcessExplorer nachsehen, was für 
Handles Dein Programm verwendet.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.