1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 Change History (most recent first):
19 $Log: ExplorerPlugin.cpp,v $
20 Revision 1.9 2006/08/14 23:24:00 cheshire
21 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
23 Revision 1.8 2005/06/30 18:01:54 shersche
24 <rdar://problem/4130635> Cause IE to rebuild cache so we don't have to reboot following an install.
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
;
128 // This is the class GUID for an undocumented hook into IE that will allow us to register
129 // and have IE notice our new ExplorerBar without rebooting.
130 // {8C7461EF-2B13-11d2-BE35-3078302C2030}
132 DEFINE_GUID(CLSID_CompCatCacheDaemon
,
133 0x8C7461EF, 0x2b13, 0x11d2, 0xbe, 0x35, 0x30, 0x78, 0x30, 0x2c, 0x20, 0x30);
137 #pragma mark == Globals ==
140 //===========================================================================================================================
142 //===========================================================================================================================
144 HINSTANCE gInstance
= NULL
;
145 int gDLLRefCount
= 0;
150 #pragma mark == DLL Exports ==
153 //===========================================================================================================================
155 //===========================================================================================================================
157 BOOL WINAPI
DllMain( HINSTANCE inInstance
, DWORD inReason
, LPVOID inReserved
)
162 DEBUG_UNUSED( inReserved
);
167 case DLL_PROCESS_ATTACH
:
168 gInstance
= inInstance
;
169 debug_initialize( kDebugOutputTypeWindowsEventLog
, "DNSServices Bar", inInstance
);
170 debug_set_property( kDebugPropertyTagPrintLevel
, kDebugLevelTrace
);
171 dlog( kDebugLevelTrace
, "\nDllMain: process attach\n" );
173 err
= MFCDLLProcessAttach( inInstance
);
174 ok
= ( err
== kNoErr
);
175 require_noerr( err
, exit
);
178 case DLL_PROCESS_DETACH
:
179 dlog( kDebugLevelTrace
, "DllMain: process detach\n" );
180 MFCDLLProcessDetach( inInstance
);
183 case DLL_THREAD_ATTACH
:
184 dlog( kDebugLevelTrace
, "DllMain: thread attach\n" );
187 case DLL_THREAD_DETACH
:
188 dlog( kDebugLevelTrace
, "DllMain: thread detach\n" );
189 MFCDLLThreadDetach( inInstance
);
193 dlog( kDebugLevelTrace
, "DllMain: unknown reason code (%d)\n",inReason
);
201 //===========================================================================================================================
203 //===========================================================================================================================
205 STDAPI
DllCanUnloadNow( void )
207 dlog( kDebugLevelTrace
, "DllCanUnloadNow (refCount=%d)\n", gDLLRefCount
);
209 return( gDLLRefCount
== 0 );
212 //===========================================================================================================================
214 //===========================================================================================================================
216 STDAPI
DllGetClassObject( REFCLSID inCLSID
, REFIID inIID
, LPVOID
*outResult
)
220 ClassFactory
* factory
;
222 dlog( kDebugLevelTrace
, "DllGetClassObject\n" );
226 // Check if the class ID is supported.
228 ok
= IsEqualCLSID( inCLSID
, CLSID_ExplorerBar
);
229 require_action_quiet( ok
, exit
, err
= CLASS_E_CLASSNOTAVAILABLE
);
231 // Create the ClassFactory object.
236 factory
= new ClassFactory( inCLSID
);
240 // Do not let exception escape.
242 require_action( factory
, exit
, err
= E_OUTOFMEMORY
);
244 // Query for the specified interface. Release the factory since QueryInterface retains it.
246 err
= factory
->QueryInterface( inIID
, outResult
);
253 //===========================================================================================================================
255 //===========================================================================================================================
257 STDAPI
DllRegisterServer( void )
259 IRunnableTask
* pTask
= NULL
;
264 dlog( kDebugLevelTrace
, "DllRegisterServer\n" );
266 ok
= s
.LoadString( IDS_NAME
);
267 require_action( ok
, exit
, err
= E_UNEXPECTED
);
269 err
= RegisterServer( gInstance
, CLSID_ExplorerBar
, s
);
270 require_noerr( err
, exit
);
272 err
= RegisterCOMCategory( CLSID_ExplorerBar
, CATID_InfoBand
, TRUE
);
273 require_noerr( err
, exit
);
275 // <rdar://problem/4130635> Clear IE cache so it will rebuild the cache when it runs next. This
276 // will allow us to install and not reboot
278 err
= CoCreateInstance(CLSID_CompCatCacheDaemon
, NULL
, CLSCTX_INPROC
, IID_IRunnableTask
, (void**) &pTask
);
279 require_noerr( err
, exit
);
288 //===========================================================================================================================
289 // DllUnregisterServer
290 //===========================================================================================================================
292 STDAPI
DllUnregisterServer( void )
296 dlog( kDebugLevelTrace
, "DllUnregisterServer\n" );
298 err
= RegisterCOMCategory( CLSID_ExplorerBar
, CATID_InfoBand
, FALSE
);
299 require_noerr( err
, exit
);
301 err
= UnregisterServer( CLSID_ExplorerBar
);
302 require_noerr( err
, exit
);
310 #pragma mark == MFC Support ==
313 //===========================================================================================================================
314 // MFCDLLProcessAttach
315 //===========================================================================================================================
317 DEBUG_LOCAL OSStatus
MFCDLLProcessAttach( HINSTANCE inInstance
)
319 wchar_t resource
[MAX_PATH
];
321 _AFX_THREAD_STATE
* threadState
;
322 AFX_MODULE_STATE
* previousModuleState
;
329 // Simulate what is done in dllmodul.cpp.
331 threadState
= AfxGetThreadState();
332 check( threadState
);
333 previousModuleState
= threadState
->m_pPrevModuleState
;
335 ok
= AfxWinInit( inInstance
, NULL
, TEXT( "" ), 0 );
336 require_action( ok
, exit
, err
= kUnknownErr
);
339 require_action( ok
, exit
, err
= kNotInitializedErr
);
341 // Before we load the resources, let's load the error string
343 // errorMessage.LoadString( IDS_REINSTALL );
344 // errorCaption.LoadString( IDS_REINSTALL_CAPTION );
348 res
= PathForResource( inInstance
, L
"ExplorerPluginResources.dll", resource
, MAX_PATH
);
350 err
= translate_errno( res
!= 0, kUnknownErr
, kUnknownErr
);
351 require_noerr( err
, exit
);
353 g_nonLocalizedResources
= LoadLibrary( resource
);
354 translate_errno( g_nonLocalizedResources
, GetLastError(), kUnknownErr
);
355 require_noerr( err
, exit
);
357 g_nonLocalizedResourcesName
= resource
;
359 res
= PathForResource( inInstance
, L
"ExplorerPluginLocalized.dll", resource
, MAX_PATH
);
360 err
= translate_errno( res
!= 0, kUnknownErr
, kUnknownErr
);
361 require_noerr( err
, exit
);
363 g_localizedResources
= LoadLibrary( resource
);
364 translate_errno( g_localizedResources
, GetLastError(), kUnknownErr
);
365 require_noerr( err
, exit
);
367 AfxSetResourceHandle( g_localizedResources
);
369 ok
= app
->InitInstance();
370 require_action( ok
, exit
, err
= kUnknownErr
);
372 threadState
->m_pPrevModuleState
= previousModuleState
;
374 AfxInitLocalData( inInstance
);
388 threadState
->m_pPrevModuleState
= previousModuleState
;
393 //===========================================================================================================================
394 // MFCDLLProcessDetach
395 //===========================================================================================================================
397 DEBUG_LOCAL
void MFCDLLProcessDetach( HINSTANCE inInstance
)
401 // Simulate what is done in dllmodul.cpp.
410 if( AfxGetModuleThreadState()->m_nTempMapLock
!= 0 )
412 dlog( kDebugLevelWarning
, "Warning: Temp map lock count non-zero (%ld).\n", AfxGetModuleThreadState()->m_nTempMapLock
);
417 AfxUnlockTempMaps( -1 );
419 // Terminate the library before destructors are called.
422 AfxTermLocalData( inInstance
, TRUE
);
425 //===========================================================================================================================
427 //===========================================================================================================================
429 DEBUG_LOCAL
void MFCDLLThreadDetach( HINSTANCE inInstance
)
431 // Simulate what is done in dllmodul.cpp.
434 if( AfxGetModuleThreadState()->m_nTempMapLock
!= 0 )
436 dlog( kDebugLevelWarning
, "Warning: Temp map lock count non-zero (%ld).\n", AfxGetModuleThreadState()->m_nTempMapLock
);
441 AfxUnlockTempMaps( -1 );
442 AfxTermThread( inInstance
);
447 #pragma mark == Utilities ==
450 //===========================================================================================================================
452 //===========================================================================================================================
454 DEBUG_LOCAL OSStatus
RegisterServer( HINSTANCE inInstance
, CLSID inCLSID
, LPCTSTR inName
)
456 typedef struct RegistryBuilder RegistryBuilder
;
457 struct RegistryBuilder
466 LPWSTR clsidWideString
;
467 TCHAR clsidString
[ 64 ];
472 TCHAR keyName
[ MAX_PATH
];
473 TCHAR moduleName
[ MAX_PATH
] = TEXT( "" );
474 TCHAR data
[ MAX_PATH
];
475 RegistryBuilder entries
[] =
477 { HKEY_CLASSES_ROOT
, TEXT( "CLSID\\%s" ), NULL
, inName
},
478 { HKEY_CLASSES_ROOT
, TEXT( "CLSID\\%s\\InprocServer32" ), NULL
, moduleName
},
479 { HKEY_CLASSES_ROOT
, TEXT( "CLSID\\%s\\InprocServer32" ), TEXT( "ThreadingModel" ), TEXT( "Apartment" ) }
482 OSVERSIONINFO versionInfo
;
484 // Convert the CLSID to a string based on the encoding of this code (ANSI or Unicode).
486 err
= StringFromIID( inCLSID
, &clsidWideString
);
487 require_noerr( err
, exit
);
488 require_action( clsidWideString
, exit
, err
= kNoMemoryErr
);
491 lstrcpyn( clsidString
, clsidWideString
, sizeof_array( clsidString
) );
492 CoTaskMemFree( clsidWideString
);
494 nChars
= WideCharToMultiByte( CP_ACP
, 0, clsidWideString
, -1, clsidString
, sizeof_array( clsidString
), NULL
, NULL
);
495 err
= translate_errno( nChars
> 0, (OSStatus
) GetLastError(), kUnknownErr
);
496 CoTaskMemFree( clsidWideString
);
497 require_noerr( err
, exit
);
500 // Register the CLSID entries.
502 nChars
= GetModuleFileName( inInstance
, moduleName
, sizeof_array( moduleName
) );
503 err
= translate_errno( nChars
> 0, (OSStatus
) GetLastError(), kUnknownErr
);
504 require_noerr( err
, exit
);
506 n
= sizeof_array( entries
);
507 for( i
= 0; i
< n
; ++i
)
509 wsprintf( keyName
, entries
[ i
].subKey
, clsidString
);
510 err
= RegCreateKeyEx( entries
[ i
].rootKey
, keyName
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
);
511 require_noerr( err
, exit
);
513 size
= (DWORD
)( ( lstrlen( entries
[ i
].data
) + 1 ) * sizeof( TCHAR
) );
514 err
= RegSetValueEx( key
, entries
[ i
].valueName
, 0, REG_SZ
, (LPBYTE
) entries
[ i
].data
, size
);
516 require_noerr( err
, exit
);
519 // If running on NT, register the extension as approved.
521 versionInfo
.dwOSVersionInfoSize
= sizeof( versionInfo
);
522 GetVersionEx( &versionInfo
);
523 if( versionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
525 lstrcpyn( keyName
, TEXT( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved" ), sizeof_array( keyName
) );
526 err
= RegCreateKeyEx( HKEY_LOCAL_MACHINE
, keyName
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
);
527 require_noerr( err
, exit
);
529 lstrcpyn( data
, inName
, sizeof_array( data
) );
530 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
531 err
= RegSetValueEx( key
, clsidString
, 0, REG_SZ
, (LPBYTE
) data
, size
);
535 // register toolbar button
536 lstrcpyn( keyName
, TEXT( "SOFTWARE\\Microsoft\\Internet Explorer\\Extensions\\{7F9DB11C-E358-4ca6-A83D-ACC663939424}"), sizeof_array( keyName
) );
537 err
= RegCreateKeyEx( HKEY_LOCAL_MACHINE
, keyName
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
);
538 require_noerr( err
, exit
);
540 lstrcpyn( data
, L
"Yes", sizeof_array( data
) );
541 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
542 RegSetValueEx( key
, L
"Default Visible", 0, REG_SZ
, (LPBYTE
) data
, size
);
544 lstrcpyn( data
, inName
, sizeof_array( data
) );
545 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
546 RegSetValueEx( key
, L
"ButtonText", 0, REG_SZ
, (LPBYTE
) data
, size
);
548 lstrcpyn( data
, L
"{E0DD6CAB-2D10-11D2-8F1A-0000F87ABD16}", sizeof_array( data
) );
549 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
550 RegSetValueEx( key
, L
"CLSID", 0, REG_SZ
, (LPBYTE
) data
, size
);
552 lstrcpyn( data
, clsidString
, sizeof_array( data
) );
553 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
554 RegSetValueEx( key
, L
"BandCLSID", 0, REG_SZ
, (LPBYTE
) data
, size
);
556 // check if we're running XP or later
557 if ( ( versionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_NT
) &&
558 ( versionInfo
.dwMajorVersion
== 5 ) &&
559 ( versionInfo
.dwMinorVersion
>= 1 ) )
561 wsprintf( data
, L
"%s,%d", (LPCTSTR
) g_nonLocalizedResourcesName
, IDI_BUTTON_XP
);
562 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
563 RegSetValueEx( key
, L
"Icon", 0, REG_SZ
, (LPBYTE
) data
, size
);
565 wsprintf( data
, L
"%s,%d", (LPCTSTR
) g_nonLocalizedResourcesName
, IDI_BUTTON_XP
);
566 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
567 RegSetValueEx( key
, L
"HotIcon", 0, REG_SZ
, (LPBYTE
) data
, size
);
571 wsprintf( data
, L
"%s,%d", (LPCTSTR
) g_nonLocalizedResourcesName
, IDI_BUTTON_2K
);
572 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
573 RegSetValueEx( key
, L
"Icon", 0, REG_SZ
, (LPBYTE
) data
, size
);
575 wsprintf( data
, L
"%s,%d", (LPCTSTR
) g_nonLocalizedResourcesName
, IDI_BUTTON_2K
);
576 size
= (DWORD
)( ( lstrlen( data
) + 1 ) * sizeof( TCHAR
) );
577 RegSetValueEx( key
, L
"HotIcon", 0, REG_SZ
, (LPBYTE
) data
, size
);
586 //===========================================================================================================================
587 // RegisterCOMCategory
588 //===========================================================================================================================
590 DEBUG_LOCAL OSStatus
RegisterCOMCategory( CLSID inCLSID
, CATID inCategoryID
, BOOL inRegister
)
595 err
= CoInitialize( NULL
);
596 require( SUCCEEDED( err
), exit
);
598 err
= CoCreateInstance( CLSID_StdComponentCategoriesMgr
, NULL
, CLSCTX_INPROC_SERVER
, IID_ICatRegister
, (LPVOID
*) &cat
);
599 check( SUCCEEDED( err
) );
600 if( SUCCEEDED( err
) )
604 err
= cat
->RegisterClassImplCategories( inCLSID
, 1, &inCategoryID
);
609 err
= cat
->UnRegisterClassImplCategories( inCLSID
, 1, &inCategoryID
);
621 //===========================================================================================================================
623 //===========================================================================================================================
625 DEBUG_LOCAL OSStatus
UnregisterServer( CLSID inCLSID
)
628 LPWSTR clsidWideString
;
629 TCHAR clsidString
[ 64 ];
631 TCHAR keyName
[ MAX_PATH
* 2 ];
632 OSVERSIONINFO versionInfo
;
634 // Convert the CLSID to a string based on the encoding of this code (ANSI or Unicode).
636 err
= StringFromIID( inCLSID
, &clsidWideString
);
637 require_noerr( err
, exit
);
638 require_action( clsidWideString
, exit
, err
= kNoMemoryErr
);
641 lstrcpyn( clsidString
, clsidWideString
, sizeof_array( clsidString
) );
642 CoTaskMemFree( clsidWideString
);
644 nChars
= WideCharToMultiByte( CP_ACP
, 0, clsidWideString
, -1, clsidString
, sizeof_array( clsidString
), NULL
, NULL
);
645 err
= translate_errno( nChars
> 0, (OSStatus
) GetLastError(), kUnknownErr
);
646 CoTaskMemFree( clsidWideString
);
647 require_noerr( err
, exit
);
650 wsprintf( keyName
, L
"CLSID\\%s", clsidString
);
651 MyRegDeleteKey( HKEY_CLASSES_ROOT
, keyName
);
653 // If running on NT, de-register the extension as approved.
655 versionInfo
.dwOSVersionInfoSize
= sizeof( versionInfo
);
656 GetVersionEx( &versionInfo
);
657 if( versionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
659 lstrcpyn( keyName
, TEXT( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved" ), sizeof_array( keyName
) );
660 err
= RegCreateKeyEx( HKEY_LOCAL_MACHINE
, keyName
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
);
661 require_noerr( err
, exit
);
663 RegDeleteValue( key
, clsidString
);
665 err
= RegCloseKey( key
);
666 require_noerr( err
, exit
);
669 // de-register toolbar button
671 lstrcpyn( keyName
, TEXT( "SOFTWARE\\Microsoft\\Internet Explorer\\Extensions\\{7F9DB11C-E358-4ca6-A83D-ACC663939424}"), sizeof_array( keyName
) );
672 MyRegDeleteKey( HKEY_LOCAL_MACHINE
, keyName
);
680 //===========================================================================================================================
682 //===========================================================================================================================
684 DEBUG_LOCAL OSStatus
MyRegDeleteKey( HKEY hKeyRoot
, LPTSTR lpSubKey
)
689 TCHAR szName
[MAX_PATH
];
693 // First, see if we can delete the key without having to recurse.
695 err
= RegDeleteKey( hKeyRoot
, lpSubKey
);
702 err
= RegOpenKeyEx( hKeyRoot
, lpSubKey
, 0, KEY_READ
, &hKey
);
703 require_noerr( err
, exit
);
705 // Check for an ending slash and add one if it is missing.
707 lpEnd
= lpSubKey
+ lstrlen(lpSubKey
);
709 if ( *( lpEnd
- 1 ) != TEXT( '\\' ) )
716 // Enumerate the keys
719 err
= RegEnumKeyEx(hKey
, 0, szName
, &dwSize
, NULL
, NULL
, NULL
, &ftWrite
);
725 lstrcpy (lpEnd
, szName
);
727 if ( !MyRegDeleteKey( hKeyRoot
, lpSubKey
) )
734 err
= RegEnumKeyEx( hKey
, 0, szName
, &dwSize
, NULL
, NULL
, NULL
, &ftWrite
);
745 // Try again to delete the key.
747 err
= RegDeleteKey(hKeyRoot
, lpSubKey
);
748 require_noerr( err
, exit
);