Prevent starting up twice

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dirk

    Prevent starting up twice

    Hello,

    Can anyone tell me what the best way is to prevent an application from
    starting up twice.

    Thanks,
    Dirk




  • Unforgiven

    #2
    Re: [OT] Prevent starting up twice

    "Dirk" <someone@nomail .xx> wrote in message
    news:GqCdnUj0W4 R5UdPcRVnyvg@ca sema.nl...[color=blue]
    > Hello,
    >
    > Can anyone tell me what the best way is to prevent an application from
    > starting up twice.[/color]

    This is off-topic in this group, as standard C++ defines no such mechanism.
    It usually involves things such as creating named pipes and checking if that
    pipe exists or not, stuff like that, but it is usually far from trivial and
    highly platform specific.

    Ask again in a newsgroup for your specific OS.

    --
    Unforgiven

    Comment

    • Victor Bazarov

      #3
      Re: Prevent starting up twice

      Dirk wrote:[color=blue]
      > Can anyone tell me what the best way is to prevent an application from
      > starting up twice.[/color]

      You cannot prevent it from starting up twice if your OS allows that.
      However, you can try to avoid letting your application proceed far
      beyond the startup code if you can determine that another copy of your
      application is already running. The only standard C++ way I know is
      to check if there exists a file in a predetermined location and when
      the application starts, and if it does, exit, and if it doesn't, create
      one to indicate that your application is the first one. The application
      that creates the file shouldn't forget to delete it when it closes.

      If you want something that doesn't involve creating files, you need
      a platform-specific solution. Ask in a newsgroup dedicated to your
      platform.

      Victor

      Comment

      • JKop

        #4
        Re: Prevent starting up twice

        Victor Bazarov posted:
        [color=blue]
        > Dirk wrote:[color=green]
        >> Can anyone tell me what the best way is to prevent an[/color][/color]
        application from[color=blue][color=green]
        >> starting up twice.[/color]
        >
        > You cannot prevent it from starting up twice if your OS[/color]
        allows that.[color=blue]
        > However, you can try to avoid letting your application[/color]
        proceed far[color=blue]
        > beyond the startup code if you can determine that another[/color]
        copy of your[color=blue]
        > application is already running. The only standard C++[/color]
        way I know is[color=blue]
        > to check if there exists a file in a predetermined[/color]
        location and when[color=blue]
        > the application starts, and if it does, exit, and if it[/color]
        doesn't, create[color=blue]
        > one to indicate that your application is the first one.[/color]
        The application[color=blue]
        > that creates the file shouldn't forget to delete it when[/color]
        it closes.[color=blue]
        >
        > If you want something that doesn't involve creating[/color]
        files, you need[color=blue]
        > a platform-specific solution. Ask in a newsgroup[/color]
        dedicated to your[color=blue]
        > platform.
        >
        > Victor
        >[/color]

        For Windows:

        go to msdn.microsoft. com and look up "CreateMute x".


        -JKop

        Comment

        • Terry Liittschwager

          #5
          Re: Prevent starting up twice

          On Mon, 20 Sep 2004 14:28:23 +0200, "Dirk" <someone@nomail .xx> wrote:[color=blue]
          >Can anyone tell me what the best way is to prevent an application from
          >starting up twice.[/color]

          If you're using Windows, here's the code generated by VC++ to prevent
          a second instance of an application named WWB:

          int APIENTRY WinMain(HINSTAN CE hInstance,
          HINSTANCE hPrevInstance,
          LPSTR lpCmdLine,
          int nCmdShow)
          {
          if (hPrevInstance)
          {
          MessageBox(0,"W WB is already running",NULL,M B_OK);
          return FALSE; // previous instance not allowed
          }

          Comment

          • Mike Wahler

            #6
            Re: Prevent starting up twice


            "Terry Liittschwager" <terry@epud.net > wrote in message
            news:5lqtk0p7e0 tdd33b1s32d7jf5 33g0d1pt0@4ax.c om...[color=blue]
            > On Mon, 20 Sep 2004 14:28:23 +0200, "Dirk" <someone@nomail .xx> wrote:[color=green]
            > >Can anyone tell me what the best way is to prevent an application from
            > >starting up twice.[/color]
            >
            > If you're using Windows, here's the code generated by VC++ to prevent
            > a second instance of an application named WWB:
            >
            > int APIENTRY WinMain(HINSTAN CE hInstance,
            > HINSTANCE hPrevInstance,
            > LPSTR lpCmdLine,
            > int nCmdShow)
            > {
            > if (hPrevInstance)
            > {
            > MessageBox(0,"W WB is already running",NULL,M B_OK);
            > return FALSE; // previous instance not allowed
            > }[/color]

            This gives an example of why it's not a good idea to
            ask or answer off topic questions. This off topic
            answer is incorrect. See a Windows newsgroup and/or
            MSDN for further information.

            -Mike



            Comment

            • Mike Smith

              #7
              Re: Prevent starting up twice

              Mike Wahler wrote:
              [color=blue]
              > "Terry Liittschwager" <terry@epud.net > wrote in message
              > news:5lqtk0p7e0 tdd33b1s32d7jf5 33g0d1pt0@4ax.c om...
              >[color=green]
              >>On Mon, 20 Sep 2004 14:28:23 +0200, "Dirk" <someone@nomail .xx> wrote:
              >>[color=darkred]
              >>>Can anyone tell me what the best way is to prevent an application from
              >>>starting up twice.[/color]
              >>
              >>If you're using Windows, here's the code generated by VC++ to prevent
              >>a second instance of an application named WWB:
              >>
              >>int APIENTRY WinMain(HINSTAN CE hInstance,
              >> HINSTANCE hPrevInstance,
              >> LPSTR lpCmdLine,
              >> int nCmdShow)
              >>{
              >>if (hPrevInstance)
              >>{
              >>MessageBox(0, "WWB is already running",NULL,M B_OK);
              >>return FALSE; // previous instance not allowed
              >>}[/color]
              >
              >
              > This gives an example of why it's not a good idea to
              > ask or answer off topic questions. This off topic
              > answer is incorrect. See a Windows newsgroup and/or
              > MSDN for further information.[/color]

              Well, since we're all indulging on OTness - the posted answer is correct
              for 16-bit Windows (i.e. pre-Win95), but not Win32. ;-P

              --
              Mike Smith

              Comment

              • Ron Natalie

                #8
                Re: Prevent starting up twice


                "Dirk" <someone@nomail .xx> wrote in message news:GqCdnUj0W4 R5UdPcRVnyvg@ca sema.nl...[color=blue]
                > Hello,
                >
                > Can anyone tell me what the best way is to prevent an application from
                > starting up twice.[/color]

                Fix it so that it doesn't start up once.

                Comment

                • Julie

                  #9
                  Re: Prevent starting up twice

                  Dirk wrote:[color=blue]
                  >
                  > Hello,
                  >
                  > Can anyone tell me what the best way is to prevent an application from
                  > starting up twice.[/color]

                  When the app starts, open a predefined (temporary) file in exclusive mode*. If
                  it succeeds, continue; if it fails, terminate. Close the file when exiting.

                  *Presumes that your OS and C/C++ library supports exclusive file access.

                  Other than that, you will need to resort to more OS specific solutions -- find
                  a newsgroup that discusses your OS and post the question there.

                  Comment

                  Working...