CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Apr 2006
    Posts
    37

    About opening exe

    Is there is any way to open the exe(executable) only once. when tried to open the exe next time it should not be opened.

  2. #2
    Join Date
    Jul 2005
    Location
    E: 120°.6, N: 31°.3′
    Posts
    795

    Re: About opening exe

    FindWindow is what u want .
    Little by little one goes far
    Keep moving.......!
    Nothing is impossible !

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: About opening exe

    Quote Originally Posted by sunny_sz
    FindWindow is what u want .
    Wrong!

    The correct way is described in J.Newcomer's essay Avoiding Multiple Instances of an Application
    Last edited by VictorN; September 7th, 2007 at 07:22 AM.
    Victor Nijegorodov

  4. #4
    Join Date
    Jul 2005
    Location
    E: 120°.6, N: 31°.3′
    Posts
    795

    Re: About opening exe

    Quote Originally Posted by VictorN
    Wrong!

    The correct way is decribed in J.Newcomer's essay Avoiding Multiple Instances of an Application
    I wanna know why that is wrong ?? Pls. advise !
    Little by little one goes far
    Keep moving.......!
    Nothing is impossible !

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: About opening exe

    Joe Newcomer described it very good in the essay I referred to in my previous post.
    Is it so hard to read it?

    PS.
    OK, the simplest example: application is windowless.
    More complicated example: application needs (for some reasons) some time to create the main window, and during this time period user started this application again.
    Victor Nijegorodov

  6. #6
    Join Date
    Jul 2005
    Location
    E: 120°.6, N: 31°.3′
    Posts
    795

    Re: About opening exe

    But what veerakalai want is that when a window(e.g. a .exe file is running) is running , if user open it again , it should not be opened again, I mean , by using findwindow function, we may find this window of this .exe file and check whether this file is running or not , if we find this handle of this window, we can prevent it opened again, right ??
    Little by little one goes far
    Keep moving.......!
    Nothing is impossible !

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: About opening exe

    Quote Originally Posted by sunny_sz
    But what veerakalai want is that when a window(e.g. a .exe file is running) is running ,
    Where in the OP did you see the word "window"?
    if user open it again , it should not be opened again, I mean , by using findwindow function, we may find this window of this .exe file and check whether this file is running or not ,
    Please, read my previous post (particularly, the PostScriptum section)
    if we find this handle of this window, we can prevent it opened again, right ??
    Wrong!
    1. As I already mentioned, the window may not exist (yet);
    2. Just when FindWindow has found something there is no guarantee it is the window of the first instance of your application. There might be some other windows of some other applications having the same window title.
    3. Please, read "Avoiding Multiple Instances of an Application" essay and you will never more suggest using such an erroneous way as FindFindow to prevent "to open the exe next time "....
    Victor Nijegorodov

  8. #8
    Join Date
    Jul 2005
    Location
    E: 120°.6, N: 31°.3′
    Posts
    795

    Re: About opening exe

    If a program is running , it must have a window to display on the screen, even if we can not find it display, perhaps the window for this program running now is invisible(hide), we can also find it out , or we also may use CreateToolhelp32Snapshot to check it exsisted in process or not .

    Perhaps you now misunderstand me , as I encounter it again from you before. as for our answer is right or not for veerakalai's question, it's up to him .

    Take care!

    See you !
    Little by little one goes far
    Keep moving.......!
    Nothing is impossible !

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: About opening exe

    Quote Originally Posted by sunny_sz
    If a program is running , it must have a window to display on the screen

    Where did you found it?
    It is wrong!
    Example no. 1: console application.
    Example no. 2: Create an MFC dialog application, put any code (for some calculations, working with some files, registry and so on) in the App InitInstance method and comment out CDialog:oModal call.
    Example nö. 3: it is for you as an exercise...
    Take care!

    See you !
    Right! I agree!
    Victor Nijegorodov

  10. #10
    Join Date
    Apr 2006
    Posts
    37

    Re: About opening exe

    I have read the essay "avoid Multiple Instance" and i tried that. I faced
    a problem in searcher function, in searcher fn the follwing condition is not met in any suitation
    if(result == wm_Find)
    {
    /* found it */
    HWND * target = (HWND *)lParam;
    *target = hWnd;
    return FALSE; // stop search
    }
    i have attched the project i tried named singleInstance below. see to that and give me some suggestion.
    Attached Files Attached Files

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: About opening exe

    Quote Originally Posted by veerakalai
    I faced
    a problem in searcher function, in searcher fn the follwing condition is not met in any suitation
    Sure!
    You don't handle UWM_ARE_YOU_ME message (in your case - CSingleInstanceApp::wm_Find message) in the CMainFrame class, so why do you expect any "feedback"?
    Victor Nijegorodov

  12. #12
    Join Date
    Apr 2006
    Posts
    37

    Re: About opening exe

    I have tried as u said but i get the error as "error C2101: '&' on constant"
    and i defined the UWM_ARE_YOU_ME as (WM_APP + 5). give me some solution to this problem. I have attached the project below
    Attached Files Attached Files

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: About opening exe

    Quote Originally Posted by veerakalai
    I have tried as u said but i get the error as "error C2101: '&' on constant"
    You did something wrong. Read MSDN about such an error. Fix this error.
    and i defined the UWM_ARE_YOU_ME as (WM_APP + 5).
    Why?
    give me some solution to this problem. I have attached the project below
    Joe Newcomer already gave you solution with a lot of pieces of code. Why are you now trying to reinvent the wheel rather than copy/paste the working code?
    The hint: your CMainFrame must handle the message that your CSingleInstanceApp::searcher() sends to all top level windows. As I saw from your previous zip you had used message id CSingleInstanceApp::wm_Find . So you must handle this message in the CMainFrame class and return this value from the message handler.
    Victor Nijegorodov

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: About opening exe

    At the risk of adding fuel to the fire, I'd like to propose another solution that uses a memory mapped file and mutex to track the main hWnd of the application.

    Here's what you do:
    1) Add the following to stdafx.h:
    Code:
    #include "Autolock.h"
    usingnamespace AutoLock;
    #include "SingleInstance.h"


    2) Modify the CWinApp Derived class

    In the header file, add:

    Code:
    class CMyWinApp : public CWinApp
    {
      ...
     
    // Implementation
    public:
      void SetFirstInstanceHwnd( HWND hWnd );
    
    // Attributes
    private:
    
      // Class which limits this application to a single instance
      CSingleInstance m_SingleInstance;
    };
    

    In the .cpp file modify the ctor and InitInstance method;

    Code:
    
    
    Code:
    CMyWinApp::CMyWinApp()
      : m_SingleInstance( _T("MYAPPNAME-088FA840-B10D-11D3-BC36-006067709674") )
    {
    }
     
    BOOL CMyWinApp::InitInstance( )
    {
      //
      // Verify this is the first instance of the program. 
      // If not, we use a memory mapped file to track the 
      // first instance hwnd and use this handle to bring
      // the first instance to the foreground.
      //
      if( S_OK != m_SingleInstance.CheckFirstInstance( ) )
      {
    	return FALSE;
      }
    
     
      // Typical init instance code here
      ...
     
      return TRUE;
    }
    
    


    Lastly, modify the MainFrame cpp file to record the main hWnd

    Code:
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
      // Typical OnCreate code here
      ...
     
      //
      // Set the hWnd of this dialog in the memory mapped file
      // This hWnd will be used on subsequent instances to bring
      // the first dialog instance to the foreground
      //
      ((CMyWinApp*)AfxGetApp())->SetFirstInstanceHwnd( GetSafeHwnd( ) );
     
      return 0;
    }
    


    Neither this nor the J.P. Newcomer solution will completely work across desktops. I mean if the goal is to limit the app, then they both can work. But if the goal is to limit the app and bring an existing app to the foreground, they both will fail if the existing app is already running on a different desktop.

    See the attached files for the modified SingleInstance.zip project.

    For an example of this approach used in a MFC dialog app, check out the OnlyOne app in the source of the Win32 Thread Sync article listed in my signature line.
    Attached Files Attached Files
    Last edited by Arjay; September 10th, 2007 at 02:39 PM.

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