如何为 Windows 创建自定义 Shell 上下文处理程序?

发布于 2024-07-06 01:55:09 字数 411 浏览 12 评论 0原文

问题

语言:C# 2.0 或更高版本


我想注册上下文处理程序以在用户右键单击某些文件(在我的例子中为 *.eic)时创建菜单。 从这些菜单中注册、取消注册(清理)和处理事件(点击)的过程是什么?

我知道这与 Windows 注册表有关,但考虑到 .net 中有多少东西,如果有方便的方法可以干净简单地完成此操作,我不会感到惊讶。

代码片段、网站参考、评论都很好。 请把它们扔给我。

更新


显然,正如一些用户评论的那样,用托管语言创建上下文菜单存在一个小问题。 是否有其他首选方法可以实现相同的行为,或者我应该花时间研究这些解决方法? 我根本不介意这样做,我很高兴人们付出努力使这成为可能 - 但我仍然想知道是否有一种“正确/干净”的方式来实现这一目标。

Problem

Language: C# 2.0 or later


I would like to register context handlers to create menues when the user right clicks certain files (in my case *.eic). What is the procedure to register, unregister (clean up) and handle events (clicks) from these menues?

I have a clue it's something to do with the windows registry, but considering how much stuff there is in .net, I wouldn't be surprised if there are handy methods to do this clean and easy.

Code snippets, website references, comments are all good. Please toss them at me.

Update


Obviously there is a slight problem creating context menues in managed languages, as several users have commented. Is there any other preferred way of achieving the same behaviour, or should I spend time looking into these workarounds? I don't mind doing that at all, I'm glad people have put effort into making this possible - but I still want to know if there is a "proper/clean" way of achieving this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

就此别过 2024-07-13 01:55:09

这不是一个好主意,因为不同版本的 .NET Framework 之间存在潜在的依赖性问题。 您的 shell 扩展可能需要一个版本,而当前正在运行的应用程序可能已经加载了不同的版本。

此帖子 包含对情况的很好的总结。

This is not a good idea because of potential dependency issues between different versions of the .NET Framework. Your shell extension could be expecting one version, while a different version may have already been loaded by the application that's currently running.

This thread contains a good summary of the situation.

莫多说 2024-07-13 01:55:09

虽然其他人已经提到,由于框架冲突,在纯 .NET 中编写 shell 扩展是一个坏主意,但您仍然应该注意:

  1. 有 3rd 方驱动程序(请参阅 Eldos 或 LogicNP)可以为您完成非托管方面的工作,允许您编写与本机驱动程序对话的托管代码,从而防止与 shell 相关的 CLR 版本冲突。

  2. 最近的一篇 MSDN 文章提到 Microsoft 已经为 Silverlight 使用的 CoreCLR 解决了这个问题。 他们通过允许多个版本的 CLR 在同一进程中运行来实现这一点,从而解决了问题。 作者进一步表示,Silverlight 中的这一修复将被纳入完整 CLR 的未来版本中。 (这意味着,将来在托管代码中编写 shell 扩展将是相当可行的。)

While others already mentioned that writing shell extensions in pure .NET is a bad idea due to framework conflicts, you should still note that:

  1. There are 3rd party drivers out there (see Eldos or LogicNP) that do the unmanaged side for you, allowing you to write managed code that talks to the native driver, thus preventing shell-related CLR version conflicts.

  2. A recent MSDN article mentioned that Microsoft has solved this problem for the CoreCLR, as used by Silverlight. They've accomplished this by allowing multiple versions of the CLR to run in the same process, thus fixing the problem. The author further stated that this fix in Silverlight will be rolled into future versions of the full CLR. (Meaning, in the future, it will be quite feasible to write shell extensions in managed code.)

苍白女子 2024-07-13 01:55:09

我以前用 C# 做过这些。 它最终变得比应有的困难得多。 不过,一旦掌握了样板代码,推出新项目就很容易了。 我点击了此链接:

信息链接

I've done them before in C#. It ends up being a hell of a lot harder than it should be. Once you get the boilerplate code down, though, it is easy to roll out new items. I followed this link:

Link To Info

因为看清所以看轻 2024-07-13 01:55:09

