views:

960

answers:

2

There's an executable file generated from my program in MFC and I want to use it as the default program to open the .jpg files. That is to say, each time I double click a .jpg file, my program will run.

I tried to add some registry entries linking .jpg files with my program, such as HKEY_CLASSES_ROOT.jpg\shell\open\command (set its value to " "myProgram.exe" "%1" "), and HKEY_CLASSES_ROOT\myProgram.

The method works just fine except when some other applications register themselves to open the .jpg files. For example, I have installed acdSee on my computer, so each time I doule click a .jpg file, it always start acdSee instead of my own program. But when I register a completely new type of file with my program, it can be open in the program. I don't know how to set my program as the default opening program of an already registered file programmatically. Can anyone help me solve this problem? Thank you very much!

+2  A: 

The more typical/standard way for doing this is to set the default value of the ".jpg" key to a name that identifies the file type more clearly, and then setup the various associated actions there. So for jpgs, you might do this:

HKCR\.jpg
   @default = MyApp.JpegImage
HKCR\MyApp.JpegImage\shell\open\command
   @default = "myApp.exe "%1""

If some other program decides to register the type, they will replace the default value for HKCR.jpg with some other value, like OtherProgram.Jpg. At that point, you could re-register it to your app by setting the value back to MyApp.JpegImage.

Disclaimer: When making this sort of change, please also try to respect the user's preferences. For instance, when installing your application, give the user the option to set this file association or not set it. You can also provide a command from inside your installed application to reset the associations, if the user should wish to do so.

If you instead wanted to add some additional commands to an existing registered type, you would read the default value of the .jpg key to find the name of the file type. Then you could open that key and add an action to the existing set of actions. For instance, you could add the following:

HKCR\ExistingApp.JpegImage\shell\myopen\
    @default = "Open with MyApp"
HKCR\ExistingApp.JpegImage\shell\myopen\command\
    @default = "myApp.exe "%1""
Charlie
Sorry. I have done all you said. But problem still exists...
A: 

Note that by writing a key to HKCR, you're actually writing to HKLM\Software\Classes. This will require administrative privileges. However, you can make per-user changes within a user context by writing your keys to HKCU\Classes\Root instead.

Also, user preferences in HKCU will override the system defaults in HKLM, which sounds like what your problem might be.

This is when a program has not registered an extension as a "Default" (Is the program listed in Set Programs and Defaults in the Control Panel?)

Time to start reading documentation!

Factor Mystic
sorry, I even can not find any key of HKCU\Classes\Root in my registry.