notepad-plus-plus / nppShell

Provide Explorer context menu entry "Edit with Notepad++"
GNU General Public License v3.0
25 stars 13 forks source link

Registry optimization #21

Closed GurliGebis closed 1 year ago

GurliGebis commented 1 year ago

This optimizes the registry work the installer does. All the registry work has been refactored into a separate RegistryKey class, which both abstracts alot away, and handles the cleanup of handles.

This has been included in the last few DLL's I have built for the notepad-plus-plus/notepad-plus-plus#13399 issue - just didn't tell anyone. So it has been tested a lot actually.

Now the installer code is way easier to understand, since it now is like this all the way:

HRESULT NppShell::Installer::RegisterOldContextMenu()
{
    const wstring contextMenuFullName = GetContextMenuFullName();
    const wstring guid = GetCLSIDString();

    // First we set the shell extension values.
    RegistryKey regKeyExtension(HKEY_LOCAL_MACHINE, ShellKey, KEY_READ | KEY_WRITE, true);
    regKeyExtension.SetStringValue(L"", L"Notepad++ Context menu");
    regKeyExtension.SetStringValue(L"ExplorerCommandHandler", guid);
    regKeyExtension.SetStringValue(L"NeverDefault", L"");

    // Then we create the CLSID for the handler with it's values.
    RegistryKey regKeyClsid(HKEY_LOCAL_MACHINE, L"Software\\Classes\\CLSID\\" + guid, KEY_READ | KEY_WRITE, true);
    regKeyClsid.SetStringValue(L"", L"notepad++");

    RegistryKey regKeyInProc = regKeyClsid.GetSubKey(L"InProcServer32", true);
    regKeyInProc.SetStringValue(L"", contextMenuFullName);
    regKeyInProc.SetStringValue(L"ThreadingModel", L"Apartment");

    return S_OK;
}
GurliGebis commented 1 year ago

@donho this would make it a lot easier for other people get use as a base for their implementation, since it cleans up the installer A LOT 😀

GurliGebis commented 1 year ago

from a functional point of view, it does exactly the same as the current code, but it is way cleaner and easier to read.

donho commented 1 year ago

@GurliGebis So it's just a refactoring right?

GurliGebis commented 1 year ago

@donho yep, refactoring and cleanup, does exactly the same as the existing code - just a lot cleaner and more readable, since all the registry logic has been refactored into a class, and then we just call it instead, making it cleaner and more readable.