unix_library.cpp
上传用户:dangjiwu
上传日期:2013-07-19
资源大小:42019k
文件大小:6k
源码类别:

Symbian

开发平台:

Visual C++

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Source last modified: $Id: unix_library.cpp,v 1.1.1.1.50.3 2004/07/09 01:47:39 hubbe Exp $
  3.  * 
  4.  * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
  5.  * 
  6.  * The contents of this file, and the files included with this file,
  7.  * are subject to the current version of the RealNetworks Public
  8.  * Source License (the "RPSL") available at
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10.  * the file under the current version of the RealNetworks Community
  11.  * Source License (the "RCSL") available at
  12.  * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13.  * will apply. You may also obtain the license terms directly from
  14.  * RealNetworks.  You may not use this file except in compliance with
  15.  * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16.  * to this file, the RCSL.  Please see the applicable RPSL or RCSL for
  17.  * the rights, obligations and limitations governing use of the
  18.  * contents of the file.
  19.  * 
  20.  * Alternatively, the contents of this file may be used under the
  21.  * terms of the GNU General Public License Version 2 or later (the
  22.  * "GPL") in which case the provisions of the GPL are applicable
  23.  * instead of those above. If you wish to allow use of your version of
  24.  * this file only under the terms of the GPL, and not to allow others
  25.  * to use your version of this file under the terms of either the RPSL
  26.  * or RCSL, indicate your decision by deleting the provisions above
  27.  * and replace them with the notice and other provisions required by
  28.  * the GPL. If you do not delete the provisions above, a recipient may
  29.  * use your version of this file under the terms of any one of the
  30.  * RPSL, the RCSL or the GPL.
  31.  * 
  32.  * This file is part of the Helix DNA Technology. RealNetworks is the
  33.  * developer of the Original Code and owns the copyrights in the
  34.  * portions it created.
  35.  * 
  36.  * This file, and the files included with this file, is distributed
  37.  * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  38.  * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  39.  * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  40.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  41.  * ENJOYMENT OR NON-INFRINGEMENT.
  42.  * 
  43.  * Technology Compatibility Kit Test Suite(s) Location:
  44.  *    http://www.helixcommunity.org/content/tck
  45.  * 
  46.  * Contributor(s):
  47.  * 
  48.  * ***** END LICENSE BLOCK ***** */
  49. ////////////////////
  50. //
  51. // This file contains functions to load & release Shared Libraries.
  52. // It is used primarily by the codec loader/unloader code.
  53. //
  54. #include "unix_library.h"
  55. #include "hxtypes.h"
  56. #include "hxstring.h"
  57. #include "carray.h"
  58. #include "dllacces.h"
  59. //
  60. // CSharedLibMgr is a class which manages the mapping between the HMODULE
  61. // and the DLLAccess object which is used to make the actual OS calls to
  62. // open, close, etc. the library.
  63. //
  64. class CSharedLibMgr
  65. {
  66. public:
  67. static ULONG32  LoadLibrary  (const char *libname);
  68. static void  FreeLibrary  (HMODULE lib);
  69. static void * GetProcAddress (HMODULE lib, char *funcName);
  70. private:
  71. DLLAccess *dll;
  72. static CHXPtrArray mLibList;
  73. static DLLAccess * GetLib (ULONG32 index);
  74. };
  75. CHXPtrArray CSharedLibMgr::mLibList;
  76. ULONG32 CSharedLibMgr::LoadLibrary (const char *libName)
  77. {
  78. DLLAccess *pAccess = NULL;
  79. ULONG32 libIndex = DL_BAD_LIB_ID;
  80. if (libName == NULL)
  81. return DL_BAD_LIB_ID;
  82. pAccess = new DLLAccess;
  83. HX_ASSERT (pAccess);
  84. if (pAccess->open (libName) == DLLAccess::DLL_OK)
  85. {
  86. libIndex = mLibList.Add (pAccess);
  87. }
  88. else
  89. {
  90. delete pAccess;
  91. }
  92. return libIndex;
  93. }
  94. void CSharedLibMgr::FreeLibrary (HMODULE libHandle)
  95. {
  96. if (libHandle == DL_BAD_LIB_ID)
  97. return;
  98. DLLAccess *pAccess = GetLib (libHandle);
  99. if (pAccess != NULL)
  100. {
  101. pAccess->close();
  102. delete pAccess;
  103. mLibList.SetAt (libHandle, NULL);
  104. }
  105. }
  106. void *CSharedLibMgr::GetProcAddress (HMODULE libHandle, char *funcName)
  107. {
  108. if (libHandle == DL_BAD_LIB_ID || funcName == NULL)
  109. return NULL;
  110. DLLAccess *pAccess = GetLib (libHandle);
  111. HX_ASSERT (pAccess);
  112. return (pAccess->getSymbol (funcName));
  113. }
  114. DLLAccess * CSharedLibMgr::GetLib (ULONG32 index)
  115. {
  116. if (index == DL_BAD_LIB_ID)
  117. return NULL;
  118. return ((DLLAccess *)mLibList.GetAt (index));
  119. }
  120. /////////////////////////////////////////////////////////////////////////////
  121. //
  122. // Function:
  123. //
  124. // LoadLibrary()
  125. //
  126. // Purpose:
  127. //
  128. // Called to load a Shared Library given the library's name.
  129. //
  130. // Parameters:
  131. //
  132. // char* dllname
  133. // The name of the library.
  134. //
  135. // Return:
  136. //
  137. // ULONG32
  138. // Returns a handle to the library.
  139. //
  140. ULONG32 LoadLibrary(const char* dllname)
  141. {
  142. return CSharedLibMgr::LoadLibrary (dllname);
  143. }
  144. /////////////////////////////////////////////////////////////////////////////
  145. //
  146. // Function:
  147. //
  148. // FreeLibrary()
  149. //
  150. // Purpose:
  151. //
  152. // Called to free a Shared Library. If this is not called the library
  153. // will be freed when the application quits.
  154. //
  155. // Parameters:
  156. //
  157. // HMODULE lib
  158. //
  159. // Return:
  160. //
  161. // none
  162. //
  163. void FreeLibrary(HMODULE lib)
  164. {
  165. CSharedLibMgr::FreeLibrary (lib);
  166. }
  167. /////////////////////////////////////////////////////////////////////////////
  168. //
  169. // Function:
  170. //
  171. // GetProcAddress()
  172. //
  173. // Purpose:
  174. //
  175. // Called to get a function pointer in a Shared Library.
  176. //
  177. // Parameters:
  178. //
  179. // HMODULE lib
  180. //
  181. // char* function
  182. // The function name
  183. //
  184. // Return:
  185. //
  186. // void*
  187. // The address of the function.
  188. //
  189. void* GetProcAddress(HMODULE lib, char* function)
  190. {
  191. return CSharedLibMgr::GetProcAddress (lib, function);
  192. }