2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 Change History (most recent first):
25 $Log: ExplorerPlugin.cpp,v $
26 Revision 1.6 2005/01/25 17:56:45 shersche
27 <rdar://problem/3911084> Load resource DLLs, get icons and bitmaps from resource DLLs
30 Revision 1.5 2004/09/15 10:33:54 shersche
31 <rdar://problem/3721611> Install XP toolbar button (8 bit mask) if running on XP platform, otherwise install 1 bit mask toolbar button
34 Revision 1.4 2004/07/13 21:24:21 rpantos
35 Fix for <rdar://problem/3701120>.
37 Revision 1.3 2004/06/26 14:12:07 shersche
38 Register the toolbar button
40 Revision 1.2 2004/06/24 20:09:39 shersche
42 Submitted by: herscher
44 Revision 1.1 2004/06/18 04:34:59 rpantos
45 Move to Clients from mDNSWindows
47 Revision 1.1 2004/01/30 03:01:56 bradley
48 Explorer Plugin to browse for DNS-SD advertised Web and FTP servers from within Internet Explorer.
54 // The following 2 includes have to be in this order and INITGUID must be defined here, before including the file
55 // that specifies the GUID(s), and nowhere else. The reason for this is that initguid.h doesn't provide separate
56 // define and declare macros for GUIDs so you have to #define INITGUID in the single file where you want to define
57 // your GUID then in all the other files that just need the GUID declared, INITGUID must not be defined.
61 #include "ExplorerPlugin.h"
66 #include "CommonServices.h"
67 #include "DebugServices.h"
69 #include "ClassFactory.h"
72 #include "loclibrary.h"
79 static char THIS_FILE
[] = __FILE__
;
83 #pragma mark == Prototypes ==
86 //===========================================================================================================================
88 //===========================================================================================================================
92 extern "C" BOOL WINAPI
DllMain( HINSTANCE inInstance
, DWORD inReason
, LPVOID inReserved
);
96 DEBUG_LOCAL OSStatus
MFCDLLProcessAttach( HINSTANCE inInstance
);
97 DEBUG_LOCAL
void MFCDLLProcessDetach( HINSTANCE inInstance
);
98 DEBUG_LOCAL
void MFCDLLThreadDetach( HINSTANCE inInstance
);
102 DEBUG_LOCAL OSStatus
RegisterServer( HINSTANCE inInstance
, CLSID inCLSID
, LPCTSTR inName
);
103 DEBUG_LOCAL OSStatus
RegisterCOMCategory( CLSID inCLSID
, CATID inCategoryID
, BOOL inRegister
);
105 // Stash away pointers to our resource DLLs
107 static HINSTANCE g_nonLocalizedResources
= NULL
;
108 static CString g_nonLocalizedResourcesName
;
109 static HINSTANCE g_localizedResources
= NULL
;
112 GetNonLocalizedResources()
114 return g_nonLocalizedResources
;
118 GetLocalizedResources()
120 return g_localizedResources
;
124 #pragma mark == Globals ==
127 //===========================================================================================================================
129 //===========================================================================================================================
131 HINSTANCE gInstance
= NULL
;
132 int gDLLRefCount
= 0;
137 #pragma mark == DLL Exports ==
140 //===========================================================================================================================
142 //===========================================================================================================================
144 BOOL WINAPI
DllMain( HINSTANCE inInstance
, DWORD inReason
, LPVOID inReserved
)
149 DEBUG_UNUSED( inReserved
);
154 case DLL_PROCESS_ATTACH
:
155 gInstance
= inInstance
;
156 debug_initialize( kDebugOutputTypeWindowsEventLog
, "DNSServices Bar", inInstance
);
157 debug_set_property( kDebugPropertyTagPrintLevel
, kDebugLevelTrace
);
158 dlog( kDebugLevelTrace
, "\nDllMain: process attach\n" );
160 err
= MFCDLLProcessAttach( inInstance
);
161 ok
= ( err
== kNoErr
);
162 require_noerr( err
, exit
);
165 case DLL_PROCESS_DETACH
:
166 dlog( kDebugLevelTrace
, "DllMain: process detach\n" );
167 MFCDLLProcessDetach( inInstance
);
170 case DLL_THREAD_ATTACH
:
171 dlog( kDebugLevelTrace
, "DllMain: thread attach\n" );
174 case DLL_THREAD_DETACH
:
175 dlog( kDebugLevelTrace
, "DllMain: thread detach\n" );
176 MFCDLLThreadDetach( inInstance
);
180 dlog( kDebugLevelTrace
, "DllMain: unknown reason code (%d)\n",inReason
);
188 //===========================================================================================================================
190 //===========================================================================================================================
192 STDAPI
DllCanUnloadNow( void )
194 dlog( kDebugLevelTrace
, "DllCanUnloadNow (refCount=%d)\n", gDLLRefCount
);
196 return( gDLLRefCount
== 0 );
199 //===========================================================================================================================
201 //===========================================================================================================================
203 STDAPI
DllGetClassObject( REFCLSID inCLSID
, REFIID inIID
, LPVOID
*outResult
)
207 ClassFactory
* factory
;
209 dlog( kDebugLevelTrace
, "DllGetClassObject\n" );
213 // Check if the class ID is supported.
215 ok
= IsEqualCLSID( inCLSID
, CLSID_ExplorerBar
);
216 require_action_quiet( ok
, exit
, err
= CLASS_E_CLASSNOTAVAILABLE
);
218 // Create the ClassFactory object.
223 factory
= new ClassFactory( inCLSID
);
227 // Do not let exception escape.
229 require_action( factory
, exit
, err
= E_OUTOFMEMORY
);
231 // Query for the specified interface. Release the factory since QueryInterface retains it.
233 err
= factory
->QueryInterface( inIID
, outResult
);
240 //===========================================================================================================================
242 //===========================================================================================================================
244 STDAPI
DllRegisterServer( void )
250 dlog( kDebugLevelTrace
, "DllRegisterServer\n" );
252 ok
= s
.LoadString( IDS_NAME
);
253 require_action( ok
, exit
, err
= E_UNEXPECTED
);
255 err
= RegisterServer( gInstance
, CLSID_ExplorerBar
, s
);
256 require_noerr( err
, exit
);
258 err
= RegisterCOMCategory( CLSID_ExplorerBar
, CATID_InfoBand
, TRUE
);
259 require_noerr( err
, exit
);
265 //===========================================================================================================================
266 // DllUnregisterServer
267 //===========================================================================================================================
269 STDAPI
DllUnregisterServer( void )
273 dlog( kDebugLevelTrace
, "DllUnregisterServer\n" );
275 err
= RegisterCOMCategory( CLSID_ExplorerBar
, CATID_InfoBand
, FALSE
);
276 require_noerr( err
, exit
);
284 #pragma mark == MFC Support ==
287 //===========================================================================================================================
288 // MFCDLLProcessAttach
289 //===========================================================================================================================
291 DEBUG_LOCAL OSStatus
MFCDLLProcessAttach( HINSTANCE inInstance
)
293 wchar_t resource
[MAX_PATH
];
295 _AFX_THREAD_STATE
* threadState
;
296 AFX_MODULE_STATE
* previousModuleState
;
303 // Simulate what is done in dllmodul.cpp.
305 threadState
= AfxGetThreadState();
306 check( threadState
);
307 previousModuleState
= threadState
->m_pPrevModuleState
;
309 ok
= AfxWinInit( inInstance
, NULL
, TEXT( "" ), 0 );
310 require_action( ok
, exit
, err
= kUnknownErr
);
313 require_action( ok
, exit
, err
= kNotInitializedErr
);
315 // Before we load the resources, let's load the error string
317 // errorMessage.LoadString( IDS_REINSTALL );
318 // errorCaption.LoadString( IDS_REINSTALL_CAPTION );
322 res
= PathForResource( inInstance
, L
"ExplorerPluginResources.dll", resource
, MAX_PATH
);
324 err
= translate_errno( res
!= 0, kUnknownErr
, kUnknownErr
);
325 require_noerr( err
, exit
);
327 g_nonLocalizedResources
= LoadLibrary( resource
);
328 translate_errno( g_nonLocalizedResources
, GetLastError(), kUnknownErr
);
329 require_noerr( err
, exit
);
331 g_nonLocalizedResourcesName
= resource
;
333 res
= PathForResource( inInstance
, L
"ExplorerPluginLocalized.dll", resource
, MAX_PATH
);
334 err
= translate_errno( res
!= 0, kUnknownErr
, kUnknownErr
);
335 require_noerr( err
, exit
);
337 g_localizedResources
= LoadLibrary( resource
);
338 translate_errno( g_localizedResources
, GetLastError(), kUnknownErr
);
339 require_noerr( err
, exit
);
341 AfxSetResourceHandle( g_localizedResources
);
343 ok
= app
->InitInstance();
344 require_action( ok
, exit
, err
= kUnknownErr
);
346 threadState
->m_pPrevModuleState
= previousModuleState
;
348 AfxInitLocalData( inInstance
);
362 threadState
->m_pPrevModuleState
= previousModuleState
;
367 //===========================================================================================================================
368 // MFCDLLProcessDetach
369 //===========================================================================================================================
371 DEBUG_LOCAL
void MFCDLLProcessDetach( HINSTANCE inInstance
)
375 // Simulate what is done in dllmodul.cpp.
384 if( AfxGetModuleThreadState()->m_nTempMapLock
!= 0 )
386 dlog( kDebugLevelWarning
, "Warning: Temp map lock count non-zero (%ld).\n", AfxGetModuleThreadState()->m_nTempMapLock
);
391 AfxUnlockTempMaps( -1 );
393 // Terminate the library before destructors are called.
396 AfxTermLocalData( inInstance
, TRUE
);
399 //===========================================================================================================================
401 //===========================================================================================================================
403 DEBUG_LOCAL
void MFCDLLThreadDetach( HINSTANCE inInstance
)
405 // Simulate what is done in dllmodul.cpp.
408 if( AfxGetModuleThreadState()->m_nTempMapLock
!= 0 )
410 dlog( kDebugLevelWarning
, "Warning: Temp map lock count non-zero (%ld).\n", AfxGetModuleThreadState()->m_nTempMapLock
);
415 AfxUnlockTempMaps( -1 );
416 AfxTermThread( inInstance
);
421 #pragma mark == Utilities ==
424 //===========================================================================================================================
426 //===========================================================================================================================
428 DEBUG_LOCAL OSStatus
RegisterServer( HINSTANCE inInstance
, CLSID inCLSID
, LPCTSTR inName
)
430 typedef struct RegistryBuilder RegistryBuilder
;
431 struct RegistryBuilder
440 LPWSTR clsidWideString
;
441 TCHAR clsidString
[ 64 ];
446 TCHAR keyName
[ MAX_PATH
];
447 TCHAR moduleName
[ MAX_PATH
] = TEXT( "" );
448 TCHAR data
[ MAX_PATH
];
449 RegistryBuilder entries
[] =
451 { HKEY_CLASSES_ROOT
, TEXT( "CLSID\\%s" ), NULL
, inName
},
452 { HKEY_CLASSES_ROOT
, TEXT( "CLSID\\%s\\InprocServer32" ), NULL
, moduleName
},
453 { HKEY_CLASSES_ROOT
, TEXT( "CLSID\\%s\\InprocServer32" ), TEXT( "ThreadingModel" ), TEXT( "Apartment" ) }
456 OSVERSIONINFO versionInfo
;
458 // Convert the CLSID to a string based on the encoding of this code (ANSI or Unicode).
460 err
= StringFromIID( inCLSID
, &clsidWideString
);
461 require_noerr( err
, exit
);
462 require_action( clsidWideString
, exit
, err
= kNoMemoryErr
);
465 lstrcpyn( clsidString
, clsidWideString
, sizeof_array( clsidString
) );
466 CoTaskMemFree( clsidWideString
);
468 nChars
= WideCharToMultiByte( CP_ACP
, 0, clsidWideString
, -1, clsidString
, sizeof_array( clsidString
), NULL
, NULL
);
469 err
= translate_errno( nChars
> 0, (OSStatus
) GetLastError(), kUnknownErr
);
470 CoTaskMemFree( clsidWideString
);
471 require_noerr( err
, exit
);
474 // Register the CLSID entries.
476 nChars
= GetModuleFileName( inInstance
, moduleName
, sizeof_array( moduleName
) );
477 err
= translate_errno( nChars
> 0, (OSStatus
) GetLastError(), kUnknownErr
);
478 require_noerr( err
, exit
);
480 n
= sizeof_array( entries
);
481 for( i
= 0; i
< n
; ++i
)
483 wsprintf( keyName
, entries
[ i
].subKey
, clsidString
);
484 err
= RegCreateKeyEx( entries
[ i
].rootKey
, keyName
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
);
485 require_noerr( err
, exit
);
487 size
= (DWORD
)( ( lstrlen( entries
[ i
].data
) + 1 ) * sizeof( TCHAR
) );
488 err
= RegSetValueEx( key
, entries
[ i
].valueName
, 0, REG_SZ
, (LPBYTE
) entries
[ i
].data
, size
);
490 require_noerr( err
, exit
);
493 // If running on NT, register the extension as approved.
495 versionInfo
.dwOSVersionInfoSize
= sizeof( versionInfo
);
496 GetVersionEx( &versionInfo
);
497 if( versionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
499 lstrcpyn( keyName
, TEXT( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved" ), sizeof_array( keyName
) );
500 err
= RegCreateKeyEx( HKEY_LOCAL_MACHINE
, keyName
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
);
501 require_noerr( err
, exit
);
503 lstrcpyn( data
, inName
, sizeof_array( data
) );
504 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
505 err
= RegSetValueEx( key
, clsidString
, 0, REG_SZ
, (LPBYTE
) data
, size
);
509 // register toolbar button
510 lstrcpyn( keyName
, TEXT( "SOFTWARE\\Microsoft\\Internet Explorer\\Extensions\\{7F9DB11C-E358-4ca6-A83D-ACC663939424}"), sizeof_array( keyName
) );
511 err
= RegCreateKeyEx( HKEY_LOCAL_MACHINE
, keyName
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
);
512 require_noerr( err
, exit
);
514 lstrcpyn( data
, L
"Yes", sizeof_array( data
) );
515 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
516 RegSetValueEx( key
, L
"Default Visible", 0, REG_SZ
, (LPBYTE
) data
, size
);
518 lstrcpyn( data
, inName
, sizeof_array( data
) );
519 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
520 RegSetValueEx( key
, L
"ButtonText", 0, REG_SZ
, (LPBYTE
) data
, size
);
522 lstrcpyn( data
, L
"{E0DD6CAB-2D10-11D2-8F1A-0000F87ABD16}", sizeof_array( data
) );
523 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
524 RegSetValueEx( key
, L
"CLSID", 0, REG_SZ
, (LPBYTE
) data
, size
);
526 lstrcpyn( data
, clsidString
, sizeof_array( data
) );
527 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
528 RegSetValueEx( key
, L
"BandCLSID", 0, REG_SZ
, (LPBYTE
) data
, size
);
530 // check if we're running XP or later
531 if ( ( versionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_NT
) &&
532 ( versionInfo
.dwMajorVersion
== 5 ) &&
533 ( versionInfo
.dwMinorVersion
>= 1 ) )
535 wsprintf( data
, L
"%s,%d", (LPCTSTR
) g_nonLocalizedResourcesName
, IDI_BUTTON_XP
);
536 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
537 RegSetValueEx( key
, L
"Icon", 0, REG_SZ
, (LPBYTE
) data
, size
);
539 wsprintf( data
, L
"%s,%d", (LPCTSTR
) g_nonLocalizedResourcesName
, IDI_BUTTON_XP
);
540 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
541 RegSetValueEx( key
, L
"HotIcon", 0, REG_SZ
, (LPBYTE
) data
, size
);
545 wsprintf( data
, L
"%s,%d", (LPCTSTR
) g_nonLocalizedResourcesName
, IDI_BUTTON_2K
);
546 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
547 RegSetValueEx( key
, L
"Icon", 0, REG_SZ
, (LPBYTE
) data
, size
);
549 wsprintf( data
, L
"%s,%d", (LPCTSTR
) g_nonLocalizedResourcesName
, IDI_BUTTON_2K
);
550 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
551 RegSetValueEx( key
, L
"HotIcon", 0, REG_SZ
, (LPBYTE
) data
, size
);
560 //===========================================================================================================================
561 // RegisterCOMCategory
562 //===========================================================================================================================
564 DEBUG_LOCAL OSStatus
RegisterCOMCategory( CLSID inCLSID
, CATID inCategoryID
, BOOL inRegister
)
569 err
= CoInitialize( NULL
);
570 require( SUCCEEDED( err
), exit
);
572 err
= CoCreateInstance( CLSID_StdComponentCategoriesMgr
, NULL
, CLSCTX_INPROC_SERVER
, IID_ICatRegister
, (LPVOID
*) &cat
);
573 check( SUCCEEDED( err
) );
574 if( SUCCEEDED( err
) )
578 err
= cat
->RegisterClassImplCategories( inCLSID
, 1, &inCategoryID
);
583 err
= cat
->UnRegisterClassImplCategories( inCLSID
, 1, &inCategoryID
);