dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Restricting exe

  1. #1
    Join Date
    Oct 2008
    Posts
    62

    Unhappy Restricting exe

    HI,
    I created one win32exe... If i run the exe for more than once, it should not run, but it's running...
    Can anyone know how to restrict my exe from running more than one instance ?

    Thanks in advance,
    Riyas.B

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: Restricting exe

    WinMain will be passed in the HINSTANCE of an existing copy. Typically you would just set the focus to that copy, and then exist.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Restricting exe

    Quote Originally Posted by riyrse View Post
    HI,
    I created one win32exe... If i run the exe for more than once, it should not run, but it's running...
    Can anyone know how to restrict my exe from running more than one instance ?
    There are many code examples of this, right here on CodeGuru (if you did a search). It is more often than not, done using mutexes.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jun 2007
    Location
    New Delhi
    Posts
    70

    Re: Restricting exe

    There are two ways of doing this.
    1) Enumerate all the running processes and check how much instances of your application are running. if it is more than 1 then exit from your application.
    2) create a global event(named as "Global\\App_event")
    using createevent() and after creating obtain last error code. if last error code = "error_already_exists" then exit from your application.

  5. #5
    Join Date
    Dec 2008
    Posts
    29

    Re: Restricting exe

    I use below method by using of my application's ClassName, call it in the beginning of WinMain:
    Code:
            HWND  hwndS2 = GetRunningW ();  // running an instance ?
    
            if (hwndS2 != 0)
            {
                ::SetForegroundWindow (hwndS2);
    
                if (::IsIconic (hwndS2))
                    ::ShowWindow (hwndS2, SW_RESTORE);
    
                return 0; // close program
            }

    Code:
    HWND GetRunningW ()
    {
        HWND hwnd = ::FindWindow (L"YourClassName", 0);
    
        if (::IsWindow (hwnd))
        {
            HWND hwndPopup = ::GetLastActivePopup (hwnd);
            if (::IsWindow (hwndPopup))
                hwnd = hwndPopup;
        }
        else 
    	 hwnd = 0;
    	
        return hwnd;
    }
    You can use MFC equivalents too

  6. #6
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Restricting exe

    Quote Originally Posted by TheCPUWizard
    WinMain will be passed in the HINSTANCE of an existing copy. Typically you would just set the focus to that copy, and then exist.
    In which stone age you are living? (no sarcasm).

    hPrevInstance (of type HINSTANCE) is always NULL for 32-bit applications. You can use named synchronization object, or a linker option (latter is not recommended).
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  7. #7
    Join Date
    Jan 2008
    Posts
    178

    Re: Restricting exe

    Quote Originally Posted by TheCPUWizard View Post
    WinMain will be passed in the HINSTANCE of an existing copy.
    Incredible to read such noob things...
    MVP program is really laughable... (and I laughed when they wanted to take me for a flying rat)
    Last edited by fred100; December 28th, 2008 at 02:11 PM.

  8. #8
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Restricting exe

    MVP program is really laughable... (and I laughed when they wanted to take me for a flying rat)
    You cannot generalize that statement. MVP is granted on various factors like qualification, experience, knowledge of programming language/tools/OSs, participation on forum etc.

    It is not expected that candidate has to know each and everything! CPUWizard must have not touched WinMain for long time. Even I get confused which parameter specifies length in strncpy - second or third? And it specifies length of source or destination?
    Reason is simple: I don't use str* function, instead I use CString/string class for string manipulation.

    Thus forgetting an old thing does not mean knowledge or certification is "laughable"
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: Restricting exe

    Quote Originally Posted by Ajay Vijay View Post
    In which stone age you are living? (no sarcasm).

    hPrevInstance (of type HINSTANCE) is always NULL for 32-bit applications. You can use named synchronization object, or a linker option (latter is not recommended).

    I have got to stop posting after pulling an all nighter.

    You are of course correct. I Must be having flashbacks to my Windows 3.1 days...

    However, because you can:

    1) You write your own startup routine to replace/suplment the CRT.
    and
    2) You are often itrested in the identity of the "other instance.

    It is possible to augment the Mutex methodology (or use some other methodology such as shared memory space) to find either:

    1) The Process ID of the other instance.
    or
    2) A HANDLE to the other instance (retrieved by OpenProcess

    Either of these can then be cast to an HINSTANCE and passed into the application level code.

    The "nice" effect of this, is that you do not need to put the code into each application that you write, you can simply leverage an existing parameter that is no longer used.

    If people are interested, I can writeup a short note on an implementation of this technique, and make it available with the source code. Contact me via PM...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)