如何用 C++ 编写 shell 扩展? [英] How to write a shell extension in C++?

查看:31
本文介绍了如何用 C++ 编写 shell 扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个常见问题,但在进行了一些搜索之后,我并没有真正找到答案.这里有一篇文章:

http://www.codeproject.com/KB/shell/shellextguide1.aspx>

但它适用于非常旧版本的 Visual Studio.我使用的是 VS 2008,所以说明和界面似乎与我所看到的不符.

我想使用 C++ 创建一个简单的 shell 扩展,为扩展名为 .GZ 的文件创建上下文菜单.右键单击这些文件时,我应该能够单击上下文菜单项并在代码中进行回调以对该文件执行某种操作.

其他上下文菜单项会执行一些操作,例如生成无模块对话框以在执行某些操作之前接受用户输入.

据我所知,ATL 用于此,但我从未使用过 ATL,因此所有对象类型和接口对我来说都非常混乱.如果我有合适的教程或文档可以阅读,那也不错.

谁能帮帮我?不是有一些不是 10 岁的教程吗?

解决方案

我不能确切地告诉你如何编写 shell 扩展,但我会提供一些技巧.与更简单的仅限注册表"方法相比,编写 Shell 扩展具有一些显着的优势:

  • 使用 Shell 扩展,您可以动态创建与所选文件更相关的上下文菜单项(或子菜单).例如,如果您正在为 zip 文件编写 Shell 扩展,则可以在上下文菜单中创建一个子菜单,以显示 zip 的全部内容.
  • 您可以同时处理多个文件,这不仅有利于提高性能,而且您还可以根据整个选择而不是仅针对每个文件来确定要执行的操作.

Shell 扩展的一些缺点是:

  • 显着增加了复杂性.准备好为此付出很多努力才能让它发挥作用.在您的计算机旁边安装一台家用浓缩咖啡机和/或雇人为您煮咖啡.

  • 调试难度大幅增加.咖啡也是如此.

编写 Shell 扩展很困难,因为它们很难调试.

  • Shell Extensions 是由 explorer.exe 进程加载的,如果没有特定的 Explorer 配置,需要强制退出 explorer.exe 进程您可以安装较新版本的 Shell 扩展.有一种方法可以让资源管理器卸载不再使用的 DLL,但您应该只在开发机器上执行此操作,而不能在部署目标上执行此操作:

    1. 在 RegEdit 中,浏览到以下键:

      HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionExplorer

    2. 添加一个名为AlwaysUnloadDLL"的新 DWORD 键并将其值设置为 1.

    3. 重新启动资源管理器.

    这在大多数情况下都有效,但有时您可能仍需要关闭资源管理器,因为未卸载 Shell 扩展.

  • 请记住,您的 Shell 扩展可能会被其他应用程序加载,例如,如果您右键单击带有应用程序打开文件"对话框的文件,那么您的 Shell 扩展将被加载到该应用程序中,而不是资源管理器.

  • 如果您的 Shell 扩展导致运行时错误,结果通常只是您的上下文菜单项不显示,很少有人会告诉您您的 Shell 扩展加载失败或导致运行时错误.

  • 配置可能很困难,即使是安装,也需要在多个位置创建注册表数据,并且根据您希望上下文菜单显示的位置,不同版本的 Windows 注册表中的位置可能会有所不同.

您需要做什么:

  • Visual Studio 提供了一些创建 Shell 扩展的快捷方式,但基本上您需要创建一个 COM DLL.上下文菜单项的 Shell 扩展必须同时实现 IContextMenu 界面和 IShellExtInit 接口.
  • IShellExtInit::Initialize() 方法中,您可以从IDataObject 参数中获取所选文件.从内存中,数据是拖放"格式,所以你需要从 IDataObject 获取一个 HDROP 句柄并从那里查询文件(这是根据记忆,它实际上可能与我在此处描述的不同,因此请谨慎操作).
  • 一旦您的 DLL 准备好安装",您必须将其复制到某处,然后运行 ​​regsvr32 以确保它已注册.
  • 遵循本指南 知道将注册表项放在哪里.
  • 64 位 Windows 可能存在问题,如果您构建 32 位 DLL,它可能无法在 64 位资源管理器中加载……所以如果您在 64 位上遇到问题,请记住这一点-位 Windows.
  • 您的 DLL 实际上有两个与之关联的 GUID.我不记得它是如何工作的,但一个 GUID 指的是 DLL 本身,另一个指的是实际的 Shell 扩展.在需要 GUID 的注册表中创建键时,请确保使用实际 Shell 扩展的 GUID.

考虑到所有事情......(tl;dr)

权衡 Shell 扩展是否值得的成本.如果您想根据所选文件动态创建菜单项,则外壳扩展可能是唯一的方法.如果您想同时处理所有文件,那么您可能还需要一个 Shell 扩展.

