上下文菜单shell扩展不会出现在资源管理器中

时间:2013-04-06 20:23:24

标签: windows com contextmenu shell-extensions

我遇到与此处所述相同的问题“Shell extension doesn't work in Windows-Explorer but works in other programs?”,但它不仅适用于资源管理器。操作系统Windows 7 x64。 Dll编译为x64项目。

当我在资源管理器中调用上下文菜单时,我的跟踪文件的一部分:

DllGetClassObject
Factory: Create component
clComponent::QueryInterface: Return pointer to IContextMenu
Factory: Self Destruction
clComponent::QueryInterface: Interface doesn't supported : {00000003-0000-0000-C000-000000000046}
clComponent::QueryInterface: Interface doesn't supported : {0000001B-0000-0000-C000-000000000046}
clComponent::QueryInterface: Return pointer to IUnknown
clComponent::QueryInterface: Interface doesn't supported : {00000018-0000-0000-C000-000000000046}
clComponent::QueryInterface: Interface doesn't supported : {00000019-0000-0000-C000-000000000046}
clComponent::QueryInterface: Interface doesn't supported : {4C1E39E1-E3E3-4296-AA86-EC938D896E92}
clComponent::QueryInterface: Return pointer to IContextMenu
Component: Self Destruction

我还实现了两个接口IShellExtInit和IContextMenu。 也许我在Windows注册中做错了什么?..有人可以在注册中写入必要的更改吗?

注册32和64位dll后的跟踪文件:

DllMain: dwReason == DLL_PROCESS_ATTACH

DllRegisterServer
HKEY_CLASSES_ROOT\CLSID\{7E6025F5-6D2F-4A95-A6FE-E38AF9D1D0E5} : COM Shell Extension
HKEY_CLASSES_ROOT\CLSID\{7E6025F5-6D2F-4A95-A6FE-E38AF9D1D0E5}\InprocServer32 : C:\COMShellExtension32.dll
HKEY_CLASSES_ROOT\CLSID\{7E6025F5-6D2F-4A95-A6FE-E38AF9D1D0E5}\ProgID : COMShellExtension.ShellExtension.1
HKEY_CLASSES_ROOT\CLSID\{7E6025F5-6D2F-4A95-A6FE-E38AF9D1D0E5}\VersionIndependentProgID : COMShellExtension.ShellExtension
HKEY_CLASSES_ROOT\COMShellExtension.ShellExtension : COM Shell Extension
HKEY_CLASSES_ROOT\COMShellExtension.ShellExtension\CLSID : {7E6025F5-6D2F-4A95-A6FE-E38AF9D1D0E5}
HKEY_CLASSES_ROOT\COMShellExtension.ShellExtension\CurVer : COMShellExtension.ShellExtension.1
HKEY_CLASSES_ROOT\COMShellExtension.ShellExtension.1 : COM Shell Extension
HKEY_CLASSES_ROOT\COMShellExtension.ShellExtension.1\CLSID : {7E6025F5-6D2F-4A95-A6FE-E38AF9D1D0E5}
HKEY_CLASSES_ROOT\*\shellex\ContextMenuHandlers\{7E6025F5-6D2F-4A95-A6FE-E38AF9D1D0E5} : COM Shell Extension
RegisterServer: S_OK

DllMain: dwReason == DLL_PROCESS_ATTACH

DllRegisterServer
HKEY_CLASSES_ROOT\CLSID\{7E6025F5-6D2F-4A95-A6FE-E38AF9D1D0E5} : COM Shell Extension
HKEY_CLASSES_ROOT\CLSID\{7E6025F5-6D2F-4A95-A6FE-E38AF9D1D0E5}\InprocServer32 : C:\COMShellExtension64.dll
HKEY_CLASSES_ROOT\CLSID\{7E6025F5-6D2F-4A95-A6FE-E38AF9D1D0E5}\ProgID : COMShellExtension.ShellExtension.1
HKEY_CLASSES_ROOT\CLSID\{7E6025F5-6D2F-4A95-A6FE-E38AF9D1D0E5}\VersionIndependentProgID : COMShellExtension.ShellExtension
HKEY_CLASSES_ROOT\COMShellExtension.ShellExtension : COM Shell Extension
HKEY_CLASSES_ROOT\COMShellExtension.ShellExtension\CLSID : {7E6025F5-6D2F-4A95-A6FE-E38AF9D1D0E5}
HKEY_CLASSES_ROOT\COMShellExtension.ShellExtension\CurVer : COMShellExtension.ShellExtension.1
HKEY_CLASSES_ROOT\COMShellExtension.ShellExtension.1 : COM Shell Extension
HKEY_CLASSES_ROOT\COMShellExtension.ShellExtension.1\CLSID : {7E6025F5-6D2F-4A95-A6FE-E38AF9D1D0E5}
HKEY_CLASSES_ROOT\*\shellex\ContextMenuHandlers\{7E6025F5-6D2F-4A95-A6FE-E38AF9D1D0E5} : COM Shell Extension
RegisterServer: S_OK

