Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associate a file type to a running application

Tags:

c++

windows

I am developing an application in windows using vs2005 c++ and want to associate a file extension to the application which I can do in the registry settings. However, when I click on the file I want to associate it with a running instance of my application and not launch a new one. How can I achieve this behavior.

like image 747
Mary hall Avatar asked May 12 '11 04:05

Mary hall


People also ask

What does it mean to associate a file?

(1) The relationship of one file to another based on the data it contains. (2) An established relationship between a file and the application used to open it. There are default file associations pre-configured in every operating system for all the common file types.

How does operating system associate a file type with a program?

Answer. Both the Windows and Mac OS X operating systems use file associations to define what default program is used to open each file type. For example, in Windows, plain text (. ... Therefore, if you double-click a plain text file in Windows, it will open in Notepad, while in Mac OS X, it will open in TextEdit.

How do I associate a file in Windows?

Open the Start menu, type “Control Panel” and then click on it. Now, navigate to Programs. Next, click on Associate a file type or protocol with a specific program. Now, scroll down and select Choose default apps by file type at the bottom of the page.

How do I associate an app?

You can associate your Google Play Store Android app with a website property in Search Console. If an app is associated with a website, Google will automatically try to index and crawl your app by inferring the app structure from the website structure. Request association using Play Console.


2 Answers

I can't give you an implementation off the top of my head but look into something called "interprocess communication" (typically using COM if you're on windows). What you can do is, when your program starts, check for other copies of itself, if it finds one then it tells that one what file you wanted open, then shuts itself down.

I found an implementation for you:

http://www.flounder.com/nomultiples.htm

like image 119
EdF Avatar answered Oct 03 '22 16:10

EdF


This used to be done with a parameter in winmain but from msdn

A handle to the previous instance of the application. This parameter is always NULL. If you need to detect whether another instance already exists, create a uniquely named mutex using the CreateMutex function. CreateMutex will succeed even if the mutex already exists, but the function will return ERROR_ALREADY_EXISTS. This indicates that another instance of your application exists, because it created the mutex first. However, a malicious user can create this mutex before you do and prevent your application from starting. To prevent this situation, create a randomly named mutex and store the name so that it can only be obtained by an authorized user. Alternatively, you can use a file for this purpose. To limit your application to one instance per user, create a locked file in the user's profile directory.

like image 22
rerun Avatar answered Oct 03 '22 18:10

rerun