正如前面的评论提到的,用托管语言编写 shell 扩展并不是最好的主意,但我想我应该分享一个正在做这件事的开源项目

:) google.com/p/shellglue/" rel="nofollow noreferrer">ShellGlue 是一个托管 shell 扩展,实际上非常有用。 如果您有兴趣用 C/C++ 编写 shell 扩展,该源代码也可能对您有所帮助。

As the prior comments mention, it isn't the best idea to write shell extensions in managed languages, but I thought I'd share an Open Source project that is doing just that :)

ShellGlue is a managed shell extension that is actually quite helpful. The source also might be helpful to you if you're interested in pursuing writing a shell extension in C/C++.

野侃 2024-07-13 01:55:09

除了已提到的有关在托管代码中实现 shell 扩展的注意事项之外,您基本上需要执行以下操作:

首先,在 C# 中创建一个实现 IShellExtInit IContextMenu 接口的 COM 组件。 此处描述了如何在 C# 中创建 COM 组件本文中描述了如何实现必要的接口。 虽然描述是针对 C++ 实现的,但您可以将这些知识应用到您的 C# 版本。

您的 COM 组件将具有称为 Class-ID 或 CLSID 的 GUID。 您需要使用您的文件类型将该 ID 注册为上下文菜单 shell 扩展:

HKEY_CLASSES_ROOT\.eic\ShellEx\ContextMenuHandlers\MyShellExt
    (Default) -> {YOUR-COMPONENTS-CLSID}

还要确保您按照 C# COM 教程中的说明正确注册了组件。 您应该在注册表中的

HKEY_CLASSES_ROOT\CLSID\{YOUR-COMPONENTS-CLSID}
    InprocServer32
        (Default) -> C:\WINDOWS\system32\mscoree.dll
        Class -> YourImplClass
        assembly -> YourAssembly, version=..., Culture=neutral, PublicKey=...
        ...

“祝你好运”下找到它...

Aside from the caveats that have been mentioned concerning the implementation of shell extensions in managed code, what you'd basically need to do is the following:

First, create a COM component in C# that implements the IShellExtInit IContextMenu interfaces. How to create COM components in C# is described here. How to implement the necessary interfaces is described in this article. While the description is for a C++ implementation, you can apply that knowledge to you C# version.

Your COM component will have GUID called the Class-ID or CLSID. You need to register that ID with your file type as a context-menu shell extension:

HKEY_CLASSES_ROOT\.eic\ShellEx\ContextMenuHandlers\MyShellExt
    (Default) -> {YOUR-COMPONENTS-CLSID}

Also make sure that you registered your component correctly as described in the C# COM tutorial. You should find it in the registry under

HKEY_CLASSES_ROOT\CLSID\{YOUR-COMPONENTS-CLSID}
    InprocServer32
        (Default) -> C:\WINDOWS\system32\mscoree.dll
        Class -> YourImplClass
        assembly -> YourAssembly, version=..., Culture=neutral, PublicKey=...
        ...

Good luck...

窗影残 2024-07-13 01:55:09

正如其他人指出的那样,shell 扩展目前在 Windows 开发中并不实用。

我最近问了一个类似的问题,该问题得到了回答,并提供了具体操作指南的链接我想做的事

As others have pointed out, shell extensions are not practical in windows development currently.

I asked a similar question recently which was answered with a link to a guide to do exactly what I wanted to do

╰ゝ天使的微笑 2024-07-13 01:55:09

抵制用托管语言编写 Shell 扩展 - 如果您采用这种方法,有很多事情可能会失败。

浏览此线程以了解更多详细信息。 它包含如果确实想要这样做的链接,以及为什么可以这样做但不应该这样做的明智建议。

http://social .msdn.microsoft.com/Forums/en-US/netfxbcl/thread/1428326d-7950-42b4-ad94-8e962124043e/

您又回到了非托管 C/C++ 作为您唯一真正的工具。

Resist writing Shell Extensions in managed languages - there are a multitude of things that could go bang if you pursue this route.

Have a browse through this thread for more details. It contains links to do it if really want, and sagely advice of why it can be done, but shouldn't.

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/1428326d-7950-42b4-ad94-8e962124043e/

You're back to unmanaged C/C++ as your only real tools here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文