PageRenderTime 157ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/beta1/harbour/source/vm/dynlibhb.c

#
C | 214 lines | 125 code | 24 blank | 65 comment | 21 complexity | d3b9e09c4167d3d1b2ed3b4afd6c3fa5 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. /*
  2. * $Id: dynlibhb.c 7892 2007-11-01 16:03:54Z druzus $
  3. */
  4. /*
  5. * Harbour Project source code:
  6. * Dynamic link libraries management functions
  7. *
  8. * Copyright 2001 Antonio Linares <alinares@fivetech.com>
  9. * www - http://www.harbour-project.org
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version, with one exception:
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2, or (at your option)
  19. * any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this software; see the file COPYING. If not, write to
  28. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  29. * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
  30. *
  31. * As a special exception, the Harbour Project gives permission for
  32. * additional uses of the text contained in its release of Harbour.
  33. *
  34. * The exception is that, if you link the Harbour libraries with other
  35. * files to produce an executable, this does not by itself cause the
  36. * resulting executable to be covered by the GNU General Public License.
  37. * Your use of that executable is in no way restricted on account of
  38. * linking the Harbour library code into it.
  39. *
  40. * This exception does not however invalidate any other reasons why
  41. * the executable file might be covered by the GNU General Public License.
  42. *
  43. * This exception applies only to the code released by the Harbour
  44. * Project under the name Harbour. If you copy code from other
  45. * Harbour Project or Free Software Foundation releases into a copy of
  46. * Harbour, as the General Public License permits, the exception does
  47. * not apply to the code that you add in this way. To avoid misleading
  48. * anyone as to the status of such modified files, you must delete
  49. * this exception notice from them.
  50. *
  51. * If you write modifications of your own for Harbour, it is your choice
  52. * whether to permit this exception to apply to your modifications.
  53. * If you do not wish that, delete this exception notice.
  54. *
  55. */
  56. #define HB_OS_WIN_32_USED
  57. #include "hbvmopt.h"
  58. #include "hbapi.h"
  59. #include "hbapiitm.h"
  60. #include "hbstack.h"
  61. #include "hbvm.h"
  62. #if defined(HB_OS_LINUX) && !defined(__WATCOMC__)
  63. # include <dlfcn.h>
  64. #endif
  65. /* This releases regex when called from the garbage collector */
  66. static HB_GARBAGE_FUNC( hb_libRelease )
  67. {
  68. /* do nothing */
  69. HB_SYMBOL_UNUSED( Cargo );
  70. }
  71. #if defined(HB_OS_WIN_32) || ( defined(HB_OS_LINUX) && !defined(__WATCOMC__) )
  72. static void * hb_parlib( int iParam )
  73. {
  74. void ** pDynLibPtr = ( void ** ) hb_parptrGC( hb_libRelease, iParam );
  75. return pDynLibPtr ? * pDynLibPtr : NULL;
  76. }
  77. #endif
  78. HB_FUNC( HB_LIBLOAD )
  79. {
  80. void * hDynLib = NULL;
  81. #if defined(HB_OS_WIN_32)
  82. if( hb_parclen( 1 ) > 0 )
  83. {
  84. int argc = hb_pcount() - 1, i;
  85. char **argv = NULL;
  86. if( argc > 0 )
  87. {
  88. argv = ( char** ) hb_xgrab( sizeof( char* ) * argc );
  89. for( i = 0; i < argc; ++i )
  90. {
  91. argv[i] = hb_parcx( i + 2 );
  92. }
  93. }
  94. /* use stack address as first level marker */
  95. hb_vmBeginSymbolGroup( ( void * ) hb_stackId(), TRUE );
  96. hDynLib = ( void * ) LoadLibraryA( hb_parc( 1 ) );
  97. /* set real marker */
  98. hb_vmInitSymbolGroup( hDynLib, argc, argv );
  99. if( argv )
  100. {
  101. hb_xfree( argv );
  102. }
  103. }
  104. #elif defined(HB_OS_LINUX) && !defined(__WATCOMC__)
  105. if( hb_parclen( 1 ) > 0 )
  106. {
  107. int argc = hb_pcount() - 1, i;
  108. char **argv = NULL;
  109. if( argc > 0 )
  110. {
  111. argv = ( char** ) hb_xgrab( sizeof( char* ) * argc );
  112. for( i = 0; i < argc; ++i )
  113. {
  114. argv[i] = hb_parcx( i + 2 );
  115. }
  116. }
  117. /* use stack address as first level marker */
  118. hb_vmBeginSymbolGroup( ( void * ) hb_stackId(), TRUE );
  119. hDynLib = ( void * ) dlopen( hb_parc( 1 ), RTLD_LAZY | RTLD_GLOBAL );
  120. /* set real marker */
  121. hb_vmInitSymbolGroup( hDynLib, argc, argv );
  122. if( argv )
  123. {
  124. hb_xfree( argv );
  125. }
  126. }
  127. #endif
  128. if( hDynLib )
  129. {
  130. void ** pLibPtr = ( void ** ) hb_gcAlloc( sizeof( void * ), hb_libRelease );
  131. * pLibPtr = hDynLib;
  132. hb_retptrGC( pLibPtr );
  133. }
  134. else
  135. hb_ret();
  136. }
  137. HB_FUNC( HB_LIBFREE )
  138. {
  139. #if defined(HB_OS_WIN_32)
  140. void * hDynLib = hb_parlib( 1 );
  141. if( hDynLib )
  142. {
  143. hb_vmExitSymbolGroup( hDynLib );
  144. hb_retl( FreeLibrary( ( HMODULE ) hDynLib ) );
  145. }
  146. else
  147. #elif defined(HB_OS_LINUX) && !defined(__WATCOMC__)
  148. void * hDynLib = hb_parlib( 1 );
  149. if( hDynLib )
  150. {
  151. hb_vmExitSymbolGroup( hDynLib );
  152. hb_retl( dlclose( hDynLib ) == 0 );
  153. }
  154. else
  155. #endif
  156. {
  157. hb_retl( FALSE );
  158. }
  159. }
  160. HB_FUNC( HB_LIBERROR )
  161. {
  162. #if defined(HB_OS_LINUX) && !defined(__WATCOMC__)
  163. hb_retc( dlerror() );
  164. #else
  165. hb_retc( NULL );
  166. #endif
  167. }
  168. /* Executes a Harbour pcode dynamically loaded DLL function or procedure
  169. * Syntax: HB_libDo( <cFuncName> [,<params...>] ) --> [<uResult>]
  170. */
  171. HB_FUNC( HB_LIBDO )
  172. {
  173. if( hb_parclen( 1 ) > 0 )
  174. {
  175. PHB_DYNS pDynSym = hb_dynsymFindName( hb_parc( 1 ) );
  176. if( pDynSym )
  177. {
  178. USHORT uiPCount = hb_pcount();
  179. USHORT uiParam;
  180. hb_vmPushSymbol( pDynSym->pSymbol );
  181. hb_vmPushNil();
  182. /* same logic here as from HB_FUNC( EVAL ) */
  183. for( uiParam = 2; uiParam <= uiPCount; uiParam++ )
  184. {
  185. hb_vmPush( hb_stackItemFromBase( uiParam ) );
  186. }
  187. hb_vmDo( uiPCount - 1 );
  188. }
  189. }
  190. }