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. How can I write a program in VC++6 such that only ONE instance of

How can I write a program in VC++6 such that only ONE instance of

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
5 Posts 3 Posters 2 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.
  • D Offline
    D Offline
    DengJW
    wrote on last edited by
    #1

    this program can be run at any time? I customized media Player based on Windows Midia player control. However, the media player could not be run exclusively, that means if I try to run two of them, each one will open a media file and play simultaneously. How can I program so that only one media player program can run at any time? Like using Windows MEdia Player, if you try to run a second movie file, the Media Player will close the first media file and turn to start running the second media file? The platforms are VC++6 and WIndows 2000 Pro. Appreciate your help! JW DJ

    7 F D 3 Replies Last reply
    0
    • D DengJW

      this program can be run at any time? I customized media Player based on Windows Midia player control. However, the media player could not be run exclusively, that means if I try to run two of them, each one will open a media file and play simultaneously. How can I program so that only one media player program can run at any time? Like using Windows MEdia Player, if you try to run a second movie file, the Media Player will close the first media file and turn to start running the second media file? The platforms are VC++6 and WIndows 2000 Pro. Appreciate your help! JW DJ

      7 Offline
      7 Offline
      73Zeppelin
      wrote on last edited by
      #2

      See the following articles: http://www.codeproject.com/useritems/Single_Instance.asp http://www.codeproject.com/cpp/singletonapp.asp http://www.codeproject.com/cpp/csingleinst.asp http://www.codeproject.com/cpp/avoidmultinstance.asp http://www.codeproject.com/csharp/cssingprocess.asp

      1 Reply Last reply
      0
      • D DengJW

        this program can be run at any time? I customized media Player based on Windows Midia player control. However, the media player could not be run exclusively, that means if I try to run two of them, each one will open a media file and play simultaneously. How can I program so that only one media player program can run at any time? Like using Windows MEdia Player, if you try to run a second movie file, the Media Player will close the first media file and turn to start running the second media file? The platforms are VC++6 and WIndows 2000 Pro. Appreciate your help! JW DJ

        F Offline
        F Offline
        Florin Ochiana
        wrote on last edited by
        #3

        Excerpt from MSDN: You will find this in the file msdn_mfcfaq50.htm (MFC FAQ). --------------- How do I limit my MFC application to one instance? Look at the Microsoft C++ sample ONETIME. In brief: const char* MyMainWndClassName = "MyMainWndXQW" BOOL CMyApp::InitApplication() { // Call base class. Default version does nothing. CWinApp::InitApplication(); WNDCLASS wndcls; // Start with NULL defaults. memset(&wndcls, 0, sizeof(WNDCLASS)); // Get class information for default window class. ::GetClassInfo(AfxGetInstanceHandle(),"AfxFrameOrView",&wndcls); // Substitute unique class name for new class. wndcls.lpszClassName = MyMainWndClassName; // Register new class and return the result code. return ::RegisterClass(&wndcls); } And: BOOL CMyApp::FirstInstance() { CWnd *PrevCWnd, *ChildCWnd; // Determine if another window with our class name exists. PrevCWnd = CWnd::FindWindow(MyMainWndClassName, NULL); if (PrevCWnd != NULL) { // If so, does it have any pop-ups? ChildCWnd=PrevCWnd->GetLastActivePopup(); // Bring the main window to the top. PrevCWnd->BringWindowToTop(); // If iconic, restore the main window. if (PrevCWnd->IsIconic()) PrevCWnd->ShowWindow(SW_RESTORE); // If there are pop-ups, bring them along too! if (PrevCWnd != ChildCWnd) ChildCWnd->BringWindowToTop(); // Return FALSE. This isn't the first instance // and we are done activating the previous one. return FALSE; } else // First instance. Proceed as normal. return TRUE; } CMyApp::InitInstance() { if (!FirstInstance()) return FALSE; // ... } null@diku.dk, programmer.tools, 6/19/95 See also Win32 SDK Knowledge Base article Q124134 ("Allowing Only One Application Instance on Win32s") and Jeffrey Richter's Advanced Windows NT, chapter 7, "Prohibiting Multiple Instances of an Application from Running: The MultInst Sample Application" (available on the MSDN Library CD). null@diku.dk, email, 8/8/95 Update—these were posted to mfc-l: I have each InitApplication() create a semaphore. If GetLastError() returns ERROR_ALREADY_EXISTS then I know that some other application is already running and has gotten that far, so I bail. Yourapp::InitInstance() { hMutexOneInstance = CreateMutex(NULL,TRUE,_T("PreventSecondInstance")); if(GetLastError() == ERROR_ALREADY_EXIST

        D 1 Reply Last reply
        0
        • F Florin Ochiana

          Excerpt from MSDN: You will find this in the file msdn_mfcfaq50.htm (MFC FAQ). --------------- How do I limit my MFC application to one instance? Look at the Microsoft C++ sample ONETIME. In brief: const char* MyMainWndClassName = "MyMainWndXQW" BOOL CMyApp::InitApplication() { // Call base class. Default version does nothing. CWinApp::InitApplication(); WNDCLASS wndcls; // Start with NULL defaults. memset(&wndcls, 0, sizeof(WNDCLASS)); // Get class information for default window class. ::GetClassInfo(AfxGetInstanceHandle(),"AfxFrameOrView",&wndcls); // Substitute unique class name for new class. wndcls.lpszClassName = MyMainWndClassName; // Register new class and return the result code. return ::RegisterClass(&wndcls); } And: BOOL CMyApp::FirstInstance() { CWnd *PrevCWnd, *ChildCWnd; // Determine if another window with our class name exists. PrevCWnd = CWnd::FindWindow(MyMainWndClassName, NULL); if (PrevCWnd != NULL) { // If so, does it have any pop-ups? ChildCWnd=PrevCWnd->GetLastActivePopup(); // Bring the main window to the top. PrevCWnd->BringWindowToTop(); // If iconic, restore the main window. if (PrevCWnd->IsIconic()) PrevCWnd->ShowWindow(SW_RESTORE); // If there are pop-ups, bring them along too! if (PrevCWnd != ChildCWnd) ChildCWnd->BringWindowToTop(); // Return FALSE. This isn't the first instance // and we are done activating the previous one. return FALSE; } else // First instance. Proceed as normal. return TRUE; } CMyApp::InitInstance() { if (!FirstInstance()) return FALSE; // ... } null@diku.dk, programmer.tools, 6/19/95 See also Win32 SDK Knowledge Base article Q124134 ("Allowing Only One Application Instance on Win32s") and Jeffrey Richter's Advanced Windows NT, chapter 7, "Prohibiting Multiple Instances of an Application from Running: The MultInst Sample Application" (available on the MSDN Library CD). null@diku.dk, email, 8/8/95 Update—these were posted to mfc-l: I have each InitApplication() create a semaphore. If GetLastError() returns ERROR_ALREADY_EXISTS then I know that some other application is already running and has gotten that far, so I bail. Yourapp::InitInstance() { hMutexOneInstance = CreateMutex(NULL,TRUE,_T("PreventSecondInstance")); if(GetLastError() == ERROR_ALREADY_EXIST

          D Offline
          D Offline
          DengJW
          wrote on last edited by
          #4

          how to implement or integrate them into an existing VC++6 Diaglogue based program so that I can use some of the ActiveX Controls? THanks JW DJ

          1 Reply Last reply
          0
          • D DengJW

            this program can be run at any time? I customized media Player based on Windows Midia player control. However, the media player could not be run exclusively, that means if I try to run two of them, each one will open a media file and play simultaneously. How can I program so that only one media player program can run at any time? Like using Windows MEdia Player, if you try to run a second movie file, the Media Player will close the first media file and turn to start running the second media file? The platforms are VC++6 and WIndows 2000 Pro. Appreciate your help! JW DJ

            D Offline
            D Offline
            DengJW
            wrote on last edited by
            #5

            Really appreciate your help! JW DJ

            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