home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip531.zip / windll / example.c < prev    next >
C/C++ Source or Header  |  1997-05-30  |  9KB  |  337 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. DWORD dwVerInfoSize;
  65. DWORD dwVerHnd;
  66. char szFullPath[PATH_MAX];
  67. int retcode;
  68. #ifdef WIN32
  69. char *ptr;
  70. #else
  71. HFILE hfile;
  72. OFSTRUCT ofs;
  73. #endif
  74. HANDLE  hMem;         /* handle to mem alloc'ed */
  75.  
  76. if (argc != 2)  /* We must have an archive to unzip */
  77.    return 0;
  78.  
  79. hDCL = GlobalAlloc( GPTR, (DWORD)sizeof(DCL));
  80. if (!hDCL)
  81.    {
  82.    return 0;
  83.    }
  84. lpDCL = (LPDCL)GlobalLock(hDCL);
  85. if (!lpDCL)
  86.    {
  87.    GlobalFree(hDCL);
  88.    return 0;
  89.    }
  90.  
  91. hUF = GlobalAlloc( GPTR, (DWORD)sizeof(USERFUNCTIONS));
  92. if (!hUF)
  93.    {
  94.    GlobalUnlock(hDCL);
  95.    GlobalFree(hDCL);
  96.    return 0;
  97.    }
  98. lpUserFunctions = (LPUSERFUNCTIONS)GlobalLock(hUF);
  99.  
  100. if (!lpUserFunctions)
  101.    {
  102.    GlobalUnlock(hDCL);
  103.    GlobalFree(hDCL);
  104.    GlobalFree(hUF);
  105.    return 0;
  106.    }
  107.  
  108. lpUserFunctions->password = password;
  109. lpUserFunctions->print = DisplayBuf;
  110. lpUserFunctions->sound = NULL;
  111. lpUserFunctions->replace = GetReplaceDlgRetVal;
  112. lpUserFunctions->SendApplicationMessage = ReceiveDllMessage;
  113.  
  114. /* First we go look for the unzip dll */
  115. #ifdef WIN32
  116. if (SearchPath(
  117.     NULL,               /* address of search path               */
  118.     UNZ_DLL_NAME,       /* address of filename                  */
  119.     NULL,               /* address of extension                 */
  120.     PATH_MAX,           /* size, in characters, of buffer       */
  121.     szFullPath,         /* address of buffer for found filename */
  122.     &ptr                /* address of pointer to file component */
  123.    ) == 0)
  124. #else
  125. hfile = OpenFile(UNZ_DLL_NAME,  &ofs, OF_SEARCH);
  126. if (hfile == HFILE_ERROR)
  127. #endif
  128.    {
  129.    char str[256];
  130.    wsprintf (str, DLL_WARNING, UNZ_DLL_NAME);
  131.    printf("%s\n", str);
  132.    FreeUpMemory();
  133.    return 0;
  134.    }
  135. #ifndef WIN32
  136. else
  137.    lstrcpy(szFullPath, ofs.szPathName);
  138. _lclose(hfile);
  139. #endif
  140.  
  141. /* Now we'll check the unzip dll version information. Note that this is
  142.    not the same information as is returned from a call to UzpVersion()
  143.  */
  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.    if (GetFileVersionInfo(szFullPath, 0L, dwVerInfoSize, lpstrVffInfo))
  162.       {
  163.       fRet = VerQueryValue(lpstrVffInfo,
  164.                TEXT("\\StringFileInfo\\040904E4\\FileVersion"),
  165.               (LPVOID)&lszVer,
  166.               &cchVer);
  167.       fRetName = VerQueryValue(lpstrVffInfo,
  168.                TEXT("\\StringFileInfo\\040904E4\\CompanyName"),
  169.                (LPVOID)&lszVerName,
  170.                &cchVer);
  171.       if (!fRet || !fRetName ||
  172.          (lstrcmpi(lszVer, UNZ_DLL_VERSION) != 0) ||
  173.          (lstrcmpi(lszVerName, COMPANY_NAME) != 0))
  174.          {
  175.          wsprintf (str, DLL_VERSION_WARNING, UNZ_DLL_NAME);
  176.          printf("%s\n", str);
  177.          FreeUpMemory();
  178.          GlobalUnlock(hMem);
  179.          GlobalFree(hMem);
  180.          return 0;
  181.          }
  182.       }
  183.       /* free memory */
  184.    GlobalUnlock(hMem);
  185.    GlobalFree(hMem);
  186.    }
  187. else
  188.    {
  189.    char str[256];
  190.    wsprintf (str, DLL_VERSION_WARNING, UNZ_DLL_NAME);
  191.    printf("%s\n", str);
  192.    FreeUpMemory();
  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. hUnzipDll = LoadLibrary(UNZ_DLL_NAME);
  199. #ifndef WIN32
  200. if (hUnzipDll > HINSTANCE_ERROR)
  201. #else
  202. if (hUnzipDll != NULL)
  203. #endif
  204.    {
  205.    (_DLL_UNZIP)windll_unzip = (_DLL_UNZIP)GetProcAddress(hUnzipDll, "windll_unzip");
  206.    }
  207. else
  208.    {
  209.    char str[256];
  210.    wsprintf (str, "Could not load %s", UNZ_DLL_NAME);
  211.    printf("%s\n", str);
  212.    FreeUpMemory();
  213.    return 0;
  214.    }
  215.  
  216. /*
  217.    Here is where the actual extraction process begins. First we set up the
  218.    flags to be passed into the dll.
  219.  */
  220. lpDCL->ncflag = 0; /* Write to stdout if true */
  221. lpDCL->fQuiet = 0; /* We want all messages.
  222.                       1 = fewer messages,
  223.                       2 = no messages */
  224. lpDCL->ntflag = 0; /* test zip file if true */
  225. lpDCL->nvflag = 0; /* give a verbose listing if true */
  226. lpDCL->nUflag = 0; /* Do not extract only newer */
  227. lpDCL->nzflag = 0; /* display a zip file comment if true */
  228. lpDCL->ndflag = 1; /* Recreate directories if true */
  229. lpDCL->noflag = 1; /* Over-write all files if true */
  230. lpDCL->naflag = 0; /* Do not convert CR to CRLF */
  231. lpDCL->lpszZipFN = argv[1]; /* The archive name */
  232. /* We want to extract all files, so the first two parameters (argc and argv)
  233.    are set to 0 and NULL respectively.
  234.  */  
  235. retcode = (*windll_unzip)(0, NULL, lpDCL, lpUserFunctions);
  236.  
  237. if (retcode != 0)
  238.    printf("Error unzipping...\n");
  239.  
  240. FreeUpMemory();
  241. FreeLibrary(hUnzipDll);
  242. return 1;
  243. }
  244.  
  245. int WINAPI GetReplaceDlgRetVal(char *filename)
  246. {
  247. /* This is where you will decide if you want to replace, rename etc existing
  248.    files.
  249.  */
  250. return 1;
  251. }
  252.  
  253. void FreeUpMemory(void)
  254. {
  255. if (hDCL)
  256.    {
  257.    GlobalUnlock(hDCL);
  258.    GlobalFree(hDCL);
  259.    }
  260. if (hUF)
  261.    {
  262.    GlobalUnlock(hUF);
  263.    GlobalFree(hUF);
  264.    }
  265. }
  266.  
  267. /* This simply determines if we are running on NT or Windows 95 */
  268. #ifdef WIN32
  269. BOOL IsNT(VOID)
  270. {
  271. if(dwPlatformId != 0xFFFFFFFF)
  272.    return dwPlatformId;
  273. else
  274. /* note: GetVersionEx() doesn't exist on WinNT 3.1 */
  275.    {
  276.    if(GetVersion() < 0x80000000)
  277.       {
  278.       (BOOL)dwPlatformId = TRUE;
  279.       }
  280.    else
  281.       {
  282.       (BOOL)dwPlatformId = FALSE;
  283.       }
  284.     }
  285. return dwPlatformId;
  286. }
  287. #endif
  288.  
  289. /* This is a very stripped down version of what is done in WiZ. Essentially
  290.    what this function is for is to do a listing of an archive contents. It
  291.    is actually never called in this example, but a dummy procedure had to
  292.    be put in, so this was used.
  293.  */
  294. void WINAPI ReceiveDllMessage(unsigned long ucsize,unsigned long csiz,
  295.     ush cfactor, ush mo, ush dy, ush yr, ush hh, ush mm,
  296.     char c, char *filename, char *methbuf, unsigned long crc, char fCrypt)
  297. {
  298. char psLBEntry[PATH_MAX];
  299. char LongHdrStats[] =
  300.       "%7lu  %7lu %4s  %02u-%02u-%02u  %02u:%02u  %c%s";
  301. char CompFactorStr[] = "%c%d%%";
  302. char CompFactor100[] = "100%%";
  303. char szCompFactor[10];
  304. char sgn;
  305.  
  306. if (csiz > ucsize)
  307.    sgn = '-';
  308. else
  309.    sgn = ' ';
  310. if (cfactor == 100)
  311.    lstrcpy(szCompFactor, CompFactor100);
  312. else
  313.    sprintf(szCompFactor, CompFactorStr, sgn, cfactor);
  314. wsprintf(psLBEntry, LongHdrStats,
  315.       ucsize, csiz, szCompFactor, mo, dy, yr, hh, mm, c, filename);
  316.  
  317. printf("%s\n", psLBEntry);
  318. }
  319.  
  320. /* Password entry routine - see password.c in the wiz directory for how
  321.    this is actually implemented in WiZ. If you have an encrypted file,
  322.    this will probably give you great pain.
  323.  */
  324. int WINAPI password(char *p, int n, const char *m, const char * name)
  325. {
  326. return 1;
  327. }
  328.  
  329. /* Dummy "print" routine that simply outputs what is sent from the dll */
  330. int WINAPI DisplayBuf(char far *buf, unsigned long size)
  331. {
  332. printf("%s", buf);
  333. return (unsigned int) size;
  334. }
  335.  
  336.  
  337.