home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip22.zip / windll / example.c < prev    next >
C/C++ Source or Header  |  1997-10-15  |  10KB  |  371 lines

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