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

  1. /*
  2.    This is a very simplistic example of how to load and make a call into the
  3.    dll. This has been compiled and tested for a 32-bit console version, but
  4.    not under 16-bit windows. However, the #ifdef's have been left in for the
  5.    16-bit code, simply as an example.
  6.  
  7.  */
  8.  
  9. #define WIN32
  10.  
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <time.h>
  14. #include <string.h>
  15. #include "example.h"
  16. #include "unzver.h"
  17. #ifdef WIN32
  18. #include <winver.h>
  19. #else
  20. #include <ver.h>
  21. #endif
  22.  
  23. #ifdef WIN32
  24. #define UNZ_DLL_NAME "UNZIP32.DLL\0"
  25. #else
  26. #define UNZ_DLL_NAME "UNZIP16.DLL\0"
  27. #endif
  28.  
  29. #define DLL_WARNING "Cannot find %s."\
  30.             " The Dll must be in the application directory, the path, "\
  31.             "the Windows directory or the Windows System directory."
  32. #define DLL_VERSION_WARNING "%s has the wrong version number."\
  33.             " Insure that you have the correct dll's installed, and that "\
  34.             "an older dll is not in your path or Windows System directory."
  35.  
  36. int hFile;              /* file handle */
  37.  
  38. LPUSERFUNCTIONS lpUserFunctions;
  39. HANDLE hUF = (HANDLE)NULL;
  40. LPDCL lpDCL = NULL;
  41. HANDLE hDCL = (HANDLE)NULL;
  42. HINSTANCE hUnzipDll;
  43. HANDLE hZCL = (HANDLE)NULL;
  44. #ifdef WIN32
  45. DWORD dwPlatformId = 0xFFFFFFFF;
  46. #endif
  47.  
  48.  
  49. /* Forward References */
  50. int WINAPI DisplayBuf(char far *, unsigned long);
  51. int WINAPI GetReplaceDlgRetVal(char *);
  52. int WINAPI password(char *, int, const char *, const char *);
  53. void WINAPI ReceiveDllMessage(unsigned long,unsigned long,
  54.     ush, ush, ush, ush, ush, ush, char, char *, char *, unsigned long, char);
  55. _DLL_UNZIP windll_unzip;
  56. _USER_FUNCTIONS UzInit;
  57. void FreeUpMemory(void);
  58. #ifdef WIN32
  59. BOOL IsNT(VOID);
  60. #endif
  61.  
  62. int main(int argc, char **argv)
  63. {
  64. int exfc, infc;
  65. char **exfv, **infv;
  66. char *x_opt;
  67. DWORD dwVerInfoSize;
  68. DWORD dwVerHnd;
  69. char szFullPath[PATH_MAX];
  70. int retcode;
  71. #ifdef WIN32
  72. char *ptr;
  73. #else
  74. HFILE hfile;
  75. OFSTRUCT ofs;
  76. #endif
  77. HANDLE  hMem;         /* handle to mem alloc'ed */
  78.  
  79. if (argc < 2)   /* We must have an archive to unzip */
  80.    return 0;
  81.  
  82. hDCL = GlobalAlloc( GPTR, (DWORD)sizeof(DCL));
  83. if (!hDCL)
  84.    {
  85.    return 0;
  86.    }
  87. lpDCL = (LPDCL)GlobalLock(hDCL);
  88. if (!lpDCL)
  89.    {
  90.    GlobalFree(hDCL);
  91.    return 0;
  92.    }
  93.  
  94. hUF = GlobalAlloc( GPTR, (DWORD)sizeof(USERFUNCTIONS));
  95. if (!hUF)
  96.    {
  97.    GlobalUnlock(hDCL);
  98.    GlobalFree(hDCL);
  99.    return 0;
  100.    }
  101. lpUserFunctions = (LPUSERFUNCTIONS)GlobalLock(hUF);
  102.  
  103. if (!lpUserFunctions)
  104.    {
  105.    GlobalUnlock(hDCL);
  106.    GlobalFree(hDCL);
  107.    GlobalFree(hUF);
  108.    return 0;
  109.    }
  110.  
  111. lpUserFunctions->password = password;
  112. lpUserFunctions->print = DisplayBuf;
  113. lpUserFunctions->sound = NULL;
  114. lpUserFunctions->replace = GetReplaceDlgRetVal;
  115. lpUserFunctions->SendApplicationMessage = ReceiveDllMessage;
  116.  
  117. /* First we go look for the unzip dll */
  118. #ifdef WIN32
  119. if (SearchPath(
  120.     NULL,               /* address of search path               */
  121.     UNZ_DLL_NAME,       /* address of filename                  */
  122.     NULL,               /* address of extension                 */
  123.     PATH_MAX,           /* size, in characters, of buffer       */
  124.     szFullPath,         /* address of buffer for found filename */
  125.     &ptr                /* address of pointer to file component */
  126.    ) == 0)
  127. #else
  128. hfile = OpenFile(UNZ_DLL_NAME,  &ofs, OF_SEARCH);
  129. if (hfile == HFILE_ERROR)
  130. #endif
  131.    {
  132.    char str[256];
  133.    wsprintf (str, DLL_WARNING, UNZ_DLL_NAME);
  134.    printf("%s\n", str);
  135.    FreeUpMemory();
  136.    return 0;
  137.    }
  138. #ifndef WIN32
  139. else
  140.    lstrcpy(szFullPath, ofs.szPathName);
  141. _lclose(hfile);
  142. #endif
  143.  
  144. /* Now we'll check the unzip dll version information. Note that this is
  145.    not the same information as is returned from a call to UzpVersion()
  146.  */
  147. dwVerInfoSize =
  148.     GetFileVersionInfoSize(szFullPath, &dwVerHnd);
  149.  
  150. if (dwVerInfoSize)
  151.    {
  152.    BOOL  fRet, fRetName;
  153.    char str[256];
  154.    LPSTR   lpstrVffInfo; /* Pointer to block to hold info */
  155.    LPSTR lszVer = NULL;
  156.    LPSTR lszVerName = NULL;
  157.    UINT  cchVer = 0;
  158.  
  159.    /* Get a block big enough to hold the version information */
  160.    hMem          = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
  161.    lpstrVffInfo  = GlobalLock(hMem);
  162.  
  163.    /* Get the version information */
  164.    if (GetFileVersionInfo(szFullPath, 0L, dwVerInfoSize, lpstrVffInfo))
  165.       {
  166.       fRet = VerQueryValue(lpstrVffInfo,
  167.                TEXT("\\StringFileInfo\\040904E4\\FileVersion"),
  168.               (LPVOID)&lszVer,
  169.               &cchVer);
  170.       fRetName = VerQueryValue(lpstrVffInfo,
  171.                TEXT("\\StringFileInfo\\040904E4\\CompanyName"),
  172.                (LPVOID)&lszVerName,
  173.                &cchVer);
  174.       if (!fRet || !fRetName ||
  175.          (lstrcmpi(lszVer, UNZ_DLL_VERSION) != 0) ||
  176.          (lstrcmpi(lszVerName, COMPANY_NAME) != 0))
  177.          {
  178.          wsprintf (str, DLL_VERSION_WARNING, UNZ_DLL_NAME);
  179.          printf("%s\n", str);
  180.          FreeUpMemory();
  181.          GlobalUnlock(hMem);
  182.          GlobalFree(hMem);
  183.          return 0;
  184.          }
  185.       }
  186.       /* free memory */
  187.    GlobalUnlock(hMem);
  188.    GlobalFree(hMem);
  189.    }
  190. else
  191.    {
  192.    char str[256];
  193.    wsprintf (str, DLL_VERSION_WARNING, UNZ_DLL_NAME);
  194.    printf("%s\n", str);
  195.    FreeUpMemory();
  196.    return 0;
  197.    }
  198. /* Okay, now we know that the dll exists, and has the proper version
  199.  * information in it. We can go ahead and load it.
  200.  */
  201. hUnzipDll = LoadLibrary(UNZ_DLL_NAME);
  202. #ifndef WIN32
  203. if (hUnzipDll > HINSTANCE_ERROR)
  204. #else
  205. if (hUnzipDll != NULL)
  206. #endif
  207.    {
  208.    (_DLL_UNZIP)windll_unzip = (_DLL_UNZIP)GetProcAddress(hUnzipDll, "windll_unzip");
  209.    }
  210. else
  211.    {
  212.    char str[256];
  213.    wsprintf (str, "Could not load %s", UNZ_DLL_NAME);
  214.    printf("%s\n", str);
  215.    FreeUpMemory();
  216.    return 0;
  217.    }
  218.  
  219. /*
  220.    Here is where the actual extraction process begins. First we set up the
  221.    flags to be passed into the dll.
  222.  */
  223. lpDCL->ncflag = 0; /* Write to stdout if true */
  224. lpDCL->fQuiet = 0; /* We want all messages.
  225.                       1 = fewer messages,
  226.                       2 = no messages */
  227. lpDCL->ntflag = 0; /* test zip file if true */
  228. lpDCL->nvflag = 0; /* give a verbose listing if true */
  229. lpDCL->nUflag = 0; /* Do not extract only newer */
  230. lpDCL->nzflag = 0; /* display a zip file comment if true */
  231. lpDCL->ndflag = 1; /* Recreate directories if true */
  232. lpDCL->noflag = 1; /* Over-write all files if true */
  233. lpDCL->naflag = 0; /* Do not convert CR to CRLF */
  234. lpDCL->lpszZipFN = argv[1]; /* The archive name */
  235. lpDCL->lpszExtractDir = NULL; /* The directory to extract to. This is set
  236.                                  to NULL if you are extracting to the
  237.                                  current directory.
  238.                                */
  239. /*
  240.    As this is a quite short example, intended primarily to show how to
  241.    load and call in to the dll, the command-line parameters are only
  242.    parsed in a very simplistic way:
  243.    We assume that the command-line parameters after the zip archive
  244.    make up a list of file patterns:
  245.    " [file_i1] [file_i2] ... [file_iN] [-x file_x1 [file_x2] ...]".
  246.    We scan for an argument "-x"; all arguments in front are
  247.    "include file patterns", all arguments after are "exclude file patterns".
  248.    If no more arguments are given, we extract ALL files.
  249.  
  250.    In summary, the example program should be run like:
  251.    example <archive.name> [files to include] [-x files to exclude]
  252.    ("<...> denotes mandatory arguments, "[...]" optional arguments)
  253.  */
  254. x_opt = NULL;
  255. if (argc > 2) {
  256.   infv = &argv[2];
  257.   for (infc = 0; infc < argc-2; infc++)
  258.     if (!strcmp("-x", argv[infc+2])) {
  259.         x_opt = infv[infc];
  260.         argv[infc] = NULL;
  261.         break;
  262.     }
  263.   exfc = argc - infc - 3;
  264.   if (exfc > 0)
  265.     exfv = &argv[infc+3];
  266.   else
  267.     exfv = NULL;
  268. } else {
  269.   infc = exfc = 0;
  270.   infv = exfv = NULL;
  271. }
  272. retcode = (*windll_unzip)(infc, infv, exfc, exfv, lpDCL, lpUserFunctions);
  273. if (x_opt) {
  274.   infv[infc] = x_opt;
  275.   x_opt = NULL;
  276. }
  277.  
  278. if (retcode != 0)
  279.    printf("Error unzipping...\n");
  280.  
  281. FreeUpMemory();
  282. FreeLibrary(hUnzipDll);
  283. return 1;
  284. }
  285.  
  286. int WINAPI GetReplaceDlgRetVal(char *filename)
  287. {
  288. /* This is where you will decide if you want to replace, rename etc existing
  289.    files.
  290.  */
  291. return 1;
  292. }
  293.  
  294. void FreeUpMemory(void)
  295. {
  296. if (hDCL)
  297.    {
  298.    GlobalUnlock(hDCL);
  299.    GlobalFree(hDCL);
  300.    }
  301. if (hUF)
  302.    {
  303.    GlobalUnlock(hUF);
  304.    GlobalFree(hUF);
  305.    }
  306. }
  307.  
  308. /* This simply determines if we are running on NT or Windows 95 */
  309. #ifdef WIN32
  310. BOOL IsNT(VOID)
  311. {
  312. if(dwPlatformId != 0xFFFFFFFF)
  313.    return dwPlatformId;
  314. else
  315. /* note: GetVersionEx() doesn't exist on WinNT 3.1 */
  316.    {
  317.    if(GetVersion() < 0x80000000)
  318.       {
  319.       (BOOL)dwPlatformId = TRUE;
  320.       }
  321.    else
  322.       {
  323.       (BOOL)dwPlatformId = FALSE;
  324.       }
  325.     }
  326. return dwPlatformId;
  327. }
  328. #endif
  329.  
  330. /* This is a very stripped down version of what is done in WiZ. Essentially
  331.    what this function is for is to do a listing of an archive contents. It
  332.    is actually never called in this example, but a dummy procedure had to
  333.    be put in, so this was used.
  334.  */
  335. void WINAPI ReceiveDllMessage(unsigned long ucsize,unsigned long csiz,
  336.         ush cfactor, ush mo, ush dy, ush yr, ush hh, ush mm,
  337.     char c, char *filename, char *methbuf, unsigned long crc, char fCrypt)
  338. {
  339. char psLBEntry[PATH_MAX];
  340. char LongHdrStats[] =
  341.           "%7lu  %7lu %4s  %02u-%02u-%02u  %02u:%02u  %c%s";
  342. char CompFactorStr[] = "%c%d%%";
  343. char CompFactor100[] = "100%%";
  344. char szCompFactor[10];
  345. char sgn;
  346.  
  347. if (csiz > ucsize)
  348.    sgn = '-';
  349. else
  350.    sgn = ' ';
  351. if (cfactor == 100)
  352.    lstrcpy(szCompFactor, CompFactor100);
  353. else
  354.    sprintf(szCompFactor, CompFactorStr, sgn, cfactor);
  355. wsprintf(psLBEntry, LongHdrStats,
  356.       ucsize, csiz, szCompFactor, mo, dy, yr, hh, mm, c, filename);
  357.  
  358. printf("%s\n", psLBEntry);
  359. }
  360.  
  361. /* Password entry routine - see password.c in the wiz directory for how
  362.    this is actually implemented in WiZ. If you have an encrypted file,
  363.    this will probably give you great pain.
  364.  */
  365. int WINAPI password(char *p, int n, const char *m, const char * name)
  366. {
  367. return 1;
  368. }
  369.  
  370. /* Dummy "print" routine that simply outputs what is sent from the dll */
  371. int WINAPI DisplayBuf(char far *buf, unsigned long size)
  372. {
  373. printf("%s", buf);
  374. return (unsigned int) size;
  375. }
  376.  
  377.