2

I have the same problem as described here "Shell extension doesn't work in Windows-Explorer but works in other programs?", but it doesn't works only in explorer. OS Windows 7 x64. Dll was compiled as x64 project.

Part of my trace-file when i call context menu in explorer:

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

I also implement two interfaces IShellExtInit and IContextMenu. Maybe I did something wrong in Windows register?.. Could someone write necessary changes in register?

Trace-file after registration 32 and 64 bit dlls:

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

My 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 ;
}

Solved! HKEY_CLASSES_ROOT\CLSID\%CLSID%\InprocServer32 : name: "ThreadingModel" val: "Apartment"

4

0 回答 0