Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to associate the my file with my app?

178 views
Skip to first unread message

Matthias S

unread,
Mar 8, 2003, 5:14:32 PM3/8/03
to
Hi,

I've written an application that creates allows the user to save his work into
pcf files. Some basic questions with that:

1. Are there any restrictions on the extensions I'm using?
2. How do I tell windows that it should launch my app when the user double-
clicks such file?
3. Can I control what cmdline args windows is passing to my app when the user
selects my file? My app can be run from cmdline, so it's important to me that
I can specify what windows is passing.
4. How do I associate an Icon with my document and tell windows that it should
use this icon?

--

Any help is highly appreceated. Thanks in advance.

Matthias

/*
'C++' should have been named 'D'
www.mattbart.org
sonork: 100.32002
*/

CJ

unread,
Mar 8, 2003, 1:21:40 PM3/8/03
to
Hey Matthias,

I will try to answer most of your questions.

1. Not if you are slam'n heads with another extension. I believe once it
is in the Shell, you are good to go.
2. Programmicaly, I am not sure. You may have to add a Key to the
Registry.
Manually, you would do this in the Windows Shell. I do not know what
OS you are running, so I won't go to much into detail.
Basically you will need to Add a new Shell Extensions to the File
Type in Windows Explorer
3. You can do this all in the Shell Extensions.
4. You would program this in you application properties. Look for the
Icon Property.

Hopefully this will get you started. If I knew what OS you where running
on, I could help you dig deeper.

Thx,

CJ

"Matthias S" <matthiasSP...@mattbart.org> wrote in message
news:MPG.18d40b242...@news.microsoft.com...

Matthias S

unread,
Mar 8, 2003, 10:27:14 PM3/8/03
to
In article <uKW61#Z5CHA...@TK2MSFTNGP09.phx.gbl>, cj_oa...@hotmail.com
says...
Hi,

the application is intended to run on all windows OS' newer then Win95. Is the
process of programatically manipulating the Shell Stuff different on the
various OS'? One more question: Do you know some good starting point for a
little reading on this topic?

Hates Spam@adelphia.net SleazySt

unread,
Mar 9, 2003, 9:56:45 PM3/9/03
to
I wrote a news group reply explaining how to do this a few years ago. It's still valid. I have
reprinted it below in its entirety, editted only slightly to correct an inaccuracy, add a couple of
small things previously omitted, and make it more relevant for .NET:

*** REPRINT BEGINS HERE ***

