Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ServerRegistrationManager class not mentioned in docs/tutorials? #398

Open
ividyon opened this issue Sep 17, 2023 · 2 comments
Open

ServerRegistrationManager class not mentioned in docs/tutorials? #398

ividyon opened this issue Sep 17, 2023 · 2 comments

Comments

@ividyon
Copy link

ividyon commented Sep 17, 2023

So, I've been following SharpShell documentation to make a Context Menu handler, and it has said that you use either regasm.exe or srm.exe to register your COM servers with the system.

I cannot expect my endusers to install the NET SDK, nor can I expect them to download SharpShell Tools, and I would imagine most users of the library can't. So I painstakingly simulated the actions of regasm.exe in a custom function that I cobbled together from StackOverflow and my own research on what regasm does, via Process Monitor.

And now that I got it all working, I find a class called ServerRegistrationManager in the SharpShell code, which seems to do exactly what I needed all along, but has not been mentioned by any of the tutorial/doc material?

Am I misunderstanding, and this is not actually intended for my purposes, or have I just wasted my time?

I do notice that, at a glance, it does not seem to offer the option to register the COM server to HKCU/Software/Classes instead of HKCR, which means it unnecessarily requires administrator mode...

@Jdbye
Copy link

Jdbye commented Nov 15, 2023

I looked at the source code for Server Manager to figure out the bits needed to install/uninstall/register/unregister. It wasn't very complicated, the main things you need are ServerRegistrationManager.InstallServer, UninstallServer, RegisterServer and UnregisterServer. But you'll also probably need some code from Server Manager itself relating to listing the servers contained in a DLL, since you can't pass the DLL path directly to these functions.

@Jdbye
Copy link

Jdbye commented Nov 15, 2023

In the end I ended up with this helper class, which takes care of everything (you only need to call 2 functions in your code, InstallAndRegisterShellEx and UninstallAndUnregisterShellEx, though I've added some other useful functions like ShellExIsInstalled)
But do take note that you will want to take special note of 2 things:

  1. RegisterContextMenuHandler/DeleteContextMenuHandler works around the bug that Microsoft caused by making things difficult (context menus won't show up on certain file types, like images), this is the easiest solution I found to that bug.
    If you aren't using context menu extensions, you will want to remove the references to that in the Install and Uninstall functions. If you do use them, change the GUIDs I've hardcoded to your own (and SDOpenWithContextMenuFile to any name you like). And you can add or remove file extensions for that fix as you like in Install and Uninstall functions.

  2. I hardcoded the path of the DLL. It's already set up to look in the application directory so if your shell extension DLL is placed alongside your application, you only need change "SDShellEx.dll" (but it does appear in a few places, so make sure you replace all the entries)

Here is the helper class: https://pastebin.com/vqDU1pfC

Additionally, you'll need 2 files from Server Manager. ServerEntry.cs and ServerManagerApi.cs. They are both here: https://github.com/dwmkerr/sharpshell/tree/main/SharpShell/Tools/ServerManager

Excuse some things being hardcoded, I spent all day working on getting this working and when I finally found a potential fix I just wanted to get it working as quickly as possible, not spend even more time on something that I wasn't yet sure would even work.

I might take the time to do it properly at a later time, but not today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants