Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Limiting an application to single instance...problem? [modified]

Limiting an application to single instance...problem? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vinod Sankaranarayanan
    wrote on last edited by
    #1

    Dear all, I had one MDI application and I want to limit its execution to single instance. I had tried source code from lots of articles (i.e Newcomer's, Naughter ...Armen...) it will work ...but while closing the second instance it raises an exception ... Abnormal program termination!!! This exception generated from "WinMain" when the "pthread->ExitInstance()" get called... How can I solve this ? I am using Windows XP SP2 .....and VC++ 6.0 Thanks in advance...... -- modified at 5:17 Monday 14th August, 2006

    vinsankar

    P 1 Reply Last reply
    0
    • V Vinod Sankaranarayanan

      Dear all, I had one MDI application and I want to limit its execution to single instance. I had tried source code from lots of articles (i.e Newcomer's, Naughter ...Armen...) it will work ...but while closing the second instance it raises an exception ... Abnormal program termination!!! This exception generated from "WinMain" when the "pthread->ExitInstance()" get called... How can I solve this ? I am using Windows XP SP2 .....and VC++ 6.0 Thanks in advance...... -- modified at 5:17 Monday 14th August, 2006

      vinsankar

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      how you are limiting its instances ?

      Prasad Notifier using ATL

      V 1 Reply Last reply
      0
      • P prasad_som

        how you are limiting its instances ?

        Prasad Notifier using ATL

        V Offline
        V Offline
        Vinod Sankaranarayanan
        wrote on last edited by
        #3

        Hi, Try these links...... 1. http://www.codeproject.com/threads/singleinstancemfc.asp 2. http://www.codeproject.com/library/PlgInArchSingInst.asp 3. http://www.codeproject.com/threads/singleinstance.asp 4. http://www.codeproject.com/cpp/csingleinst.asp

        vinsankar

        P 1 Reply Last reply
        0
        • V Vinod Sankaranarayanan

          Hi, Try these links...... 1. http://www.codeproject.com/threads/singleinstancemfc.asp 2. http://www.codeproject.com/library/PlgInArchSingInst.asp 3. http://www.codeproject.com/threads/singleinstance.asp 4. http://www.codeproject.com/cpp/csingleinst.asp

          vinsankar

          P Offline
          P Offline
          prasad_som
          wrote on last edited by
          #4

          I tried first link only. But cant get second instance. Can you explain it in detail?

          Prasad Notifier using ATL

          V 1 Reply Last reply
          0
          • P prasad_som

            I tried first link only. But cant get second instance. Can you explain it in detail?

            Prasad Notifier using ATL

            V Offline
            V Offline
            Vinod Sankaranarayanan
            wrote on last edited by
            #5

            Hi , How will u get second instance? those articles tell u abt preventing the second instance from running when there is already one instance running on ur desktop.... ok.........

            vinsankar

            M W P 3 Replies Last reply
            0
            • V Vinod Sankaranarayanan

              Hi , How will u get second instance? those articles tell u abt preventing the second instance from running when there is already one instance running on ur desktop.... ok.........

              vinsankar

              M Offline
              M Offline
              Milton Karimbekallil
              wrote on last edited by
              #6

              There are no direct APIs or functions to identify which is second instance or third instance and so on. Why do u need second instance? What exactly is the problem u r facing? If something is crashing in the ExistInstance in the second time, you have to find out which statement is crashing. It may be because, u r trying to access an un initialized variable there which may get initialized in the first instance as InitInstance will execute completely in the first instance. But in the second instance you may bypass those initializations and exits after the 'previous instance' checking. Then when control reaches ExitInstance it will crash as what ever u r accessing is not initialized. Just a thought. chk it out. Cheers..Milton KB.

              V 1 Reply Last reply
              0
              • V Vinod Sankaranarayanan

                Hi , How will u get second instance? those articles tell u abt preventing the second instance from running when there is already one instance running on ur desktop.... ok.........

                vinsankar

                W Offline
                W Offline
                Waldermort
                wrote on last edited by
                #7

                When the first instance starts execution, the very first thing it should do is check for a mutex, if not found it should create one. When the second instance starts, it should check for the mutex, when it finds it it should terminate. Look at the CreateMutex() and GetLastError() functions. I'm not sure where to do this in MFC, but in Win32 these routines are the first to be executed in WinMain(). Also note all of this should be done before creating/reading any other variables. I have a header file containing all these functions and a few more like handling Command line paramaters, and handling MultiUser/MultiDesktop environments. (A modified combination of CP examples). If you like I will send it to you.

                V 1 Reply Last reply
                0
                • M Milton Karimbekallil

                  There are no direct APIs or functions to identify which is second instance or third instance and so on. Why do u need second instance? What exactly is the problem u r facing? If something is crashing in the ExistInstance in the second time, you have to find out which statement is crashing. It may be because, u r trying to access an un initialized variable there which may get initialized in the first instance as InitInstance will execute completely in the first instance. But in the second instance you may bypass those initializations and exits after the 'previous instance' checking. Then when control reaches ExitInstance it will crash as what ever u r accessing is not initialized. Just a thought. chk it out. Cheers..Milton KB.

                  V Offline
                  V Offline
                  Vinod Sankaranarayanan
                  wrote on last edited by
                  #8

                  Hi, Sorry....... I don't need the second instance......I want to prevent all other instances while one is there.. then..... I fix it ..... I made a mistake in the "return statement" after finding the previous instance of that program in the 'InitInstance()' of my App class. I was written like this... BOOL CMyApp::InitInstance() { if(IsthereApreviousInstance(....GUID)) { AfxMessageBox("Already one Instance"); return FALSE; // This is wrong } } The above code will work but an exception will raised... So I put it like this.... exit(0); // in place of "return FALSE" Ok.............Thanks....to all

                  vinsankar

                  1 Reply Last reply
                  0
                  • W Waldermort

                    When the first instance starts execution, the very first thing it should do is check for a mutex, if not found it should create one. When the second instance starts, it should check for the mutex, when it finds it it should terminate. Look at the CreateMutex() and GetLastError() functions. I'm not sure where to do this in MFC, but in Win32 these routines are the first to be executed in WinMain(). Also note all of this should be done before creating/reading any other variables. I have a header file containing all these functions and a few more like handling Command line paramaters, and handling MultiUser/MultiDesktop environments. (A modified combination of CP examples). If you like I will send it to you.

                    V Offline
                    V Offline
                    Vinod Sankaranarayanan
                    wrote on last edited by
                    #9

                    Hi, I solve it .... See my previous message.... Thanks.... -- modified at 11:31 Monday 14th August, 2006

                    vinsankar

                    1 Reply Last reply
                    0
                    • V Vinod Sankaranarayanan

                      Hi , How will u get second instance? those articles tell u abt preventing the second instance from running when there is already one instance running on ur desktop.... ok.........

                      vinsankar

                      P Offline
                      P Offline
                      prasad_som
                      wrote on last edited by
                      #10

                      Vinod A Sankaranarayanan wrote:

                      How will u get second instance?

                      that's why I wanted you to explain in detail. Anyway you solve your problem, and shared,too.Thats good.

                      Prasad Notifier using ATL

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups