home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedZip.Cab / F112448_DllAPI.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-13  |  17.1 KB  |  566 lines

  1. /*
  2.  * DllAPI.cpp
  3.  * Copyright (c) 1998-1999, Xceed Software Inc.
  4.  *
  5.  * Description:
  6.  *    Sample application using the Xceed Zip 4 DLL API interface.
  7.  *    This file contains the entry point of the application.
  8.  *
  9.  */
  10.  
  11. #include "stdafx.h"
  12. #include "resource.h"
  13. #include "Examples.h"
  14.  
  15.  
  16. #define MAX_LOADSTRING  100
  17.  
  18. // Global Variables:
  19. HINSTANCE hInst;                                // current instance
  20. TCHAR szTitle[MAX_LOADSTRING];                                // The title bar text
  21. TCHAR szWindowClass[MAX_LOADSTRING];                                // The title bar text
  22.  
  23. // Foward declarations of functions included in this code module:
  24. ATOM                MyRegisterClass(HINSTANCE hInstance);
  25. BOOL                InitInstance(HINSTANCE, int);
  26. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  27. LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  28.  
  29. // Xceed Zip Dll instance
  30. HMODULE hXceedZipDll;
  31.  
  32. int APIENTRY WinMain(HINSTANCE hInstance,
  33.                      HINSTANCE hPrevInstance,
  34.                      LPSTR     lpCmdLine,
  35.                      int       nCmdShow)
  36. {
  37.     MSG msg;
  38.     HACCEL hAccelTable;
  39.  
  40.     // Initialize global strings
  41.     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  42.     LoadString(hInstance, IDC_API, szWindowClass, MAX_LOADSTRING);
  43.     MyRegisterClass(hInstance);
  44.  
  45.     // Perform application initialization:
  46.     if (!InitInstance (hInstance, nCmdShow)) 
  47.     {
  48.         return FALSE;
  49.     }
  50.  
  51.     hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_API);
  52.  
  53.   // Initialize the Xceed Zip Compression Library for Dll API access
  54.   hXceedZipDll = LoadLibrary( "XceedZip.dll" );
  55.  
  56.   LPFNXCEEDZIPINITDLL lpfnInitDll = ( LPFNXCEEDZIPINITDLL ) GetProcAddress( hXceedZipDll, "XceedZipInitDLL" );
  57.  
  58.   if( lpfnInitDll )
  59.   {
  60.     lpfnInitDll();
  61.   }
  62.  
  63.   // Main message loop:
  64.     while (GetMessage(&msg, NULL, 0, 0)) 
  65.     {
  66.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
  67.         {
  68.             TranslateMessage(&msg);
  69.             DispatchMessage(&msg);
  70.         }
  71.     }
  72.  
  73.   LPFNXCEEDZIPSHUTDOWNDLL lpfnShutdown  = ( LPFNXCEEDZIPSHUTDOWNDLL ) GetProcAddress( hXceedZipDll, "XceedZipShutdownDLL" );
  74.  
  75.   if( lpfnShutdown )
  76.   {
  77.     lpfnShutdown();
  78.   }
  79.   
  80.   FreeLibrary( hXceedZipDll );
  81.  
  82.     return msg.wParam;
  83. }
  84.  
  85.  
  86.  
  87. //
  88. //  FUNCTION: MyRegisterClass()
  89. //
  90. //  PURPOSE: Registers the window class.
  91. //
  92. //  COMMENTS:
  93. //
  94. //    This function and its usage is only necessary if you want this code
  95. //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
  96. //    function that was added to Windows 95. It is important to call this function
  97. //    so that the application will get 'well formed' small icons associated
  98. //    with it.
  99. //
  100. ATOM MyRegisterClass(HINSTANCE hInstance)
  101. {
  102.     WNDCLASSEX wcex;
  103.  
  104.     wcex.cbSize = sizeof(WNDCLASSEX); 
  105.  
  106.     wcex.style            = CS_HREDRAW | CS_VREDRAW;
  107.     wcex.lpfnWndProc    = (WNDPROC)WndProc;
  108.     wcex.cbClsExtra        = 0;
  109.     wcex.cbWndExtra        = 0;
  110.     wcex.hInstance        = hInstance;
  111.     wcex.hIcon            = LoadIcon(hInstance, (LPCTSTR)IDI_API);
  112.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  113.     wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
  114.     wcex.lpszMenuName    = (LPCSTR)IDC_API;
  115.     wcex.lpszClassName    = szWindowClass;
  116.     wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
  117.  
  118.     return RegisterClassEx(&wcex);
  119. }
  120.  
  121. //
  122. //   FUNCTION: InitInstance(HANDLE, int)
  123. //
  124. //   PURPOSE: Saves instance handle and creates main window
  125. //
  126. //   COMMENTS:
  127. //
  128. //        In this function, we save the instance handle in a global variable and
  129. //        create and display the main program window.
  130. //
  131. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  132. {
  133.    HWND hWnd;
  134.  
  135.    hInst = hInstance; // Store instance handle in our global variable
  136.  
  137.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  138.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  139.  
  140.    if (!hWnd)
  141.    {
  142.       return FALSE;
  143.    }
  144.  
  145.    ShowWindow(hWnd, nCmdShow);
  146.    UpdateWindow(hWnd);
  147.  
  148.    return TRUE;
  149. }
  150.  
  151. //
  152. //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  153. //
  154. //  PURPOSE:  Processes messages for the main window.
  155. //
  156. //  WM_COMMAND    - process the application menu
  157. //  WM_PAINT    - Paint the main window
  158. //  WM_DESTROY    - post a quit message and return
  159. //
  160. //
  161. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  162. {
  163.   static  HWND  hList = NULL;
  164.  
  165.   // For preview example
  166.   static HXCEEDZIP  hCurrent    = NULL;
  167.  
  168.     switch (message) 
  169.     {
  170.     case WM_CREATE:
  171.     {
  172.       RECT  rClient;
  173.       GetClientRect( hWnd, &rClient );
  174.  
  175.       hList = CreateWindow( "LISTBOX", "", WS_BORDER | WS_CHILD | WS_VISIBLE | WS_VSCROLL | LBS_HASSTRINGS | LBS_NOINTEGRALHEIGHT, 
  176.                             rClient.left, rClient.top, rClient.right, rClient.bottom, 
  177.                             hWnd, ( HMENU )11, hInst, 0 );
  178.  
  179.       break;
  180.     }
  181.  
  182.     case WM_SIZE:
  183.     {
  184.       RECT  rClient;
  185.       GetClientRect( hWnd, &rClient );
  186.  
  187.       MoveWindow( hList, rClient.left, rClient.top, rClient.right, rClient.bottom, TRUE );
  188.  
  189.       break;
  190.     }
  191.  
  192.         case WM_COMMAND:
  193.     {
  194.             int wmId    = LOWORD(wParam); 
  195.             int wmEvent = HIWORD(wParam); 
  196.  
  197.             // Parse the menu selections:
  198.             switch (wmId)
  199.             {
  200.                 case IDM_ABOUT:
  201.                    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  202.                    break;
  203.                 case IDM_EXIT:
  204.                    DestroyWindow(hWnd);
  205.                    break;
  206.         case IDM_OPTIONS_REPLACE:
  207.         {
  208.           HMENU hMenu = GetMenu( hWnd );
  209.  
  210.           CheckMenuItem( hMenu, wmId, 
  211.                          MF_BYCOMMAND & ( GetMenuState( hMenu, wmId, MF_BYCOMMAND ) ^ MF_CHECKED ) );
  212.           break;
  213.         }
  214.         case IDM_OPTIONS_CLEAR:
  215.           SendMessage( hList, LB_RESETCONTENT, 0, 0 );
  216.           break;
  217.  
  218.         //
  219.         // Zip example
  220.         //
  221.         case IDM_XCEED_ZIP:
  222.         {
  223.           DoZipExample( hWnd, hList );
  224.  
  225.           break;
  226.         }
  227.  
  228.         //
  229.         // Unzip example
  230.         //
  231.         case IDM_XCEED_UNZIP:
  232.         {
  233.           DoUnzipExample( hList );
  234.  
  235.           break;
  236.         }
  237.  
  238.         //
  239.         // Preview example
  240.         //
  241.         case IDM_XCEED_PREVIEW:
  242.         {
  243.           XceedZipFunctions* pFuncs = ( XceedZipFunctions* ) GetProcAddress( hXceedZipDll, "g_xzFunctions" );
  244.  
  245.           if ( hCurrent )
  246.           {
  247.             // Abort the operation
  248.             pFuncs->lpfnXzSetAbort( hCurrent, TRUE );
  249.             SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) "The previewing operation is being aborted." );
  250.           }
  251.           else
  252.           {
  253.             DoPreviewExample( hWnd, hList, &hCurrent );
  254.           }
  255.  
  256.           ModifyMenu( GetMenu( hWnd ), IDM_XCEED_PREVIEW, MF_BYCOMMAND | MF_STRING, IDM_XCEED_PREVIEW, 
  257.                       ( hCurrent ? "Abort &Preview files" : "&Preview files" ) );
  258.  
  259.           break;
  260.         }
  261.  
  262.         //
  263.         // Listing zip file example
  264.         //
  265.         case IDM_XCEED_LIST:
  266.         {
  267.           DoListingExample( hWnd, hList );
  268.  
  269.           break;
  270.         }
  271.  
  272.         //
  273.         // Testing zip file example
  274.         //
  275.         case IDM_XCEED_TEST:
  276.         {
  277.           DoTestingExample( hWnd, hList );
  278.  
  279.           break;
  280.         }
  281.  
  282.         //
  283.         // Getting zip file contents example
  284.         //
  285.         case IDM_XCEED_GET:
  286.         {
  287.           DoGetContentsExample( hWnd, hList );
  288.  
  289.           break;
  290.         }
  291.  
  292.                 default:
  293.                    return DefWindowProc(hWnd, message, wParam, lParam);
  294.             }
  295.             break;
  296.     }
  297.  
  298.         case WM_DESTROY:
  299.             PostQuitMessage(0);
  300.             break;
  301.  
  302.     // When XceedZip triggers an event, and you specified handling events with
  303.     // a window message, your window receives this message. Then, the wParam
  304.     // contains the XceedZip event id, as found in XceedZipAPI.h, and the lParam
  305.     // is a pointer to the parameters structure. Depending on if you created
  306.     // your HXCEEDZIP with XzCreateXceedZipA or XzCreateXceedZipW, you need
  307.     // to typecast lParam to the equivalent structure. For the sake of these
  308.     // examples, when using the window message, we use Ansi calls. For an example
  309.     // on using Unicode calls, take a look at "MyCallback" in "Examples.cpp"
  310.     // Since this project is compiling in Ansi, not in Unicode, we can use the
  311.     // typenames that do not end with an A or W. These are automatically mapped
  312.     // to the correct type, depending if "UNICODE" is defined.
  313.     // (see XceedZipAPI.h)
  314.     case WM_USER_XCEEDZIPEVENT:
  315.     {
  316.       switch( wParam )
  317.       {
  318.         // Another disk is required
  319.         case XM_INSERTDISK:
  320.         {
  321.           // We typecast lParam to the correct structure pointer
  322.           xcdInsertDiskParams*  pParams = ( xcdInsertDiskParams * ) lParam;
  323.           int                   nAnswer = IDCANCEL;
  324.  
  325.           if ( pParams->lDiskNumber == 0 )
  326.           {
  327.             nAnswer = MessageBox( hWnd, "Please insert the last disk of the set.", "Disk required",
  328.                                   MB_ICONQUESTION | MB_OKCANCEL );
  329.           }
  330.           else
  331.           {
  332.             char  szMsg[ 100 ];
  333.             wsprintf( szMsg, "Please insert disk #%d.", pParams->lDiskNumber );
  334.             nAnswer = MessageBox( hWnd, szMsg, "Disk required", MB_ICONQUESTION | MB_OKCANCEL );
  335.           }
  336.  
  337.           // We modify the bDiskInserted member to reflect the desired action
  338.           pParams->bDiskInserted  = ( nAnswer == IDOK );
  339.  
  340.           break;
  341.         }
  342.         // A file is evaluated for zipping
  343.         case XM_ZIPPREPROCESSINGFILE:
  344.         {
  345.           xcdZipPreprocessingFileParams* pParams = ( xcdZipPreprocessingFileParams * ) lParam;
  346.           // Here, you could change some information about this file, or reject
  347.           // the file based on your own custom filtering conditions.
  348.           break;
  349.         }
  350.         // A file would be evaluated in a similar zip operation
  351.         case XM_PREVIEWINGFILE:
  352.         {
  353.           xcdPreviewingFileParams*  pParams = ( xcdPreviewingFileParams * ) lParam;
  354.           SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) pParams->szFilename );
  355.           break;
  356.         }
  357.         // A file is found in the zip file
  358.         case XM_LISTINGFILE:
  359.         {
  360.           xcdListingFileParams* pParams = ( xcdListingFileParams * ) lParam;
  361.           SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) pParams->szFilename );
  362.           break;
  363.         }
  364.         // A file is skipped for some reason
  365.         case XM_SKIPPINGFILE:
  366.         {
  367.           xcdSkippingFileParams*  pParams = ( xcdSkippingFileParams * ) lParam;
  368.  
  369.           // Display the skipped filename
  370.           char  szMsg[ 300 ];
  371.           wsprintf( szMsg, "Skipping file %s", pParams->szFilename );
  372.           SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  373.  
  374.           // Get and display the reason. You can see that each structure contains
  375.           // the handle top the instance that triggered the event (hZip).
  376.           szMsg[ 0 ]  = ' ';
  377.  
  378.           LPFNXZGETERRORDESCRIPTIONA  pDesc = ( LPFNXZGETERRORDESCRIPTIONA ) GetProcAddress( hXceedZipDll, "XzGetErrorDescriptionA" );
  379.  
  380.           if( pDesc )
  381.           {
  382.             pDesc( pParams->hZip, xvtSkippingReason, pParams->xReason, szMsg+1, 299 );
  383.             SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  384.           }
  385.  
  386.           break;
  387.         }
  388.         // A file will be tested. If an error occurs, a SkippingFile or 
  389.         // Warning event will occur
  390.         case XM_TESTINGFILE:
  391.         {
  392.           xcdTestingFileParams* pParams = ( xcdTestingFileParams * ) lParam;
  393.           // Here, we could verify that the list of files is what we expect
  394.           break;
  395.         }
  396.         // The FileStatus event is the triggered event along the actual
  397.         // processing. That's why it's the best place for status messages.
  398.         case XM_FILESTATUS:
  399.         {
  400.           xcdFileStatusParams*  pParams = ( xcdFileStatusParams * ) lParam;
  401.  
  402.           // When starting to process a file, we display it's name
  403.           if ( pParams->lBytesProcessed == 0 )
  404.           {
  405.             LPFNXZGETCURRENTOPERATION pOper = ( LPFNXZGETCURRENTOPERATION ) GetProcAddress( hXceedZipDll, "XzGetCurrentOperation" );
  406.  
  407.             if( pOper )
  408.             {
  409.               char  szMsg[ 300 ] = { 0 };
  410.  
  411.               // Starting to process this file
  412.               switch( pOper( pParams->hZip ) )
  413.               {
  414.                 case xcoZipping:
  415.                 {
  416.                   wsprintf( szMsg, "Zipping %s", pParams->szFilename );
  417.                   break;
  418.                 }
  419.                 case xcoTestingZipFile:
  420.                 {
  421.                   wsprintf( szMsg, "Testing %s", pParams->szFilename );
  422.                   break;
  423.                 }
  424.               }
  425.  
  426.               if ( szMsg[ 0 ] )
  427.               {
  428.                 SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  429.               }
  430.             }
  431.           }
  432.  
  433.           // We could insert here code to update a file status progress bar.
  434.  
  435.           break;
  436.         }
  437.         // The GlobalStatus event gives the global progression
  438.         case XM_GLOBALSTATUS:
  439.         {
  440.           xcdGlobalStatusParams*  pParam  = ( xcdGlobalStatusParams * ) lParam;
  441.           break;
  442.         }
  443.         // When spanning, we could handle this to wipe inserted disks.
  444.         case XM_DISKNOTEMPTY:
  445.         {
  446.           xcdDiskNotEmptyParams*  pParams = ( xcdDiskNotEmptyParams * ) lParam;
  447.           break;
  448.         }
  449.         // The process is completed. This is highly usefull when working
  450.         // in background processing
  451.         case XM_PROCESSCOMPLETED:
  452.         {
  453.           xcdProcessCompletedParams*  pParams = ( xcdProcessCompletedParams * ) lParam;
  454.  
  455.           XceedZipFunctions*  pFuncs  = ( XceedZipFunctions* ) GetProcAddress( hXceedZipDll, "g_xzFunctions" );
  456.  
  457.           if( pFuncs && pFuncs->lpfnXzGetCurrentOperation( pParams->hZip ) == xcoPreviewing )
  458.           {
  459.             // Since the previewing is done in background, we release handles here!
  460.             char  szMsg[ 200 ];
  461.             pFuncs->lpfnXzGetErrorDescriptionA( pParams->hZip, xvtError, pParams->xResult, szMsg, 200 );
  462.             SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  463.  
  464.             pFuncs->lpfnXzDestroyXceedZip( hCurrent );
  465.             hCurrent  = NULL;
  466.  
  467.             ModifyMenu( GetMenu( hWnd ), IDM_XCEED_PREVIEW, MF_BYCOMMAND | MF_STRING, IDM_XCEED_PREVIEW, "&Preview files" );
  468.           }
  469.  
  470.           break;
  471.         }
  472.         // When creating a zip file, we can assign a global comment here.
  473.         // When reading a zip file, we can retrieve the global comment here.
  474.         // When modifying a zip file, we both can get and set the comment here.
  475.         case XM_ZIPCOMMENT:
  476.         {
  477.           xcdZipCommentParams*  pParams = ( xcdZipCommentParams * ) lParam;
  478.           break;
  479.         }
  480.         // If you want to zip memory, do start here
  481.         case XM_QUERYMEMORYFILE:
  482.         {
  483.           xcdQueryMemoryFileParams* pParams = ( xcdQueryMemoryFileParams * ) lParam;
  484.           break;
  485.         }
  486.         // This is triggered when you handle QueryMemoryFile
  487.         case XM_ZIPPINGMEMORYFILE:
  488.         {
  489.           xcdZippingMemoryFileParams* pParams = ( xcdZippingMemoryFileParams * ) lParam;
  490.           break;
  491.         }
  492.         // This is triggered when you redirect to memory through the 
  493.         // UnzipPreprocessingFile event.
  494.         case XM_UNZIPPINGMEMORYFILE:
  495.         {
  496.           xcdUnzippingMemoryFileParams* pParams = ( xcdUnzippingMemoryFileParams * ) lParam;
  497.           break;
  498.         }
  499.         // A recoverable inconsistency was encountered
  500.         case XM_WARNING:
  501.         {
  502.           xcdWarningParams* pParams = ( xcdWarningParams * ) lParam;
  503.  
  504.           // Display the affected file and the message
  505.           char  szWarning[ 200 ] = { 0 };
  506.  
  507.           LPFNXZGETERRORDESCRIPTIONA  pDesc = ( LPFNXZGETERRORDESCRIPTIONA ) GetProcAddress( hXceedZipDll, "XzGetErrorDescriptionA" );
  508.  
  509.           if( pDesc )
  510.           {
  511.             pDesc( pParams->hZip, xvtWarning, pParams->xWarning, szWarning, 200 );
  512.           }
  513.  
  514.           char  szMsg[ 200 ];
  515.           wsprintf( szMsg, "Warning for file %s: %s", pParams->szFilename, szWarning );
  516.           SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  517.  
  518.           break;
  519.         }
  520.         // The provided password is incorrect
  521.         case XM_INVALIDPASSWORD:
  522.         {
  523.           xcdInvalidPasswordParams* pParams = ( xcdInvalidPasswordParams * ) lParam;
  524.           break;
  525.         }
  526.         // A file is about to be replaced
  527.         case XM_REPLACINGFILE:
  528.         {
  529.           xcdReplacingFileParams* pParams = ( xcdReplacingFileParams * ) lParam;
  530.           break;
  531.         }
  532.         // When updating a zip file, this is triggered while reading the 
  533.         // original zip file.
  534.         case XM_ZIPCONTENTSSTATUS:
  535.         {
  536.           xcdZipContentsStatusParams*  pParam  = ( xcdZipContentsStatusParams * ) lParam;
  537.           break;
  538.         }
  539.       }
  540.       break;
  541.     }
  542.         default:
  543.             return DefWindowProc(hWnd, message, wParam, lParam);
  544.    }
  545.    return 0;
  546. }
  547.  
  548. // Mesage handler for about box.
  549. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  550. {
  551.     switch (message)
  552.     {
  553.         case WM_INITDIALOG:
  554.                 return TRUE;
  555.  
  556.         case WM_COMMAND:
  557.             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  558.             {
  559.                 EndDialog(hDlg, LOWORD(wParam));
  560.                 return TRUE;
  561.             }
  562.             break;
  563.     }
  564.     return FALSE;
  565. }
  566.