How to make program open some sort of files

Started by
21 comments, last by Erik Rufelt 15 years, 5 months ago
Hello, I have a question. If I have lets say an application that opens image files (.jpg), and in program, the image file path variable is called myImage, how could I make the program open up and load image when I double click on any .jpg image? Just like Windows Picture viewer, when you click on any image, it loads up in image viewer. Sorry for my bad English.
Advertisement
This is OS specific, for Windows you need to create several registry entries.
Note that of course the original setting is lost.

Pseudocode:
  char      szDesc[MAX_PATH];  wsprintf( szDesc, _T( "%s.Document" ), szAppDescription );  SetKey( HKEY_CLASSES_ROOT, szExtension, NULL, szDesc );  SetKey( HKEY_CLASSES_ROOT, szDesc, NULL, szAppDescription );  wsprintf( szDesc, _T( "%s.Document\\DefaultIcon" ), szAppDescription );  SetKey( HKEY_CLASSES_ROOT, szDesc, NULL, szIconPath );  wsprintf( szDesc, _T( "%s.Document\\shell\\open\\command" ), szAppDescription );  SetKey( HKEY_CLASSES_ROOT, szDesc, NULL, szOpenCommand );


szExtension is the extension (for example ".jpg")
szAppDescription is a name for your application
szIconPath is the path to an icon (used by explorer to display the file)
szOpenCommand is the commandline for opening the file (usually the full path followed by %1%)

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Quote:Original post by Endurion
This is OS specific, for Windows you need to create several registry entries.
Note that of course the original setting is lost.

Pseudocode:
  char      szDesc[MAX_PATH];  wsprintf( szDesc, _T( "%s.Document" ), szAppDescription );  SetKey( HKEY_CLASSES_ROOT, szExtension, NULL, szDesc );  SetKey( HKEY_CLASSES_ROOT, szDesc, NULL, szAppDescription );  wsprintf( szDesc, _T( "%s.Document\\DefaultIcon" ), szAppDescription );  SetKey( HKEY_CLASSES_ROOT, szDesc, NULL, szIconPath );  wsprintf( szDesc, _T( "%s.Document\\shell\\open\\command" ), szAppDescription );  SetKey( HKEY_CLASSES_ROOT, szDesc, NULL, szOpenCommand );


szExtension is the extension (for example ".jpg")
szAppDescription is a name for your application
szIconPath is the path to an icon (used by explorer to display the file)
szOpenCommand is the commandline for opening the file (usually the full path followed by %1%)


Thank you for reply, but I didnt mean that.
I am expressing myself badly in English...

I meant that when you set the application as default for opening .jpg files, and than you try to open that picture in the application by doubleclicking on any picture, but the default picture loads up (myImage).
How could I make the application know what picture I want to open, and than store its path to myImage?
Ah, sorry for misunderstanding. Your english is good, i just skipped some parts of your explanation.

Usually Windows will pass in the filename in the command line parameters. Just check your argc and argv variables, if argc equals 2 then argv[1] contains the filename.

argv[0] usually contains the filename to your application itself.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Add the image you want to open as a command line argument to your program. For instance: my_program.exe my_image.jpg

If your making an Win32 application you will have some form of WinMain function that is called when the program starts. One of the parameters to this function is 'lpCmdLine' which holds the command line arguments, in this case 'my_image.jpg'.

So set up your program to load this image, if the user provides a command line argument, or your default image if he doesn't.

When you register a file extension with Windows and double click on a file, your program is called in the same way, with the file as the first (and only) command line argument.
On Windows, to make a new program the default used for opening a file type, find a file of that type. Right click on it. Go to "Open With..." then "Choose Program" and browse to your executable. Check the "always use this program" box, and open the file.

Now, it should be the default. Sometimes Windows is odd and doesn't actually do that, but if it doesn't, you can still right click on files of that type and click on "Open With". Then, your program should be on the short list of associated programs there.

In order to do the above, you must first have done as the last two posts have instructed. These instructions must also be done on any computer that wants to have this. Your installer can mess with registry values to do this automatically, but I couldn't tell you which ones you need to change.
Quote:Original post by Endurion
Ah, sorry for misunderstanding. Your english is good, i just skipped some parts of your explanation.

Usually Windows will pass in the filename in the command line parameters. Just check your argc and argv variables, if argc equals 2 then argv[1] contains the filename.

argv[0] usually contains the filename to your application itself.


ok, so I have something like this:
LPWSTR* argv = CommandLineToArgvW(GetCommandLine(), &argc);

Now, argv holds path, right? But argv is LPWSTR, but I need to save path as WCHAR variable.
Quote:Original post by Jan Birsa
Now, argv holds path, right? But argv is LPWSTR, but I need to save path as WCHAR variable.


LPWSTR is a Long Pointer to Wide STRing, or in other words, a pointer to a null-terminated array of WCHARs.

A WCHAR variable would just be a single character. I'd assume that the routine you are using to load the image takes a null-terminated character string.
Quote:Original post by EasilyConfused
Quote:Original post by Jan Birsa
Now, argv holds path, right? But argv is LPWSTR, but I need to save path as WCHAR variable.


LPWSTR is a Long Pointer to Wide STRing, or in other words, a pointer to a null-terminated array of WCHARs.

A WCHAR variable would just be a single character. I'd assume that the routine you are using to load the image takes a null-terminated character string.


Ok, so what should I do? (sorry for my dumb questions)
Quote:Original post by Jan Birsa
Quote:Original post by EasilyConfused
Quote:Original post by Jan Birsa
Now, argv holds path, right? But argv is LPWSTR, but I need to save path as WCHAR variable.


LPWSTR is a Long Pointer to Wide STRing, or in other words, a pointer to a null-terminated array of WCHARs.

A WCHAR variable would just be a single character. I'd assume that the routine you are using to load the image takes a null-terminated character string.


Ok, so what should I do? (sorry for my dumb questions)


How are you currently loading the image?

This topic is closed to new replies.

Advertisement