创建注册表项以将文件扩展名与C ++中的应用程序相关联 [英] Create registry entry to associate file extension with application in C++

查看:214
本文介绍了创建注册表项以将文件扩展名与C ++中的应用程序相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用我的C ++应用程序注册文件扩展名的最简洁的方法,这样当与我的程序相关联的数据文件被双击时,应用程序被打开,文件名作为参数传递给应用程序。



目前,我通过我的wix安装程序,但有一些情况下,应用程序不会安装在用户的计算机上,所以我还需要选择创建



另外,这也意味着如果应用程序被删除,注册表中的未使用的条目将被放置在周围?

解决方案

您可以在这篇MSDN文章。关键部分位于列表底部:




  • 注册ProgID



ProgID(实质上是文件类型注册表项)包含重要的文件类型属性,例如图标,描述和上下文菜单项,包括双击文件时使用的应用程序。许多扩展可能具有相同的文件类型。该映射在下一步完成:




  • 注册文件类型的文件扩展名



    • 在此设置扩展的注册表值,将该扩展的文件类型设置为您在上一步中创建的ProgID。



      使用应用程序打开文件所需的最少工作量是设置/创建两个注册表项。在这个例子中, .reg 文件,我创建一个文件类型( blergcorp.blergapp.v1 .blerg )。

        Windows注册表编辑器版本5.00 

      [HKEY_CURRENT_USER\Software\Classes\blergcorp.blergapp.v1\shell\open\command]
      @ =c:\path\to\app .exe \%1 \
      [HKEY_CURRENT_USER\Software\Classes\.blerg]
      @ =blergcorp.blergapp.v1

      现在,您可能希望以编程方式完成此操作。为了绝对保守,你可以检查这些键的存在,并相应地改变你的程序行为,特别是如果你假设控制一些常见的文件扩展名。但是,目标可以通过使用SetValue函数设置这两个键来实现。



      我不确定的C ++语法,但在C#语法看起来东西像这样:

        Registry.SetValue(@HKEY_CURRENT_USER\Software\Classes\blergcorp.blergapp.v1\shell \open\command,null,@c:\path\to\app.exe \%1 \); 
      Registry.SetValue(@HKEY_CURRENT_USER\Software\Classes\.blerg,null,blergcorp.blergapp.v1);

      当然,您可以手动打开每个子键,手动创建ProgID和扩展子键, >然后设置键值,但 SetValue 函数的一个好处是,如果键或值不存在,它们将自动创建。非常方便。



      现在,快速了解要使用的蜂巢。许多文件关联示例在线,包括MSDN上的示例,显示在 HKEY_CLASSES_ROOT 中设置的这些键。我不建议这样做。该hive是 HKEY_LOCAL_MACHINE \Software \Classes (系统默认值)和 HKEY_CURRENT_USER\ Software \ Classes 的合并虚拟视图。 code>(每个用户设置),并且写入hive中的任何子项将重定向到 HKEY_LOCAL_MACHINE \Software \Classes 中的同一个键。现在,这没有直接的问题,但可能会遇到这个问题:如果您写入HKCR(重定向到HKLM),并且用户已指定相同的密钥在HKCU中具有不同的值,HKCU值将优先。因此,您的写入将会成功,但您不会看到任何更改,因为 HKEY_CURRENT_USER 设置优先于 HKEY_LOCAL_MACHINE 设置。



      因此,在设计应用程序时应考虑到这一点。现在,另一方面,你可以只写 HKEY_CURRENT_USER ,如我的例子所示。但是,该文件关联设置只会为当前用户加载,如果您的应用程序已为所有用户安装,则当其他用户在Windows中打开该文件时,您的应用程序将无法启动。



      这应该是一个不错的底漆,你想做什么。要进一步阅读,我建议





      类似的回答类似的问题:




      I would like to know the cleanest way of registering a file extension with my C++ application so that when a data file associated with my program is double clicked, the application is opened and the filename is passed as a parameter to the application.

      Currently, I do this through my wix installer, but there are some instances where the application will not be installed on ths user's computer, so I also need the option of creating the registry key through the application.

      Additionally, will this also mean that if the application is removed, unused entries in the registry will be left lying around?

      解决方案

      Your basic overview of the process is found in this MSDN article. The key parts are at the bottom of the list:

      • Register the ProgID

      A ProgID (essentially, the file type registry key) is what contains your important file type properties, such as icon, description, and context menu items including application used when the file is double clicked. Many extensions may have the same file type. That mapping is done in the next step:

      • Register the file name extension for the file type

      Here, you set a registry value for your extension, setting that extension's file type to the ProgID you created in the previous step.

      The minimum amount of work required to get a file to open with your application is setting/creating two registry keys. In this example .reg file, I create an file type (blergcorp.blergapp.v1) and association a file extension (.blerg) with it.

      Windows Registry Editor Version 5.00
      
      [HKEY_CURRENT_USER\Software\Classes\blergcorp.blergapp.v1\shell\open\command]
      @="c:\path\to\app.exe \"%1\""
      [HKEY_CURRENT_USER\Software\Classes\.blerg]
      @="blergcorp.blergapp.v1"
      

      Now, you probably want to accomplish this programmatically. To be absolutely kosher, you could check for the existence of these keys, and change your program behavior accordingly, especially if you're assuming control of some common file extension. However, the goal can be accomplished by setting those two keys using the SetValue function.

      I'm not positive of the exact C++ syntax, but in C# the syntax looks something like this:

      Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\blergcorp.blergapp.v1\shell\open\command", null, @"c:\path\to\app.exe \"%1\"");
      Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\.blerg", null, "blergcorp.blergapp.v1");
      

      Of course you could manually open each sub key, manually create the ProgID and extension subkey, and then set the key value, but a nice thing about the SetValue function is that if the keys or values don't exist, they will automatically be created. Very handy.

      Now, a quick word about which hive to use. Many file association examples online, including ones on MSDN, show these keys being set in HKEY_CLASSES_ROOT. I don't recommend doing this. That hive is a merged, virtual view of HKEY_LOCAL_MACHINE\Software\Classes (the system defaults) and HKEY_CURRENT_USER\Software\Classes (the per-user settings), and writes to any subkey in the hive are redirected to the same key in HKEY_LOCAL_MACHINE\Software\Classes. Now, there's no direct problem doing this, but you may run into this issue: If you write to HKCR (redirected to HKLM), and the user has specified the same keys with different values in HKCU, the HKCU values will take precedence. Therefore, your writes will succeed but you won't see any change, because HKEY_CURRENT_USER settings take precedence over HKEY_LOCAL_MACHINE settings.

      Therefore, you should take this into consideration when designing your application. Now, on the flip side you can write to only HKEY_CURRENT_USER, as my examples here show. However, that file association setting will only be loaded for the current user, and if your application has been installed for all users, your application won't launch when that other user opens the file in Windows.

      That should be a decent primer for what you want to do. For further reading I suggest

      And see also my similar answer to a similar question:

      这篇关于创建注册表项以将文件扩展名与C ++中的应用程序相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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