CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 1999
    Posts
    33

    associating file types with my program and more!


    I know this has been answered before, but I am not sure how to search for it....
    I want to associate a file type with my program, so that when the user double-clicks on a file of that type, my program runs and opens that file. In addition, I only want ONE instance of my app to run at a time. So, if my app is running, and the user double-clicks on the file, my app comes to the front and opens that file. My app is already MDI, so it can open multiple files at a time...
    Can someone point me to an example?

    thanx

  2. #2
    Join Date
    Aug 2001
    Location
    North Bend, WA
    Posts
    1,947
    There is an article, here on CG, about restricting your app to one instance. Search for "One instance application" to find it.


    This article in MSDN describes how to do it in the registry.

    "Creating a File Association"

    I think you can register associations in the Doc/View architecture as well

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    In addition to Bill's answer...

    1. Single instance

    You need to create a named mutex semaphore when you start your application. When the second one starts it tries to get access to the mutex but will fail...
    Code:
    // app.h
    class CYourApp : public CWinApp
    {
      ...
    
    private:
      HANDLE hMutex;
    };
    
    // app.cpp
    BOOL CYourApp::InitInstance()
    {
      // Create mutex
      hMutex = ::CreateMutex(NULL, TRUE, "GlobalMutex");
    
      switch(::GetLastError())
      {
        case ERROR_SUCCESS:
          // Mutex created successfully. There is no instance running
          break;
    
        case ERROR_ALREADY_EXISTS:
          // Mutex already exists so there is a running instance of our app.
          return FALSE;
    
        default:
          // Failed to create mutex by unknown reason
          return FALSE;
      }
    }
    2. File association

    Since I happen to have the mentioned article within my bookmarks...here is it....

  4. #4
    Join Date
    Aug 1999
    Posts
    33
    Thanx everyone for all the help. Now that I can have only one instance of my MDI app running, when the user double-clicks on a file of the right type, I want my already running app to open that file in a new view. I am not sure I saw how to do that based on the readings in MSDN.

  5. #5
    Join Date
    Aug 2001
    Location
    North Bend, WA
    Posts
    1,947
    I don't know if this is the best way to do it, or not. The second instance could send a message to the first instance requesting the file to be opened by the first instance before the second instance exits.

  6. #6
    Join Date
    Aug 1999
    Posts
    33
    Hmmmmmm, to tell the truth, I thought about that. Might work, but it is not a design I would want to show any of my old college professors

    I cannot believe that MS does not supply a cleaner way of doing this......

    thanx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured