home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / xwphescr.zip / XWPH0208.ZIP / src / helpers / resh.c < prev    next >
C/C++ Source or Header  |  2002-06-02  |  5KB  |  135 lines

  1.  
  2. /*
  3.  *@@sourcefile resh.c:
  4.  *      resh.c contains Resource helper functions.
  5.  *
  6.  *      Function prefixes:
  7.  *      --  resh*   Resource helper functions
  8.  *
  9.  *      These functions are forward-declared in resh.h, which
  10.  *      must be #include'd first.
  11.  *
  12.  *@@header "helpers\resh.h"
  13.  *@@added V0.9.4 (2000-07-27) [umoeller]
  14.  */
  15.  
  16. /*
  17.  *      Copyright (C) 2000 Christoph Schulte Mönting.
  18.  *
  19.  *      This program is free software; you can redistribute it and/or modify
  20.  *      it under the terms of the GNU General Public License as published by
  21.  *      the Free Software Foundation, in version 2 as it comes in the COPYING
  22.  *      file of this distribution.
  23.  *      This program is distributed in the hope that it will be useful,
  24.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  25.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26.  *      GNU General Public License for more details.
  27.  */
  28.  
  29. // OS2 includes
  30.  
  31. #define OS2EMX_PLAIN_CHAR
  32.     // this is needed for "os2emx.h"; if this is defined,
  33.     // emx will define PSZ as _signed_ char, otherwise
  34.     // as unsigned char
  35.  
  36. #define INCL_DOSMODULEMGR
  37. #define INCL_DOSRESOURCES
  38. #define INCL_DOSERRORS
  39. #include <os2.h>
  40.  
  41. #include "setup.h"                      // code generation and debugging options
  42.  
  43. #include "helpers\resh.h"
  44.  
  45. /*
  46.  *@@category: Helpers\Control program helpers\Resources
  47.  */
  48.  
  49. /*
  50.  *@@ reshWriteResourceToFile:
  51.  *      get the indicated resource from the current module (usually the
  52.  *      executable) and write it to a file. If pulBytesWritten is not NULL,
  53.  *      the number of written bytes is stored to that location.
  54.  *
  55.  *      Returns:
  56.  *      -- NO_ERROR: no error, resource successfully read and written.
  57.  *      -- ERROR_INVALID_DATA: no. of bytes written != no. of bytes in
  58.  *                             resource. Shouldn't happen.
  59.  *
  60.  *      plus the error codes from DosQueryResourceSize, DosOpen,
  61.  *      DosGetResource, DosWrite.
  62.  *
  63.  *      <B>Warning:</B> If the given file exists, it will be
  64.  *      overwritten without asking for confirmation!
  65.  *
  66.  *@@added V0.9.4 [csm]
  67.  *@@changed V0.9.4 (2000-07-24) [umoeller]: added hmod, changed return values
  68.  */
  69.  
  70. APIRET reshWriteResourceToFile(HMODULE hmod,        // in: module handle or NULLHANDLE for current EXE
  71.                                ULONG  ulTypeID,        // in:  type of the resource
  72.                                ULONG  ulNameID,        // in:  ID of the resource
  73.                                const char *pcszFilename, // in:  name of file to write to
  74.                                PULONG pulBytesWritten) // out: number of bytes written
  75. {
  76.     ULONG   arc = 0;
  77.     ULONG ulResourceSize;
  78.     ULONG ulBytesWritten = 0;
  79.  
  80.     arc = DosQueryResourceSize(hmod,
  81.                                ulTypeID,
  82.                                ulNameID,
  83.                                &ulResourceSize);
  84.     if (arc == NO_ERROR)
  85.     {
  86.         PVOID pvResourceData;
  87.  
  88.         HFILE hFile;
  89.         ULONG ulAction = 0;
  90.  
  91.         // Open file for writing, replace if exists
  92.         arc = DosOpen((PSZ)pcszFilename,
  93.                       &hFile,
  94.                       &ulAction,                           // action taken
  95.                       ulResourceSize,                      // primary allocation size
  96.                       FILE_ARCHIVED | FILE_NORMAL,         // file attribute
  97.                       OPEN_ACTION_CREATE_IF_NEW
  98.                         | OPEN_ACTION_REPLACE_IF_EXISTS,   // open flags
  99.                       OPEN_FLAGS_NOINHERIT
  100.                         | OPEN_FLAGS_SEQUENTIAL            // sequential, not random access
  101.                         | OPEN_SHARE_DENYREADWRITE         // deny r/w share
  102.                         | OPEN_ACCESS_WRITEONLY,           // write mode
  103.                       NULL);                               // no EAs
  104.  
  105.         // If successful: get resource, write it and close file
  106.         if (arc == NO_ERROR)
  107.         {
  108.             arc = DosGetResource(hmod,
  109.                                  ulTypeID,
  110.                                  ulNameID,
  111.                                  &pvResourceData);
  112.             if (arc == NO_ERROR)
  113.             {
  114.                 arc = DosWrite(hFile,
  115.                                pvResourceData,
  116.                                ulResourceSize,
  117.                                &ulBytesWritten);
  118.                 if (arc == NO_ERROR)
  119.                     if (ulResourceSize != ulBytesWritten)
  120.                         arc = ERROR_INVALID_DATA;
  121.  
  122.                 DosFreeResource(pvResourceData);
  123.             }
  124.  
  125.             DosClose(hFile);
  126.         }
  127.     }
  128.  
  129.     if (pulBytesWritten)
  130.         *pulBytesWritten = ulBytesWritten;
  131.  
  132.     return arc;
  133. }
  134.  
  135.