diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 921b79e1a5..9184eef8cf 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -16,6 +16,27 @@ The license applies to all entries newer than 2009-04-28. */ +2010-12-10 09:08 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) + - harbour/contrib/hbwin/oleinit.c + * removed old OLE initialization and cleanup code which was working + only for main thread + + * harbour/contrib/hbwin/axcore.c + * harbour/contrib/hbwin/olecore.c + * harbour/contrib/hbwin/hbwin.hbp + * harbour/contrib/hbwin/hbolesrv.c + * harbour/contrib/hbwin/legacyco.c + + added new OLE init/exit code which is thread oriented + WARNING: now OLE is initialized when thread executes first time + one of Harbour OLE functions not at application startup. + C code which uses OLE without calling any Harbour PRG + level OLE function should initialize OLE for each thread + calling hb_oleInit() - it's very fast function so it can + be called without noticeable overhead. Please remember + that such initialization should be done for each thread. + Please test it - I do not have access to MS-Windows computer now + so I cannot make any test myself. + 2010-12-09 19:36 UTC+0200 Viktor Szakats (harbour.01 syenar.hu) * package/winuni/RELNOTES * INSTALL diff --git a/harbour/contrib/hbwin/axcore.c b/harbour/contrib/hbwin/axcore.c index b4b247cede..a191267b5c 100644 --- a/harbour/contrib/hbwin/axcore.c +++ b/harbour/contrib/hbwin/axcore.c @@ -99,6 +99,8 @@ static void hb_oleAxExit( void* cargo ) HB_BOOL hb_oleAxInit( void ) { + hb_oleInit(); + if( s_hLib == NULL ) { PHB_AX_WININIT pAtlAxWinInit; @@ -617,6 +619,8 @@ HB_FUNC( __AXREGISTERHANDLER ) /* ( pDisp, bHandler [, cIID] ) --> pSink */ void* hCLSID; const char * szIID; + hb_oleInit(); + szIID = hb_parc( 3 ); if( szIID && szIID[ 0 ] == '{' ) { diff --git a/harbour/contrib/hbwin/hbolesrv.c b/harbour/contrib/hbwin/hbolesrv.c index fe862916b4..1dcf6ea031 100644 --- a/harbour/contrib/hbwin/hbolesrv.c +++ b/harbour/contrib/hbwin/hbolesrv.c @@ -797,6 +797,8 @@ BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD dwReason, PVOID pvReserved ) if( s_fInit ) hb_vmInit( HB_FALSE ); + hb_oleInit(); + if( !s_fServerReady ) { PHB_DYNS pDynSym = hb_dynsymFind( "DLLMAIN" ); diff --git a/harbour/contrib/hbwin/hbwin.hbp b/harbour/contrib/hbwin/hbwin.hbp index 5f385c7b6f..18199cbedb 100644 --- a/harbour/contrib/hbwin/hbwin.hbp +++ b/harbour/contrib/hbwin/hbwin.hbp @@ -31,7 +31,6 @@ legacyco.c legacycp.c mapi.c olecore.c -oleinit.c hbolesrv.c wapi_alloc.c wapi_commctrl.c diff --git a/harbour/contrib/hbwin/legacyco.c b/harbour/contrib/hbwin/legacyco.c index a3603972d5..defd52db5e 100644 --- a/harbour/contrib/hbwin/legacyco.c +++ b/harbour/contrib/hbwin/legacyco.c @@ -121,6 +121,7 @@ HB_FUNC( MESSAGEBOX ) HB_FUNC( __OLEPDISP ) { + hb_oleInit(); hb_oleItemPut( hb_param( -1, HB_IT_ANY ), ( IDispatch * ) ( HB_PTRUINT ) hb_parnint( 1 ) ); } diff --git a/harbour/contrib/hbwin/olecore.c b/harbour/contrib/hbwin/olecore.c index cb868298ba..a9f9cf9ae6 100644 --- a/harbour/contrib/hbwin/olecore.c +++ b/harbour/contrib/hbwin/olecore.c @@ -89,15 +89,46 @@ typedef struct typedef struct { HRESULT lOleError; + int iInit; } HB_OLEDATA, * PHB_OLEDATA; -static HB_TSD_NEW( s_oleData, sizeof( HB_OLEDATA ), NULL, NULL ); -#define hb_getOleData() ( ( PHB_OLEDATA ) hb_stackGetTSD( &s_oleData ) ) +static void hb_oleDataInit( void * cargo ) +{ + PHB_OLEDATA pOleData = ( PHB_OLEDATA ) cargo; +#if defined( HB_OS_WIN_CE ) + if( CoInitializeEx( NULL, COINIT_APARTMENTTHREADED ) == S_OK ) +#else + if( OleInitialize( NULL ) == S_OK ) +#endif + pOleData->iInit = 1; +} + +static void hb_oleDataRelease( void * cargo ) +{ + PHB_OLEDATA pOleData = ( PHB_OLEDATA ) cargo; + + if( pOleData->iInit ) + { +#if defined( HB_OS_WIN_CE ) + CoUninitialize(); +#else + OleUninitialize(); +#endif + } +} + +static HB_TSD_NEW( s_oleData, sizeof( HB_OLEDATA ), hb_oleDataInit, hb_oleDataRelease ); +#define hb_getOleData() ( ( PHB_OLEDATA ) hb_stackGetTSD( &s_oleData ) ) HB_FUNC_EXTERN( WIN_OLEAUTO ); +HB_BOOL hb_oleInit( void ) +{ + return hb_getOleData()->iInit != 0; +} + void hb_oleSetError( HRESULT lOleError ) { hb_getOleData()->lOleError = lOleError; @@ -123,8 +154,6 @@ static void hb_olecore_init( void* cargo ) /* Never executed. Just force linkage */ HB_FUNC_EXEC( WIN_OLEAUTO ); } - - hb_oleInit(); } @@ -1285,6 +1314,8 @@ HB_FUNC( __OLECREATEOBJECT ) /* ( cOleName | cCLSID [, cIID ] ) */ const char* cID = hb_parc( 2 ); HRESULT lOleError; + hb_oleInit(); + if( cOleName ) { cCLSID = AnsiToWide( cOleName ); @@ -1335,6 +1366,8 @@ HB_FUNC( __OLEGETACTIVEOBJECT ) /* ( cOleName | cCLSID [, cIID ] ) */ const char* cID = hb_parc( 2 ); HRESULT lOleError; + hb_oleInit(); + if( cOleName ) { wCLSID = ( BSTR ) AnsiToWide( ( LPSTR ) cOleName ); @@ -1391,6 +1424,8 @@ HB_FUNC( __OLEENUMCREATE ) /* ( __hObj ) */ UINT uiArgErr; HRESULT lOleError; + hb_oleInit(); + if( hb_parl( 2 ) ) { hb_oleSetError( S_OK ); @@ -1447,18 +1482,24 @@ HB_FUNC( __OLEENUMCREATE ) /* ( __hObj ) */ HB_FUNC( __OLEENUMNEXT ) { IEnumVARIANT * pEnum = hb_oleenumParam( 1 ); - VARIANTARG variant; + HB_BOOL fResult = HB_FALSE; - VariantInit( &variant ); - if( HB_VTBL( pEnum )->Next( HB_THIS_( pEnum ) 1, &variant, NULL ) == S_OK ) + if( pEnum ) { - hb_oleVariantToItemEx( hb_stackReturnItem(), &variant, - ( HB_USHORT ) hb_parni( 3 ) ); - VariantClear( &variant ); - hb_storl( HB_TRUE, 2 ); + VARIANTARG variant; + + hb_oleInit(); + + VariantInit( &variant ); + if( HB_VTBL( pEnum )->Next( HB_THIS_( pEnum ) 1, &variant, NULL ) == S_OK ) + { + hb_oleVariantToItemEx( hb_stackReturnItem(), &variant, + ( HB_USHORT ) hb_parni( 3 ) ); + VariantClear( &variant ); + fResult = HB_TRUE; + } } - else - hb_storl( HB_FALSE, 2 ); + hb_storl( fResult, 2 ); } @@ -1546,6 +1587,8 @@ HB_FUNC( WIN_OLEAUTO___ONERROR ) HRESULT lOleError; HB_USHORT uiClass; + hb_oleInit(); + uiClass = hb_objGetClass( hb_stackSelfItem() ); /* Get object handle */ @@ -1643,6 +1686,8 @@ HB_FUNC( WIN_OLEAUTO___OPINDEX ) HB_BOOL fAssign; HB_USHORT uiClass; + hb_oleInit(); + uiClass = hb_objGetClass( hb_stackSelfItem() ); /* Get object handle */ diff --git a/harbour/contrib/hbwin/oleinit.c b/harbour/contrib/hbwin/oleinit.c deleted file mode 100644 index bb4d262472..0000000000 --- a/harbour/contrib/hbwin/oleinit.c +++ /dev/null @@ -1,100 +0,0 @@ -/* - * $Id$ - */ - -/* - * Harbour Project source code: - * OLE initialization - * - * Copyright 2008 Mindaugas Kavaliauskas - * www - http://harbour-project.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this software; see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, - * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/). - * - * As a special exception, the Harbour Project gives permission for - * additional uses of the text contained in its release of Harbour. - * - * The exception is that, if you link the Harbour libraries with other - * files to produce an executable, this does not by itself cause the - * resulting executable to be covered by the GNU General Public License. - * Your use of that executable is in no way restricted on account of - * linking the Harbour library code into it. - * - * This exception does not however invalidate any other reasons why - * the executable file might be covered by the GNU General Public License. - * - * This exception applies only to the code released by the Harbour - * Project under the name Harbour. If you copy code from other - * Harbour Project or Free Software Foundation releases into a copy of - * Harbour, as the General Public License permits, the exception does - * not apply to the code that you add in this way. To avoid misleading - * anyone as to the status of such modified files, you must delete - * this exception notice from them. - * - * If you write modifications of your own for Harbour, it is your choice - * whether to permit this exception to apply to your modifications. - * If you do not wish that, delete this exception notice. - * - */ - -#include "hbwinole.h" - -/* - * Duplicated OleUninitialize() call causes GPF. So, if a few OLE libraries - * is used inside Harbour code, you can expect GPF on application exit. - * This code does not implement any OLE interface except initialization. It is - * have to be used from all other OLE libraries. [Mindaugas] - */ - -static int s_iOleInit = 0; - -static void hb_ole_exit( void* cargo ) -{ - HB_SYMBOL_UNUSED( cargo ); - - if( s_iOleInit ) - { -#if defined( HB_OS_WIN_CE ) - CoUninitialize(); -#else - OleUninitialize(); -#endif - s_iOleInit = 0; - } -} - -HB_BOOL hb_oleInit( void ) -{ - HB_BOOL fResult = HB_TRUE; - - if( ! s_iOleInit ) - { -#if defined( HB_OS_WIN_CE ) - fResult = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED ) == S_OK; -#else - fResult = OleInitialize( NULL ) == S_OK; -#endif - if( fResult ) - { - hb_vmAtQuit( hb_ole_exit, NULL ); - s_iOleInit++; - } - } - else - s_iOleInit++; - - return fResult; -}