我的Registry.cpp

//
// Registry.cpp
//

#include <objbase.h>
#include <Shlobj.h>
#include <assert.h>
#include "trace_tool.h"
#include "Registry.h"



////////////////////////////////////////////////////////
//
// Internal helper functions prototypes
//

// Set the given key and its value.
BOOL setKeyAndValue(const wchar_t* pszPath,
                    const wchar_t* szSubkey,
                    const wchar_t* szValue) ;

// Convert a CLSID into a char string.
void CLSIDtochar(const CLSID& clsid, 
                 wchar_t* szCLSID,
                 int length) ;

// Delete szKeyChild and all of its descendents.
LONG recursiveDeleteKey(HKEY hKeyParent, const wchar_t* szKeyChild) ;

////////////////////////////////////////////////////////
//
// Constants
//

// Size of a CLSID as a string
const int CLSID_STRING_SIZE = 39 ;

/////////////////////////////////////////////////////////
//
// Public function implementation
//

//
// Register the component in the registry.
//
HRESULT RegisterServer(HMODULE hModule,            // DLL module handle
                       const CLSID& clsid,         // Class ID
                       const wchar_t* szFriendlyName, // Friendly Name
                       const wchar_t* szVerIndProgID, // Programmatic
                       const wchar_t* szProgID)       //   IDs
{
    // Get server location.
    wchar_t szModule[512] ;
    DWORD dwResult =
        ::GetModuleFileName(hModule, 
                            szModule,
                            sizeof(szModule)/sizeof(wchar_t)) ;
    assert(dwResult != 0) ;

    // Convert the CLSID into a char.
    wchar_t szCLSID[CLSID_STRING_SIZE] = {0};
    CLSIDtochar(clsid, szCLSID, CLSID_STRING_SIZE) ;

    // Build the key CLSID\\{...}
    wchar_t szKey[64] ;
    wcscpy(szKey, L"CLSID\\") ;
    wcscat(szKey, szCLSID) ;

    // Add the CLSID to the registry.
    setKeyAndValue(szKey, NULL, szFriendlyName) ;

    // Add the server filename subkey under the CLSID key.
    setKeyAndValue(szKey, L"InprocServer32", szModule) ;

    // Add the ProgID subkey under the CLSID key.
    setKeyAndValue(szKey, L"ProgID", szProgID) ;

    // Add the version-independent ProgID subkey under CLSID key.
    setKeyAndValue(szKey, L"VersionIndependentProgID", szVerIndProgID) ;

    // Add the version-independent ProgID subkey under HKEY_CLASSES_ROOT.
    setKeyAndValue(szVerIndProgID, NULL, szFriendlyName) ; 
    setKeyAndValue(szVerIndProgID, L"CLSID", szCLSID) ;
    setKeyAndValue(szVerIndProgID, L"CurVer", szProgID) ;

    // Add the versioned ProgID subkey under HKEY_CLASSES_ROOT.
    setKeyAndValue(szProgID, NULL, szFriendlyName) ;
    setKeyAndValue(szProgID, L"CLSID", szCLSID) ;

    // context-menu hendler
    setKeyAndValue(L"*\\shellex\\ContextMenuHandlers", szCLSID, szFriendlyName);

    trace("RegisterServer: S_OK");
    return S_OK ;
}

