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; // 第一个实例

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值