VC 程序只運行一個進程,再次運行傳參數給第一個進程的問題

// 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_onlyOneExeApp
BEGIN_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 construction

CTest_onlyOneExeApp::CTest_onlyOneExeApp()

29.{

30.	// TODO: add construction code here,

31.	// Place all significant initialization in InitInstance

32.}

33.

34./////////////////////////////////////////////////////////////////////////////

35.// The one and only CTest_onlyOneExeApp object

36.

37.CTest_onlyOneExeApp theApp;

38.

39./////////////////////////////////////////////////////////////////////////////

40.// CTest_onlyOneExeApp initialization

41.

42.BOOL CTest_onlyOneExeApp::InitInstance()

43.{

44.	if(!IsFirstInstance("test_onlyOneExe"))

45.		return false;

46.

47.	AfxEnableControlContainer();

48.

49.	// Standard initialization

50.	// If you are not using these features and wish to reduce the size

51.	//  of your final executable, you should remove from the following

52.	//  the specific initialization routines you do not need.

53.

54.#ifdef _AFXDLL

55.	Enable3dControls();			// Call this when using MFC in a shared DLL

56.#else

57.	Enable3dControlsStatic();	// Call this when linking to MFC statically

58.#endif

59.

60.	CTest_onlyOneExeDlg dlg;

61.	m_pMainWnd = &dlg;

62.	int nResponse = dlg.DoModal();

63.	if (nResponse == IDOK)

64.	{

65.		// TODO: Place code here to handle when the dialog is

66.		//  dismissed with OK

67.	}

68.	else if (nResponse == IDCANCEL)

69.	{

70.		// TODO: Place code here to handle when the dialog is

71.		//  dismissed with Cancel

72.	}

73.

74.	// Since the dialog has been closed, return FALSE so that we exit the

75.	//  application, rather than start the application's message pump.

76.	return FALSE;

77.}

78.

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; // 第一個實例

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章