上下文菜单方法的替代方法可能是在用户桌面上放置一个拖放目标或其他东西.探索可以让用户将文件提交到应用程序的其他方式,因为 Shell 扩展通常比它的价值要大得多.我很艰难地发现了这一点,我认为其他人也有.

This seemed like a common question but after doing some searching, I wasn't really able to find my answers. There is an article on this here:

http://www.codeproject.com/KB/shell/shellextguide1.aspx

But it's for a very old version of Visual Studio. I'm using VS 2008, so the instructions and interfaces don't seem to match what I'm seeing.

I want to create a simple shell extension using C++ that creates a context menu for files with extension .GZ. When right clicking on these files, I should be able to click my context menu item and have a callback in code to do some sort of operation on that file.

Other context menu items would do things like spawn modless dialogs to accept user input before executing some action.

From what I've seen, ATL is used for this but I have never used ATL, so all of the object types and interfaces are very confusing to me. It wouldn't be so bad if I had a proper tutorial or documentation to read.

Can anyone help me out? Isn't there some sort of tutorial out there that isn't 10 years old?

解决方案

I can't tell you exactly how to write a shell extension, but I will provide a number of tips. Writing a Shell Extension offers some significant advantages over the much simpler "registry-only" method:

  • With a Shell Extension, you can dynamically create a context menu item (or submenu) that is more relevant to the selected file(s). For example, if you are writing a Shell Extension for zip files, it is possible to create a submenu within the context menu that shows the entire contents of the zip.
  • You can handle multiple files simultaneously, which may be more beneficial not just for performance purposes but also so that you can work out what to do based on the selection as a whole rather than just for each file.

Some of the downfalls to Shell Extensions are:

  • Substantially increased complexity. Be prepared to spend a lot of effort on this to get it working. Have a home-espresso machine installed next to your computer and/or hire someone to make you coffee.

  • Substantially increased difficulty in debugging. Ditto about coffee.

It's difficult to write a Shell Extension because they can be very hard to debug.

  • Shell Extensions are loaded by the explorer.exe process, and without specific configuration of Explorer, you need to force-quit the explorer.exe process so that you can install a newer version of your Shell Extension. There is a way to get Explorer to unload DLLs that it is no longer using, but you should only do this on a development machine and not on a deployment target:

    1. In RegEdit, browse to the following key:

      HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionExplorer

    2. Add a new DWORD key called "AlwaysUnloadDLL" and set its value to 1.

    3. Restart explorer.

    This works most of the time, but there may still be times where you need to close Explorer because the Shell Extension was not unloaded.

  • Keep in mind that your Shell Extension may be loaded by other applications, for example, if you right-click on a file with an applications "open file" dialog, then your Shell Extension will be loaded into that application, and not Explorer.

  • If your Shell Extension causes a runtime error, quite often the result will simply be that your context menu item does not show, very rarely will you be told that your Shell Extension failed to load or that it caused a runtime error.

  • Configuration can be hard, even with an installation, registry data needs to be created in several places, and depending where you want your context menu to show, the places in the registry may differ between different versions of Windows.

What you'll need to do:

  • Visual Studio offers some shortcuts to creating Shell Extensions, but basically you'll need to create a COM DLL. A Shell Extension for context menu items must implement both the IContextMenu interface and the IShellExtInit interface.
  • In the IShellExtInit::Initialize() method, you can obtain the selected files from the IDataObject parameter. From memory, the data is in "Drag-n-Drop" format, so you need to get an HDROP handle from the IDataObject and query the files from there (this is from memory, it may actually be different than as I described here, so proceed with caution).
  • Once your DLL is ready to be "installed", you must copy it somewhere, and then run regsvr32 to make sure it is registered.
  • Follow this guide to know where to put registry keys.
  • There may be issues with 64-bit Windows, if you build a 32-bit DLL it may not load in 64-bit Explorer… so keep this in mind if you are having trouble with 64-bit Windows.
  • Your DLL will actually have two GUIDs associated with it. I can't remember exactly how it works, but one GUID refers to the DLL itself and the other refers to the actual Shell Extension. Make sure you use the GUID of the actual Shell Extension when creating keys in the registry where a GUID is required.

All things considered… (tl;dr)

Weigh up the costs of whether a Shell Extension is worth it. If you want to create menu items dynamically based on the selected files, then a Shell Extension may be the only way. If you want to handle all files simultaneously then you'll probably need a Shell Extension as well.

An alternative to the context menu method, could be to have a drag-n-drop target on the user's desktop or something. Explore other ways that you could have the user submit your files to your application, because a Shell Extension is often far more effort than it is worth. I found this out the hard way and I think everyone else has too.

这篇关于如何用 C++ 编写 shell 扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