// The one and only window has been initialized, so show and update it. m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; } 下面该例子改写了CAppComuApp::PreTranslateMessage,在这里处理自定义消息 BOOL CAppComuApp::PreTranslateMessage(MSG* pMsg) { if( pMsg->message == m_uUserMessage ) { AfxMessageBox("there is an instance running in the sys"); return TRUE; // 自定义消息已经被处理 } return CWinApp::PreTranslateMessage(pMsg); }
1。使用公用文件; 2。使用WinSock; 3。使用DDE; 4。使用COM。
WM_COPYDATAAn application sends the WM_COPYDATA message to pass data to another application. To send this message, call the SendMessage function with the following parameters (do not call the PostMessage function). SendMessage( (HWND) hWnd, // handle to destination window WM_COPYDATA, // message to send (WPARAM) wParam, // handle to window (HWND) (LPARAM) lParam // data (PCOPYDATASTRUCT) );
具体的程序你可以在网上找。
首先,先讲一些预备知识:
在VC中自定义消息的方法:
一般在单一的应用程序(指无需在进程之间通信的)中 ,自定义消息方法有:
(1)借助预处理器的帮助:在resourse.h中,#define UDM_MESSAGE WM_USER+XXX //UDM: user-defined messages;
大多数人都这么做。
(2)最近有些大虾们推荐使用标准C++语法定义,这样可以最大可能的发挥C++编译器的作用;
const UINT UDM_MESSAGE somenum;//somenum 在0xC000 和0xFFFF之间;
若在不止一个应用之间使用同一个消息,这就要使用一个API来定义一个全局消息了这个API是
UINT RegisterWindowMessage(LPCTSTR lpString // address of message string);
这个全局消息在程序启动后被注册,若有多个程序同时注册了这个消息,应用程序将得到相同的返回值 。(这个返回值在0xC000 和 0xFFFF之间)
本文由于讲的是应用程序间的通讯将涉及到不止一个应用的运行,所以自然用的是第二种方法了。
本文的举例程序是部分修改自Jean-Claude Garreau的文章"Limiting Applications to a Single Instance "中的例子。本例子利用的是窗口通信技术来实现限制应用程序只运行一个实例。
下面仅就一些关键的技术进行分析:
例子先在主应用程序类CAppComuApp的.h文件中为该类添加了一个protected数据成员作为注册消息返回 码:
UINT m_uUserMessage。然后例子改写了CAppComuApp::initinstance()
部分代码如下:
BOOL CAppComuApp::initinstance()
{
//...
if (!ProcessShellCommand(cmdInfo))
return FALSE;
//以下代码使察看在本机系统上是否已有一个实例正在运行
//在此注册一消息
m_uUserMessage=RegisterWindowMessage("IS_ANOTHER_INSTANCE");
//读取section"Control"下的"MainWndHwnd"的值,其实是正在运行的实例
//的主窗口句柄,后面就是靠它发消息的
CString str=GetProfileString("Control","MainWndHwnd","0");
sscanf(str,"%ld",&hWnd);
// Test existing window
if( IsWindow(hWnd) )// 若有一个实例在运行
{
// 发送自定义消息给那个正在运行的实例;
PostMessage(hWnd,m_uUserMessage,0,0);
// 将那个正在运行的实例推到前台
SetForegroundWindow(hWnd);
return FALSE;//退出
}
// 若无实例正在运行
// 将这个实例的窗口句柄写入到application’s .INI file 中
hWnd=m_pMainWnd->GetSafeHwnd();
str.Format("%ld",hWnd);
WriteProfileString("Control","MainWndHwnd",str);//在application’s .INI file 中
//创建section"control",并在该"section"下
//创建"MainWndHwnd",其值为str
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow(); return TRUE;
}
下面该例子改写了CAppComuApp::PreTranslateMessage,在这里处理自定义消息
BOOL CAppComuApp::PreTranslateMessage(MSG* pMsg)
{
if( pMsg->message == m_uUserMessage )
{
AfxMessageBox("there is an instance running in the sys");
return TRUE; // 自定义消息已经被处理
}
return CWinApp::PreTranslateMessage(pMsg);
}
2。使用WinSock;
3。使用DDE;
4。使用COM。
(HWND) hWnd, // handle to destination window
WM_COPYDATA, // message to send
(WPARAM) wParam, // handle to window (HWND)
(LPARAM) lParam // data (PCOPYDATASTRUCT)
);
当然还可以使用共享内存的方法,但是也需要知道何时被更改,最简单的是使用消息的方法。获得窗口句柄使用findwindow函数。
其他方法:
1。sock,管道,邮槽,消息队列
2。内存映像文件
3。剪贴板...