single instance dialog

I have a dialog application and I only want a single instance of it running at one time.  How can I do this.  I have tried these articles and they didn't seem to work for me:
http://www.codeproject.com/threads/singleinstancemfc.asp
http://www.codeguru.com/cpp/w-d/dislog/dialog-basedapplications/article.php/c1971/
http://www.codeproject.com/cpp/avoidmultinstance.asp
I must be missing something obvious so I've decided to take a step back and pretend I didn't read any of them and ask how to do it.
dociebAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DanRollinsCommented:
Those techniques *do* work.  Here is my version:

#ifndef LimitSingleInstance_H
#define LimitSingleInstance_H

#include <windows.h>

//this code is from Q243953 in case you lose the article and wonder
//where this code came from...
class CLimitSingleInstance
{
protected:
  DWORD  m_dwLastError;
  HANDLE m_hMutex;

public:
  CLimitSingleInstance(TCHAR *strMutexName)
  {
    //be sure to use a name that is unique for this application otherwise
    //two apps may think they are the same if they are using same name for
    //3rd parm to CreateMutex
    m_hMutex = CreateMutex(NULL, FALSE, strMutexName); //do early
    m_dwLastError = GetLastError(); //save for use later...
  }
   
  ~CLimitSingleInstance()
  {
    if (m_hMutex)  //don't forget to close handles...
    {
       CloseHandle(m_hMutex); //do as late as possible
       m_hMutex = NULL; //good habit to be in
    }
  }

  BOOL IsAnotherInstanceRunning()
  {
    return (ERROR_ALREADY_EXISTS == m_dwLastError);
  }
};
#endif  //#ifndef LimitSingleInstance_H

=-=-==-=-=-=-=-

Then in the app...

CLimitSingleInstance g_SingleInstanceObj( "CreditBaseApp" ); // checked in InitInstance

in InitInstance...

   //------------------------------------ avoid running two copies
    if ( g_SingleInstanceObj.IsAnotherInstanceRunning() ) {
        CWnd* pWnd=  CWnd::FindWindow( NULL, CSTR_AppWinTitle );
        if ( pWnd )   {
              CWnd* pWndChild= pWnd->GetLastActivePopup(); // If so, does it have any popups?
            // if ( pWnd->IsIconic() ) {            // If iconic, restore the main window
            if ( !pWnd->IsWindowVisible() ) {            // If iconic, restore the main window
                    pWnd->ShowWindow(SW_RESTORE); // tray app stuff
              }
              pWndChild->SetForegroundWindow(); // Bring the main window or its popup to the foreground
        }
        return FALSE; // dont start this instance
    }
.... else, run normally ...

-- Dan

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dociebAuthor Commented:
Ahh, dumb me.  I figured out what I was doing.  I forgot that I changed the folder that I was placing the release in and I kept trying it with older version.  I used yours and it works and the others probably works also.  Man the last few hours was a waste of time.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.