是否需要用到自定义消息,通过窗口句柄传递

解决方案 »

  1.   

    第二次启动的程序通过消息等传递给第一个进程,然后自己退出自定义消息,通过窗口句柄传递
    ---这个方法可以
      

  2.   

    自己解决了
    // test_onlyOneExe.cpp : Defines the class behaviors for the application.
    //#include "stdafx.h"
    #include "test_onlyOneExe.h"
    #include "test_onlyOneExeDlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CTest_onlyOneExeAppBEGIN_MESSAGE_MAP(CTest_onlyOneExeApp, CWinApp)
    //{{AFX_MSG_MAP(CTest_onlyOneExeApp)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //    DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG
    ON_COMMAND(ID_HELP, CWinApp::OnHelp)
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CTest_onlyOneExeApp constructionCTest_onlyOneExeApp::CTest_onlyOneExeApp()
    {
    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
    }/////////////////////////////////////////////////////////////////////////////
    // The one and only CTest_onlyOneExeApp objectCTest_onlyOneExeApp theApp;/////////////////////////////////////////////////////////////////////////////
    // CTest_onlyOneExeApp initializationBOOL CTest_onlyOneExeApp::InitInstance()
    {
    if(!IsFirstInstance("test_onlyOneExe"))
    return false; AfxEnableControlContainer(); // Standard initialization
    // If you are not using these features and wish to reduce the size
    //  of your final executable, you should remove from the following
    //  the specific initialization routines you do not need.#ifdef _AFXDLL
    Enable3dControls(); // Call this when using MFC in a shared DLL
    #else
    Enable3dControlsStatic(); // Call this when linking to MFC statically
    #endif CTest_onlyOneExeDlg dlg;
    m_pMainWnd = &dlg;
    int nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with Cancel
    } // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;
    }BOOL CTest_onlyOneExeApp::IsFirstInstance(CString title)
    {
    CWnd *pWndPrev, *pWndChild;
    if (pWndPrev = CWnd::FindWindow(NULL,title))
    {
    //AfxMessageBox( _TEXT("只允许一个实例在运行!"));
    pWndChild = pWndPrev->GetLastActivePopup();
    if (pWndPrev->IsIconic())
    pWndPrev->ShowWindow(SW_RESTORE);
    pWndChild->SetForegroundWindow(); //找到并激活到前端 CString sCopyData = GetCommandLine();
    COPYDATASTRUCT cpd;
    cpd.dwData = 0;
    cpd.cbData = sCopyData.GetLength() + 1;//多加一个长度,防止乱码
    cpd.lpData = (void*)sCopyData.GetBuffer(cpd.cbData);
    pWndChild->SendMessage(WM_COPYDATA,NULL,(LPARAM)&cpd); // pWndChild->PostMessage(WM_GXMSG,0,100);//发消息让已经有的实例到前段,当然也可以发消息让其退出
    return FALSE;
    }
    else
    return TRUE; // 第一个实例}