Solved

Opening Files in .exe

Posted on 2003-10-31
10
273 Views
Last Modified: 2010-04-16
I want to associate a filename with my exe, and be able to open files when a use double click on a file with the correct extension.   If my program is already open, I do not want to start another instance, but rather pass this file to the instance already running.  what is the best way to code this?
0
Comment
Question by:jjacksn
10 Comments
 
LVL 6

Expert Comment

by:ryerras
ID: 9665006
You have to create a setup package, and while youc reate the setup, open the file types editor by right clicking on view, and selecting file types. Select the file types on target machine node and choose action, add file type. Open the Properties window, select the command property, and click the ellipsis (...) button. The select item in project dialog box appears. Select primary output from your project from the Application folder node and click OK.
 Change the Name propery to whtever u want, and the Extensions property to the extensions you want  your target files to contain. I guess you build and install.. you should be ready to go.. I assume you use VS .net. Let me know if this doesnt work for you..
0
 
LVL 5

Author Comment

by:jjacksn
ID: 9665221
Right, that works for the file extension.

I'm also wondering in terms of opening the file, what is the best way to accomplish getting an existing instance of the .exe to learn that someone has double click on a file associated with the program.  My goal is to have it work in such a way as word or paintshop file where it opens inside the existing .exe rather than somewhere else.  is there an easier way to do this than setting up a remoting instance?
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9666196
You can create a registry entry:

Create a ".reg"-file with this contents:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.ext]

[HKEY_CLASSES_ROOT\.ext\shell]

[HKEY_CLASSES_ROOT\.ext\shell\open]
@="Menu item text for open"

[HKEY_CLASSES_ROOT\.ext\shell\open\command]
@="c:\\winnt\\notepad.exe \"%1\""


Replace .ext with your file extension and the path in open\command with your application. You can use * instead of a file extension to provide a command on every file type.

----------------

Now about the instance:

When you use this open\command registry entry then there is always a new instance started. Usually to avoid two instances of the same program there is a startup code that checks for an existing instance.
You need inter process communication (memory mapped files, shared memory, ...) to send the command to the existing instance.
Some programs are realized as COM-Components and they are using the HKEY_CLASSES_ROOT\.ext\shellex\ entries (e.g.: Microsoft Office). I don't know if it would be easier that way.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 5

Author Comment

by:jjacksn
ID: 9666386
Is it too big of a hack to write the file name the registry then send a windows message to the running instance?  Or should I actually take the type to create a remoting object from one instance to the other?
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9666990
For that you need a target instance. You could start a TCP listener on a fixed port and when the second instance is started you try to create the listener and fail (cause the port is already occupied) then create a TcpClient and send the data to the first instance. Of course you could also use TCP remoting but you have to configure it in the code since only the first instance can obtain the port.
0
 
LVL 5

Author Comment

by:jjacksn
ID: 9668070
The way I have it setup now is that a newly created instance loops through all running processes, looks for the filename, and if it finds it, sends it a custom windows message.  the running process then gets this windows message, and goes to the registry to find the file name.  Is this a bad way to do it?
0
 
LVL 5

Author Comment

by:jjacksn
ID: 9697382
ptmcomp. If I want to do it by hand what would the actual reg key look like?  

it doesn't appear than any other file extenions behave in this manner.  I see some OpenWithList and what not, but even then paths aren't sepcified.  I'm on an XP box.  
0
 
LVL 5

Author Comment

by:jjacksn
ID: 9697411
I added that key (I to my xp box) and it didn't do anything.  It was original associated win an exe of an old version of program and wouldn't change, so I deleted all old versions.  when I tried to create the file association again, it didn't work.  I go to choose program and select my program, but it brings me back to the previous Choose Program dialog but my selected program is not in the view
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9707811
I don't have any PC with Windows XP, but it worked on Windows 95, 98, ME, NT4 and 2000. I think there is a typing mistake if it doesn't work. Often also the extension linked to a filetype and then on the filetype the commands are defined.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.ext]
@="MyFileType"

[HKEY_CLASSES_ROOT\MyFileType\DefaultIcon]

[HKEY_CLASSES_ROOT\MyFileType\DefaultIcon]
@="%SystemRoot%\system32\shell32.dll,-152"

[HKEY_CLASSES_ROOT\MyFileType\shell]

[HKEY_CLASSES_ROOT\MyFileType\shell\open]
@="Menu item text for open"

[HKEY_CLASSES_ROOT\MyFileType\shell\open\command]
@="c:\\winnt\\notepad.exe \"%1\""

May be a log off / log on  or a restart is needed to make the changes work.
Replace ".ext" with your file extension and "c:\\winnt\\notepad.exe" with your application path.
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 250 total points
ID: 10547116
jjacksn,

 I don't know if you have gotten this problem solved, but check this post: http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20910110.html.  There is a link to a website that demonstrates using DDE to accomplish exactly what you are trying to do: http://www.codeproject.com/cs/miscctrl/ddelistener.asp.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Exceptions 3 46
Facial recognition to look through the whole database for a person 3 69
C# parent child form 5 28
Finally almost got it 5 36
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question