First you need to create a subkey under HKEY_CLASSES_ROOT that will hold the commands that start
your application. You should name this key something semi-descriptive. Your file extension[s] will
be mapped to this key. For example, TXT files are mapped to a key named "txtfile", by default
(without the quotation marks, of course). The benefit of using this setup is that if you have
multiple extensions that your app can handle, you can map them all to this key. For example, many
image editting apps create a subkey called something like "imagefile", and map .bmp, .jpg, .gif,
etc. to that key. We'll call your key "JoeBlowFile". Next, you need to set the "default" value for
your new JoeBlowFile key to a text string describing to the user just what type of file they have.
This is what shows up in Windows Explorer under "Type". Again, to use the TXT file example, a good
string here would be "Text File" or "Text Document". (It is the latter by default.) Your string
might be "Joe Blow Data".
Now, under your new key, you can create another subkey, called "DefaultIcon". This, as its name
suggests, sets the icon that is used with files of this type. You should create a custom icon that
pictorially represents documents handled by your program. You can save this icon as an ICO file in
your app's directory, but even better, you can include it as a resource in either your EXE or DLL.
Either way, you'll then need to set the subkey's default value to a string representing the full
path and filename of the ICO, EXE, or DLL. If there is more than one icon in the file (particularly
likely if you include it as a resource in your EXE or DLL), you'll also need to add a comma, and
either a positive index number representing which icon you'd like to use (1 is the first icon in the
file, 2 is the second, and so on), or a negative resource ID, using the negative of whatever ID
you've assigned your icon in your resource script. So yours could be, for example "C:\Program
Files\JoeBlow\JoeBlow.exe, 2".
A note for C# developers on the above paragraph. Unfortunately, C# projects can't have resource
scripts. Resources added to .NET applications by adding them to the project and designating them an
"Embedded Resource" are included in a new .NET-specific format that's not compatible with previous
methods. The only icon you can correctly embed in your application using C# on VS.NET is the
application icon, accessible from the project properties. If you need additional icons -- e.g. an
icon to represent a document file handled by your app rather than to represent the app itself --
you'll need to either include the ICO files themselves, or compile a DLL with C++ with your icons
embedded.
Whether or not you choose to use the DefaultIcon key, you now need to create a subkey named
"shell" under your JoeBlowFile key. Under the shell key, you'll create individual keys for each of
the commands you'd like the user to be able to perform on your filetype from the context menu
(right-click menu). These items are called "verbs". For consistency, one of them should be
"open" -- this key, if it exists, will be the default (i.e. when a user double clicks on a file of
your type, the open command will be performed). Instead, you can set the default value for the
"shell" key equal to the verb you'd like to perform by default. You can optionally set the default
value for each verb key to the text that you would like to appear in the context menu when a user
right-clicks on a file of your type. An ampersand (&) can be used within this text to designate
that the following character will be underlined, which means that the user can press the key
corresponding to that character to select that command from the context menu. For instance, for the
"open" key, you could put "Open with &Joe Blow's app" as the default value. That text then, with an
underlined J, will appear in the context menu for files of that type, and a user can press the
letter J to start Joe Blow's app.
The only thing you *have* to do, though, with the "open" (and subsequent) keys, is create
another key as a subkey of that one called "command". The default value for the command key must be
set to a string representing just that, the command required to perform that action. For instance,
the default string in the command key under the "open" key might be ""C:\Program
Files\JoeBlow\JoeBlow.exe" "%1"". Note the quotation marks around the path\filename for your app
and around the %1. They are only neccessary around the path\filename of your app if there are any
spaces in the path or filename, but they are absolutely a requirement around the %1 for 32-bit apps.
The %1 is just like %1 in old MS-DOS batch (.bat) files. %1 is replaced with the first parameter on
the command line, in this case, it becomes the filename of the file your app is supposed to open.
Because you do not know in advance if the path or filename containing the file you're supposed to
open will contain spaces, you must put the quotes around %1.
Other required parameters for your app should also be included. For instance, the default value
in the "command" key, under the "print" key might be ""C:\Program Files\JoeBlow\JoeBlow.exe" "%1"
/print", or ""C:\Program Files\JoeBlow\JoeBlow.exe" /print "%1"". It's up to you how you want to
process command line parameters in your app.
A note on replaceable parameters like "%1", mentioned above. Apparently, the "%1" parameter
/may/ be replaced with the *short* filename to be opened. This isn't always the case, and I haven't
figured out what criteria Windows uses to determine which it will pass -- short or long. It may be
that if the executable path listed in the registry is a long filename, Windows will replace %1 with
the long filename to start, but if the executable path is a short filename, or can be interpreted as
one, Windows will replace %1 with the short filename. If you want to be sure that you always get
the *long* filename, use "%L" instead. You can use an uppercase L (as I've done) or a lowercase
one, but I prefer to use uppercase because lowercase "l" looks _way_ to much like the number "1".
What's more, if your program knows how to deal with Shell Item IDs, you can get *that* instead
of the long filename with the "%i" parameter. Again, upper- or lowercase "i" are equally suitable,
but I find uppercase "I" harder to distiguish from lowercase "l" and the number "1". If you don't
know what a Shell Item ID is, it's okay. You'll probably never need to use one. If you're using
C#, I don't think you can use them anyway without some API code in a section marked "unsafe".
You're finally done with the JoeBlowFile key. The rest is relatively simple. You simply need
to create (if it doesn't already
exist) another subkey under HKEY_CLASSES_ROOT, and name it the same as the extension of your
document type. To use the txtfile example, the name would be ".txt" (with the dot, but without the
quotes). Yours (Joe Blow's) might be ".jbf" (for Joe Blow File). The default value for this key
must now be set to the name of the first key your created, which in the example we've using is
"JoeBlowFile".
That's it. You're done in the registry. Do remember that you'll have to make sure your app
proccesses the command line in a manner consistent with the commands you set under the "shell" key.
Window's won't open that file for you automatically when your app starts... you have to do it.


Graphically, it looks like this:

HKEY_CLASSES_ROOT
| +--.jbf = JoeBlowFile
|
+--JoeBlowFile = Joe Blow Data
|
+--DefaultIcon = C:\Program Files\JoeBlow\JoeBlow.exe, 2
|
+--Shell
|
+--open = Open with &Joe Blow's App
| |
| +--command = "C:\Program Files\JoeBlow\JoeBlow.exe" "%1"
|
+--print
|
+--command = "C:\Program Files\JoeBlow\JoeBlow.exe" "%1" /print

If you don't already know how to modify the registry, look in MSDN for all the functions
beginning with "Reg", including
RegOpenKey[Ex](), RegCreateKey[Ex](), and RegSetValue[Ex](). You can also do it the wimpy way by
creating a ".reg" file and simply use ShellExecuteEx() to call "regedit.exe /s" on it. (The /s
keeps regedit from popping up a message box asking "Are you sure you want to add the information in
[name of file.reg] to the registry?") The format of the a REG file is simple and straight forward.
Here is an example REG file to add the "JoeBlow" example from above:

REGEDIT4

[HKEY_CLASSES_ROOT\.jbf]
@="JoeBlowFile"

[HKEY_CLASSES_ROOT\JoeBlowFile]
@="Joe Blow Data"

[HKEY_CLASSES_ROOT\JoeBlowFile\DefaultIcon]
@="C:\\Program Files\\JoeBlow\\JoeBlow.exe, 2"

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell]

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\open]
@="Open with &Joe Blow's app"

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\open\command]
@="\"C:\\Program Files\\JoeBlow\\JoeBlow.exe\" \"%1\""

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\print]
@="&Print"

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\print\command]
@="\"C:\\Program Files\\JoeBlow\\JoeBlow.exe \"%1\" /print"

Make sure that you include "REGEDIT4" as the first line of the file, or it won't work. Also
make sure to press enter on the last line, or that line won't be read in. Altogether, adding your
program to the registry this way is not as convenient as it sounds, because you'll have to modify
your REG file if your app is installed anywhere *except* to C:\Program Files\JoeBlow.

The above instructions were aimed at a user programming directly to the Win32 API using C or
C++. For C# on .NET, it's rather a bit easier. See the Registry class, or you can even do much of
it graphically using a deployment project in VS.NET.

HTH

Matthias S

unread,
Mar 11, 2003, 12:31:50 AM3/11/03
to
In article <ucOdCCr5...@TK2MSFTNGP11.phx.gbl>, "SleazySt" <SleazySt Hates
Sp...@adelphia.net> says...

Hi,

thanks for your help - very much appreceated!

--

0 new messages