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.7 2005/02/23 02:00:45 shersche
27 <rdar://problem/4014479> Delete all the registry entries when component is unregistered
29 Revision 1.6 2005/01/25 17:56:45 shersche
30 <rdar://problem/3911084> Load resource DLLs, get icons and bitmaps from resource DLLs
33 Revision 1.5 2004/09/15 10:33:54 shersche
34 <rdar://problem/3721611> Install XP toolbar button (8 bit mask) if running on XP platform, otherwise install 1 bit mask toolbar button
37 Revision 1.4 2004/07/13 21:24:21 rpantos
38 Fix for <rdar://problem/3701120>.
40 Revision 1.3 2004/06/26 14:12:07 shersche
41 Register the toolbar button
43 Revision 1.2 2004/06/24 20:09:39 shersche
45 Submitted by: herscher
47 Revision 1.1 2004/06/18 04:34:59 rpantos
48 Move to Clients from mDNSWindows
50 Revision 1.1 2004/01/30 03:01:56 bradley
51 Explorer Plugin to browse for DNS-SD advertised Web and FTP servers from within Internet Explorer.
57 // The following 2 includes have to be in this order and INITGUID must be defined here, before including the file
58 // that specifies the GUID(s), and nowhere else. The reason for this is that initguid.h doesn't provide separate
59 // define and declare macros for GUIDs so you have to #define INITGUID in the single file where you want to define
60 // your GUID then in all the other files that just need the GUID declared, INITGUID must not be defined.
64 #include "ExplorerPlugin.h"
69 #include "CommonServices.h"
70 #include "DebugServices.h"
72 #include "ClassFactory.h"
75 #include "loclibrary.h"
82 static char THIS_FILE
[] = __FILE__
;
86 #pragma mark == Prototypes ==
89 //===========================================================================================================================
91 //===========================================================================================================================
95 extern "C" BOOL WINAPI
DllMain( HINSTANCE inInstance
, DWORD inReason
, LPVOID inReserved
);
99 DEBUG_LOCAL OSStatus
MFCDLLProcessAttach( HINSTANCE inInstance
);
100 DEBUG_LOCAL
void MFCDLLProcessDetach( HINSTANCE inInstance
);
101 DEBUG_LOCAL
void MFCDLLThreadDetach( HINSTANCE inInstance
);
105 DEBUG_LOCAL OSStatus
RegisterServer( HINSTANCE inInstance
, CLSID inCLSID
, LPCTSTR inName
);
106 DEBUG_LOCAL OSStatus
RegisterCOMCategory( CLSID inCLSID
, CATID inCategoryID
, BOOL inRegister
);
107 DEBUG_LOCAL OSStatus
UnregisterServer( CLSID inCLSID
);
108 DEBUG_LOCAL OSStatus
MyRegDeleteKey( HKEY hKeyRoot
, LPTSTR lpSubKey
);
110 // Stash away pointers to our resource DLLs
112 static HINSTANCE g_nonLocalizedResources
= NULL
;
113 static CString g_nonLocalizedResourcesName
;
114 static HINSTANCE g_localizedResources
= NULL
;
117 GetNonLocalizedResources()
119 return g_nonLocalizedResources
;
123 GetLocalizedResources()
125 return g_localizedResources
;
129 #pragma mark == Globals ==
132 //===========================================================================================================================
134 //===========================================================================================================================
136 HINSTANCE gInstance
= NULL
;
137 int gDLLRefCount
= 0;
142 #pragma mark == DLL Exports ==
145 //===========================================================================================================================
147 //===========================================================================================================================
149 BOOL WINAPI
DllMain( HINSTANCE inInstance
, DWORD inReason
, LPVOID inReserved
)
154 DEBUG_UNUSED( inReserved
);
159 case DLL_PROCESS_ATTACH
:
160 gInstance
= inInstance
;
161 debug_initialize( kDebugOutputTypeWindowsEventLog
, "DNSServices Bar", inInstance
);
162 debug_set_property( kDebugPropertyTagPrintLevel
, kDebugLevelTrace
);
163 dlog( kDebugLevelTrace
, "\nDllMain: process attach\n" );
165 err
= MFCDLLProcessAttach( inInstance
);
166 ok
= ( err
== kNoErr
);
167 require_noerr( err
, exit
);
170 case DLL_PROCESS_DETACH
:
171 dlog( kDebugLevelTrace
, "DllMain: process detach\n" );
172 MFCDLLProcessDetach( inInstance
);
175 case DLL_THREAD_ATTACH
:
176 dlog( kDebugLevelTrace
, "DllMain: thread attach\n" );
179 case DLL_THREAD_DETACH
:
180 dlog( kDebugLevelTrace
, "DllMain: thread detach\n" );
181 MFCDLLThreadDetach( inInstance
);
185 dlog( kDebugLevelTrace
, "DllMain: unknown reason code (%d)\n",inReason
);
193 //===========================================================================================================================
195 //===========================================================================================================================
197 STDAPI
DllCanUnloadNow( void )
199 dlog( kDebugLevelTrace
, "DllCanUnloadNow (refCount=%d)\n", gDLLRefCount
);
201 return( gDLLRefCount
== 0 );
204 //===========================================================================================================================
206 //===========================================================================================================================
208 STDAPI
DllGetClassObject( REFCLSID inCLSID
, REFIID inIID
, LPVOID
*outResult
)
212 ClassFactory
* factory
;
214 dlog( kDebugLevelTrace
, "DllGetClassObject\n" );
218 // Check if the class ID is supported.
220 ok
= IsEqualCLSID( inCLSID
, CLSID_ExplorerBar
);
221 require_action_quiet( ok
, exit
, err
= CLASS_E_CLASSNOTAVAILABLE
);
223 // Create the ClassFactory object.
228 factory
= new ClassFactory( inCLSID
);
232 // Do not let exception escape.
234 require_action( factory
, exit
, err
= E_OUTOFMEMORY
);
236 // Query for the specified interface. Release the factory since QueryInterface retains it.
238 err
= factory
->QueryInterface( inIID
, outResult
);
245 //===========================================================================================================================
247 //===========================================================================================================================
249 STDAPI
DllRegisterServer( void )
255 dlog( kDebugLevelTrace
, "DllRegisterServer\n" );
257 ok
= s
.LoadString( IDS_NAME
);
258 require_action( ok
, exit
, err
= E_UNEXPECTED
);
260 err
= RegisterServer( gInstance
, CLSID_ExplorerBar
, s
);
261 require_noerr( err
, exit
);
263 err
= RegisterCOMCategory( CLSID_ExplorerBar
, CATID_InfoBand
, TRUE
);
264 require_noerr( err
, exit
);
270 //===========================================================================================================================
271 // DllUnregisterServer
272 //===========================================================================================================================
274 STDAPI
DllUnregisterServer( void )
278 dlog( kDebugLevelTrace
, "DllUnregisterServer\n" );
280 err
= RegisterCOMCategory( CLSID_ExplorerBar
, CATID_InfoBand
, FALSE
);
281 require_noerr( err
, exit
);
283 err
= UnregisterServer( CLSID_ExplorerBar
);
284 require_noerr( err
, exit
);
292 #pragma mark == MFC Support ==
295 //===========================================================================================================================
296 // MFCDLLProcessAttach
297 //===========================================================================================================================
299 DEBUG_LOCAL OSStatus
MFCDLLProcessAttach( HINSTANCE inInstance
)
301 wchar_t resource
[MAX_PATH
];
303 _AFX_THREAD_STATE
* threadState
;
304 AFX_MODULE_STATE
* previousModuleState
;
311 // Simulate what is done in dllmodul.cpp.
313 threadState
= AfxGetThreadState();
314 check( threadState
);
315 previousModuleState
= threadState
->m_pPrevModuleState
;
317 ok
= AfxWinInit( inInstance
, NULL
, TEXT( "" ), 0 );
318 require_action( ok
, exit
, err
= kUnknownErr
);
321 require_action( ok
, exit
, err
= kNotInitializedErr
);
323 // Before we load the resources, let's load the error string
325 // errorMessage.LoadString( IDS_REINSTALL );
326 // errorCaption.LoadString( IDS_REINSTALL_CAPTION );
330 res
= PathForResource( inInstance
, L
"ExplorerPluginResources.dll", resource
, MAX_PATH
);
332 err
= translate_errno( res
!= 0, kUnknownErr
, kUnknownErr
);
333 require_noerr( err
, exit
);
335 g_nonLocalizedResources
= LoadLibrary( resource
);
336 translate_errno( g_nonLocalizedResources
, GetLastError(), kUnknownErr
);
337 require_noerr( err
, exit
);
339 g_nonLocalizedResourcesName
= resource
;
341 res
= PathForResource( inInstance
, L
"ExplorerPluginLocalized.dll", resource
, MAX_PATH
);
342 err
= translate_errno( res
!= 0, kUnknownErr
, kUnknownErr
);
343 require_noerr( err
, exit
);
345 g_localizedResources
= LoadLibrary( resource
);
346 translate_errno( g_localizedResources
, GetLastError(), kUnknownErr
);
347 require_noerr( err
, exit
);
349 AfxSetResourceHandle( g_localizedResources
);
351 ok
= app
->InitInstance();
352 require_action( ok
, exit
, err
= kUnknownErr
);
354 threadState
->m_pPrevModuleState
= previousModuleState
;
356 AfxInitLocalData( inInstance
);
370 threadState
->m_pPrevModuleState
= previousModuleState
;
375 //===========================================================================================================================
376 // MFCDLLProcessDetach
377 //===========================================================================================================================
379 DEBUG_LOCAL
void MFCDLLProcessDetach( HINSTANCE inInstance
)
383 // Simulate what is done in dllmodul.cpp.
392 if( AfxGetModuleThreadState()->m_nTempMapLock
!= 0 )
394 dlog( kDebugLevelWarning
, "Warning: Temp map lock count non-zero (%ld).\n", AfxGetModuleThreadState()->m_nTempMapLock
);
399 AfxUnlockTempMaps( -1 );
401 // Terminate the library before destructors are called.
404 AfxTermLocalData( inInstance
, TRUE
);
407 //===========================================================================================================================
409 //===========================================================================================================================
411 DEBUG_LOCAL
void MFCDLLThreadDetach( HINSTANCE inInstance
)
413 // Simulate what is done in dllmodul.cpp.
416 if( AfxGetModuleThreadState()->m_nTempMapLock
!= 0 )
418 dlog( kDebugLevelWarning
, "Warning: Temp map lock count non-zero (%ld).\n", AfxGetModuleThreadState()->m_nTempMapLock
);
423 AfxUnlockTempMaps( -1 );
424 AfxTermThread( inInstance
);
429 #pragma mark == Utilities ==
432 //===========================================================================================================================
434 //===========================================================================================================================
436 DEBUG_LOCAL OSStatus
RegisterServer( HINSTANCE inInstance
, CLSID inCLSID
, LPCTSTR inName
)
438 typedef struct RegistryBuilder RegistryBuilder
;
439 struct RegistryBuilder
448 LPWSTR clsidWideString
;
449 TCHAR clsidString
[ 64 ];
454 TCHAR keyName
[ MAX_PATH
];
455 TCHAR moduleName
[ MAX_PATH
] = TEXT( "" );
456 TCHAR data
[ MAX_PATH
];
457 RegistryBuilder entries
[] =
459 { HKEY_CLASSES_ROOT
, TEXT( "CLSID\\%s" ), NULL
, inName
},
460 { HKEY_CLASSES_ROOT
, TEXT( "CLSID\\%s\\InprocServer32" ), NULL
, moduleName
},
461 { HKEY_CLASSES_ROOT
, TEXT( "CLSID\\%s\\InprocServer32" ), TEXT( "ThreadingModel" ), TEXT( "Apartment" ) }
464 OSVERSIONINFO versionInfo
;
466 // Convert the CLSID to a string based on the encoding of this code (ANSI or Unicode).
468 err
= StringFromIID( inCLSID
, &clsidWideString
);
469 require_noerr( err
, exit
);
470 require_action( clsidWideString
, exit
, err
= kNoMemoryErr
);
473 lstrcpyn( clsidString
, clsidWideString
, sizeof_array( clsidString
) );
474 CoTaskMemFree( clsidWideString
);
476 nChars
= WideCharToMultiByte( CP_ACP
, 0, clsidWideString
, -1, clsidString
, sizeof_array( clsidString
), NULL
, NULL
);
477 err
= translate_errno( nChars
> 0, (OSStatus
) GetLastError(), kUnknownErr
);
478 CoTaskMemFree( clsidWideString
);
479 require_noerr( err
, exit
);
482 // Register the CLSID entries.
484 nChars
= GetModuleFileName( inInstance
, moduleName
, sizeof_array( moduleName
) );
485 err
= translate_errno( nChars
> 0, (OSStatus
) GetLastError(), kUnknownErr
);
486 require_noerr( err
, exit
);
488 n
= sizeof_array( entries
);
489 for( i
= 0; i
< n
; ++i
)
491 wsprintf( keyName
, entries
[ i
].subKey
, clsidString
);
492 err
= RegCreateKeyEx( entries
[ i
].rootKey
, keyName
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
);
493 require_noerr( err
, exit
);
495 size
= (DWORD
)( ( lstrlen( entries
[ i
].data
) + 1 ) * sizeof( TCHAR
) );
496 err
= RegSetValueEx( key
, entries
[ i
].valueName
, 0, REG_SZ
, (LPBYTE
) entries
[ i
].data
, size
);
498 require_noerr( err
, exit
);
501 // If running on NT, register the extension as approved.
503 versionInfo
.dwOSVersionInfoSize
= sizeof( versionInfo
);
504 GetVersionEx( &versionInfo
);
505 if( versionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
507 lstrcpyn( keyName
, TEXT( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved" ), sizeof_array( keyName
) );
508 err
= RegCreateKeyEx( HKEY_LOCAL_MACHINE
, keyName
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
);
509 require_noerr( err
, exit
);
511 lstrcpyn( data
, inName
, sizeof_array( data
) );
512 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
513 err
= RegSetValueEx( key
, clsidString
, 0, REG_SZ
, (LPBYTE
) data
, size
);
517 // register toolbar button
518 lstrcpyn( keyName
, TEXT( "SOFTWARE\\Microsoft\\Internet Explorer\\Extensions\\{7F9DB11C-E358-4ca6-A83D-ACC663939424}"), sizeof_array( keyName
) );
519 err
= RegCreateKeyEx( HKEY_LOCAL_MACHINE
, keyName
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
);
520 require_noerr( err
, exit
);
522 lstrcpyn( data
, L
"Yes", sizeof_array( data
) );
523 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
524 RegSetValueEx( key
, L
"Default Visible", 0, REG_SZ
, (LPBYTE
) data
, size
);
526 lstrcpyn( data
, inName
, sizeof_array( data
) );
527 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
528 RegSetValueEx( key
, L
"ButtonText", 0, REG_SZ
, (LPBYTE
) data
, size
);
530 lstrcpyn( data
, L
"{E0DD6CAB-2D10-11D2-8F1A-0000F87ABD16}", sizeof_array( data
) );
531 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
532 RegSetValueEx( key
, L
"CLSID", 0, REG_SZ
, (LPBYTE
) data
, size
);
534 lstrcpyn( data
, clsidString
, sizeof_array( data
) );
535 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
536 RegSetValueEx( key
, L
"BandCLSID", 0, REG_SZ
, (LPBYTE
) data
, size
);
538 // check if we're running XP or later
539 if ( ( versionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_NT
) &&
540 ( versionInfo
.dwMajorVersion
== 5 ) &&
541 ( versionInfo
.dwMinorVersion
>= 1 ) )
543 wsprintf( data
, L
"%s,%d", (LPCTSTR
) g_nonLocalizedResourcesName
, IDI_BUTTON_XP
);
544 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
545 RegSetValueEx( key
, L
"Icon", 0, REG_SZ
, (LPBYTE
) data
, size
);
547 wsprintf( data
, L
"%s,%d", (LPCTSTR
) g_nonLocalizedResourcesName
, IDI_BUTTON_XP
);
548 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
549 RegSetValueEx( key
, L
"HotIcon", 0, REG_SZ
, (LPBYTE
) data
, size
);
553 wsprintf( data
, L
"%s,%d", (LPCTSTR
) g_nonLocalizedResourcesName
, IDI_BUTTON_2K
);
554 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
555 RegSetValueEx( key
, L
"Icon", 0, REG_SZ
, (LPBYTE
) data
, size
);
557 wsprintf( data
, L
"%s,%d", (LPCTSTR
) g_nonLocalizedResourcesName
, IDI_BUTTON_2K
);
558 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
559 RegSetValueEx( key
, L
"HotIcon", 0, REG_SZ
, (LPBYTE
) data
, size
);
568 //===========================================================================================================================
569 // RegisterCOMCategory
570 //===========================================================================================================================
572 DEBUG_LOCAL OSStatus
RegisterCOMCategory( CLSID inCLSID
, CATID inCategoryID
, BOOL inRegister
)
577 err
= CoInitialize( NULL
);
578 require( SUCCEEDED( err
), exit
);
580 err
= CoCreateInstance( CLSID_StdComponentCategoriesMgr
, NULL
, CLSCTX_INPROC_SERVER
, IID_ICatRegister
, (LPVOID
*) &cat
);
581 check( SUCCEEDED( err
) );
582 if( SUCCEEDED( err
) )
586 err
= cat
->RegisterClassImplCategories( inCLSID
, 1, &inCategoryID
);
591 err
= cat
->UnRegisterClassImplCategories( inCLSID
, 1, &inCategoryID
);
603 //===========================================================================================================================
605 //===========================================================================================================================
607 DEBUG_LOCAL OSStatus
UnregisterServer( CLSID inCLSID
)
610 LPWSTR clsidWideString
;
611 TCHAR clsidString
[ 64 ];
613 TCHAR keyName
[ MAX_PATH
* 2 ];
614 OSVERSIONINFO versionInfo
;
616 // Convert the CLSID to a string based on the encoding of this code (ANSI or Unicode).
618 err
= StringFromIID( inCLSID
, &clsidWideString
);
619 require_noerr( err
, exit
);
620 require_action( clsidWideString
, exit
, err
= kNoMemoryErr
);
623 lstrcpyn( clsidString
, clsidWideString
, sizeof_array( clsidString
) );
624 CoTaskMemFree( clsidWideString
);
626 nChars
= WideCharToMultiByte( CP_ACP
, 0, clsidWideString
, -1, clsidString
, sizeof_array( clsidString
), NULL
, NULL
);
627 err
= translate_errno( nChars
> 0, (OSStatus
) GetLastError(), kUnknownErr
);
628 CoTaskMemFree( clsidWideString
);
629 require_noerr( err
, exit
);
632 wsprintf( keyName
, L
"CLSID\\%s", clsidString
);
633 MyRegDeleteKey( HKEY_CLASSES_ROOT
, keyName
);
635 // If running on NT, de-register the extension as approved.
637 versionInfo
.dwOSVersionInfoSize
= sizeof( versionInfo
);
638 GetVersionEx( &versionInfo
);
639 if( versionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
641 lstrcpyn( keyName
, TEXT( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved" ), sizeof_array( keyName
) );
642 err
= RegCreateKeyEx( HKEY_LOCAL_MACHINE
, keyName
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
);
643 require_noerr( err
, exit
);
645 RegDeleteValue( key
, clsidString
);
647 err
= RegCloseKey( key
);
648 require_noerr( err
, exit
);
651 // de-register toolbar button
653 lstrcpyn( keyName
, TEXT( "SOFTWARE\\Microsoft\\Internet Explorer\\Extensions\\{7F9DB11C-E358-4ca6-A83D-ACC663939424}"), sizeof_array( keyName
) );
654 MyRegDeleteKey( HKEY_LOCAL_MACHINE
, keyName
);
662 //===========================================================================================================================
664 //===========================================================================================================================
666 DEBUG_LOCAL OSStatus
MyRegDeleteKey( HKEY hKeyRoot
, LPTSTR lpSubKey
)
671 TCHAR szName
[MAX_PATH
];
675 // First, see if we can delete the key without having to recurse.
677 err
= RegDeleteKey( hKeyRoot
, lpSubKey
);
684 err
= RegOpenKeyEx( hKeyRoot
, lpSubKey
, 0, KEY_READ
, &hKey
);
685 require_noerr( err
, exit
);
687 // Check for an ending slash and add one if it is missing.
689 lpEnd
= lpSubKey
+ lstrlen(lpSubKey
);
691 if ( *( lpEnd
- 1 ) != TEXT( '\\' ) )
698 // Enumerate the keys
701 err
= RegEnumKeyEx(hKey
, 0, szName
, &dwSize
, NULL
, NULL
, NULL
, &ftWrite
);
707 lstrcpy (lpEnd
, szName
);
709 if ( !MyRegDeleteKey( hKeyRoot
, lpSubKey
) )
716 err
= RegEnumKeyEx( hKey
, 0, szName
, &dwSize
, NULL
, NULL
, NULL
, &ftWrite
);
727 // Try again to delete the key.
729 err
= RegDeleteKey(hKeyRoot
, lpSubKey
);
730 require_noerr( err
, exit
);