//
// Remove the component from the registry.
//
LONG UnregisterServer(const CLSID& clsid,         // Class ID
                      const wchar_t* szVerIndProgID, // Programmatic
                      const wchar_t* szProgID)       //   IDs
{
    // Convert the CLSID into a char.
    wchar_t szCLSID[CLSID_STRING_SIZE] = {0};
    CLSIDtochar(clsid, szCLSID, CLSID_STRING_SIZE) ;

    // Build the key CLSID\\{...}
    wchar_t szKey[64] = {0};
    wcscpy(szKey, L"CLSID\\") ;
    wcscat(szKey, szCLSID) ;

    // Delete the CLSID Key - CLSID\{...}
    LONG lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szKey) ;
    assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)) ; // Subkey may not exist.

    // Delete the version-independent ProgID Key.
    lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szVerIndProgID) ;
    assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)) ; // Subkey may not exist.

    // Delete the ProgID key.
    lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szProgID) ;
    assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)) ; // Subkey may not exist.

    // Delete the context-menu hendler
    //lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szProgID) ;
    //assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)) ; // Subkey may not exist.


    trace("UnregisterServer: S_OK");
    return S_OK ;
}

///////////////////////////////////////////////////////////
//
// Internal helper functions
//

// Convert a CLSID to a char string.
void CLSIDtochar(const CLSID& clsid, wchar_t* szCLSID, int length)
{
    assert(length >= CLSID_STRING_SIZE) ;
    // Get CLSID
    LPOLESTR wszCLSID = NULL ;
    HRESULT hr = StringFromCLSID(clsid, &wszCLSID) ;
    assert(SUCCEEDED(hr)) ;

    //// Covert from wide characters to non-wide.
    //wcstombs(szCLSID, wszCLSID, length) ;

    wcsncpy(szCLSID, wszCLSID, length - 1);

    // Free memory.
    CoTaskMemFree(wszCLSID) ;
}

//
// Delete a key and all of its descendents.
//
LONG recursiveDeleteKey(HKEY hKeyParent,           // Parent of key to delete
                        const wchar_t* lpszKeyChild)  // Key to delete
{
    // Open the child.
    HKEY hKeyChild ;
    LONG lRes = RegOpenKeyEx(hKeyParent, lpszKeyChild, 0, KEY_ALL_ACCESS, &hKeyChild);
    if (lRes != ERROR_SUCCESS)
    {
        return lRes ;
    }

    // Enumerate all of the decendents of this child.
    FILETIME time ;
    wchar_t szBuffer[256];
    DWORD dwSize = 256 ;
    while (RegEnumKeyEx(hKeyChild, 0, szBuffer, &dwSize, NULL, NULL, NULL, &time) == S_OK)
    {
        // Delete the decendents of this child.
        lRes = recursiveDeleteKey(hKeyChild, szBuffer) ;
        if (lRes != ERROR_SUCCESS)
        {
            // Cleanup before exiting.
            RegCloseKey(hKeyChild) ;
            return lRes;
        }
        dwSize = 256 ;
    }

    // Close the child.
    RegCloseKey(hKeyChild) ;

    // Delete this child.
    return RegDeleteKey(hKeyParent, lpszKeyChild) ;
}

//
// Create a key and set its value.
//   - This helper function was borrowed and modifed from
//     Kraig Brockschmidt's book Inside OLE.
//
BOOL setKeyAndValue(const wchar_t* szKey,
                    const wchar_t* szSubkey,
                    const wchar_t* szValue)
{
    HKEY hKey;
    wchar_t szKeyBuf[1024] = {0};

    // Copy keyname into buffer.
    wcscpy(szKeyBuf, szKey) ;

    // Add subkey name to buffer.
    if (szSubkey != NULL)
    {
        wcscat(szKeyBuf, L"\\") ;
        wcscat(szKeyBuf, szSubkey ) ;
    }


    // Create and open key and subkey.
    long lResult = RegCreateKeyEx(HKEY_CLASSES_ROOT ,
                                  szKeyBuf, 
                                  0, NULL, REG_OPTION_NON_VOLATILE,
                                  KEY_ALL_ACCESS, NULL, 
                                  &hKey, NULL) ;
    if (lResult != ERROR_SUCCESS)
    {
        return FALSE ;
    }

    // Set the Value.
    if (szValue != NULL)
    {
        RegSetValueEx(hKey, NULL, 0, REG_SZ, (BYTE *)szValue, (wcslen(szValue)+1) * sizeof(wchar_t) );
        trace(std::wstring(L"HKEY_CLASSES_ROOT\\") + szKeyBuf + L" : " + szValue);
    }

    RegCloseKey(hKey) ;
    return TRUE ;
}

解决! HKEY_CLASSES_ROOT \ CLSID \%CLSID%\ InprocServer32:name:“ThreadingModel”val:“Apartment”

0 个答案:

没有答案