home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Source / ODynamicLib.cpp < prev    next >
C/C++ Source or Header  |  1997-04-05  |  5KB  |  209 lines

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1995
  3. // All Rights Reserved
  4. // ODynamicLib.cpp
  5.  
  6. /*
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  13.  *    endorse or promote products derived from this software
  14.  *    without specific prior written permission.
  15.  * 3. See OCL.INF for a detailed copyright notice.
  16.  *
  17.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  18.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27.  * SUCH DAMAGE.
  28.  */
  29.  
  30.  
  31. // $Header: W:/Projects/OCL/Source/rcs/ODynamicLib.cpp 1.50 1996/08/11 23:49:14 B.STEIN Release $
  32.  
  33. #define __OCL_SOURCE__
  34.  
  35. #define OINCL_OSTRING
  36. #define OINCL_BASE
  37.  
  38. #include <ocl.hpp>
  39. #include <ODynamicLib.hpp>
  40. #include <OMessage.hpp>
  41.  
  42.  
  43. ODynamicLib::ODynamicLib(HMODULE mod)
  44.   : modHandle(mod)
  45.   {}
  46.  
  47.  
  48. ODynamicLib::ODynamicLib(PCSZ libname)
  49. {
  50.  APIRET rc;
  51.  CHAR   errBuffer[100];
  52.  
  53.  rc = DosLoadModule(errBuffer, 100, (PSZ)libname, &modHandle);
  54.  if ((rc != 0) || (!modHandle))
  55.    throw OVioException(errBuffer, rc);
  56. }
  57.  
  58.  
  59. ODynamicLib::~ODynamicLib()
  60. {
  61.  freeModule();
  62. }
  63.  
  64.  
  65. PSZ ODynamicLib::isOfType() const
  66.  return("ODynamicLib"); 
  67. }
  68.  
  69.  
  70. ODynamicLib& ODynamicLib::freeModule()
  71. {
  72.  if (modHandle)
  73.    DosFreeModule(modHandle);
  74.  return(*this);
  75. }
  76.  
  77.  
  78.  
  79. HMODULE ODynamicLib::getLibHandle()
  80. {
  81.  return( modHandle );                           
  82. }
  83.  
  84.  
  85.  
  86. ODynamicLib&  ODynamicLib::setLibHandle(HMODULE mod)
  87. {
  88.  modHandle = mod;
  89.  return(*this);
  90. }
  91.  
  92.  
  93.  
  94. PFN ODynamicLib::queryFn(ULONG ordinal)
  95. {
  96.  APIRET rc;
  97.  PFN    fn = (PFN) NULL;
  98.  
  99.  rc = DosQueryProcAddr(modHandle, ordinal, (PSZ)NULL, &fn);
  100.  if ((rc != 0) || (!fn))
  101.    throw OVioException(OCL::apierror(50, rc), rc);
  102.  return(fn);
  103. }
  104.  
  105.  
  106. PFN ODynamicLib::queryFn(PCSZ fnName)
  107. {
  108.  APIRET rc;
  109.  PFN    fn = (PFN) NULL;
  110.  
  111.  rc = DosQueryProcAddr(modHandle, 0, (PSZ)fnName, &fn);
  112.  if ((rc != 0) || (!fn))
  113.    throw OVioException(OCL::apierror(51, rc), rc);
  114.  return(fn);
  115. }
  116.  
  117.  
  118.  
  119. ULONG ODynamicLib::queryFnType(ULONG ordinal)
  120. {
  121.  APIRET  rc;
  122.  ULONG   ul;
  123.  
  124.  rc = DosQueryProcType(modHandle, ordinal, (PSZ)NULL, &ul);
  125.  if (rc != 0)
  126.    throw OVioException(OCL::apierror(52, rc), rc);
  127.  return( (ul == PT_16BIT) ? 16 : 32);
  128. }
  129.  
  130.  
  131. ULONG ODynamicLib::queryFnType(PCSZ fnName)
  132. {
  133.  APIRET  rc;
  134.  ULONG   ul;
  135.  
  136.  rc = DosQueryProcType(modHandle, 0, (PSZ)fnName, &ul);
  137.  if (rc != 0)
  138.    throw OVioException(OCL::apierror(53, rc), rc);
  139.  return( (ul == PT_16BIT) ? 16 : 32);
  140. }
  141.  
  142.  
  143. PSZ ODynamicLib::queryModuleName()
  144. {
  145.  APIRET  rc;
  146.  OString Buffer(CCHMAXPATH);
  147.  
  148.  rc = DosQueryModuleName(modHandle, CCHMAXPATH, Buffer);
  149.  if (rc != 0)
  150.    throw OVioException(OCL::apierror(54, rc), rc);
  151.  return(Buffer);
  152. }
  153.  
  154.  
  155. ULONG ODynamicLib::queryAppType()
  156. {
  157.  APIRET  rc;
  158.  ULONG   ul;
  159.  
  160.  try {
  161.    rc = DosQueryAppType(queryModuleName(), &ul); }
  162.  catch(OVioException& ex) {
  163.    rc = ex.rc; }
  164.  
  165.  if (rc != 0)
  166.    throw OVioException(OCL::apierror(55, rc), rc);
  167.  
  168.  return(ul);
  169. }
  170.  
  171.  
  172. PVOID ODynamicLib::getResource(ULONG resType, ULONG resID)
  173. {
  174.  APIRET rc;
  175.  PVOID  pv;
  176.  
  177.  rc = DosGetResource(modHandle, resType, resID, &pv);
  178.  if (rc != 0)
  179.    throw OVioException(OCL::apierror(56, rc), rc);
  180.  
  181.  return(pv);
  182. }
  183.  
  184.  
  185.  
  186. ODynamicLib& ODynamicLib::freeResource(PVOID resOffset)
  187. {
  188.  if (resOffset)
  189.    DosFreeResource(resOffset);
  190.  return(*this);
  191. }
  192.  
  193.  
  194. ULONG ODynamicLib::queryResourceSize(ULONG resType, ULONG resID)
  195. {
  196.  APIRET rc;
  197.  ULONG  ul;
  198.  
  199.  rc = DosQueryResourceSize(modHandle, resType, resID, &ul);
  200.  if (rc != 0)
  201.    throw OVioException(OCL::apierror(57, rc), rc);
  202.  
  203.  return(ul);
  204. }
  205.  
  206.  
  207. // end of source
  208.