The Windowsoperating system provides mechanisms for facilitating communications and datasharing between applications. Collectively, the activities enabled by thesemechanisms are calledinterprocess communications (IPC). 有些机制只能实现同一computer上运行的进程间通讯,还有一些能够实现跨网络的进程间通讯。
After youdecide that your application would benefit from IPC, you must decide which ofthe available IPC methods to use. 答案取决于以下几个方面:
· 进程间通讯是否跨网络 (between applications on the localcomputer or across computers on a network)?
· 是否跨系统平台(operating systems)?
· 跟已知的特定Application交流还是跟不确指的Application交流?
· 对通讯内容的format是否有特殊要求?
· Isperformance a critical aspect of the application? All IPC mechanisms includesome amount of overhead.
· 是解决遗留系统间的通讯吗?当前的新技术(WCF)适用吗?
Using the Clipboard for IPC
The clipboardacts as a central depository for data sharing among applications. The clipboardis a very loosely coupled exchange medium, where applications need only agreeon the data format.
对于Window来说, the clipboard is a set of functions and messages thatenable applications to transfer data.The applications can reside on thesame computer or on different computers on a network.
In fact, theclipboard is a software facility used for short-term data storage and/or datatransfer between documents or applications。是底层技术封装后的功能,并以接口的形式暴露给GUI程序使用!
Earlyimplementations of the clipboard stored data as plain text withoutmeta-information such as typeface, type style or color. More recentimplementations support the multiple types of data, allowing complex datastructures to be stored. These range from styled text formats such as RTF orHTML, through a variety of bitmap and vector image formats to complex datatypes like spreadsheets and database entries.
In Windows inparticular, the internal clipboard functionality of the operating system willautomatically translate data from known advanced formats to simpler formats(such as RTF to plain text, or Unicode to ANSI Text), increasing the likelihoodthat any given application can interpret some form of the original data.
ClipboardMessages:WM_CLEAR, WM_COPY, WM_CUT, WM_PASTE
Using DDE for IPC
DDE is aprotocol that enables applications to exchange data in a variety of formats.Applications can use DDE for one-time data exchanges or for ongoing exchangesin which the applications update one another as new data becomes available.
The dataformats used by DDE are the same as those used by the clipboard. DDE can bethought of as an extension of the clipboard mechanism. The clipboard is almostalways used for a one-time response to a user command, such as choosing thePaste command from a menu. DDE is also usually initiated by a user command, butit often continues to function without further user interaction. You can alsodefine custom DDE data formats for special-purpose IPC between applicationswith more tightly coupled communications requirements.
DDE exchangescan occur between applications running on the same computer or on differentcomputers on a network.
Key Point: DDE is not as efficient as newertechnologies. However, you can still use DDE if other IPC mechanisms are notsuitable or if you must interface with an existing application that onlysupports DDE. For more information, seeDynamic Data Exchange and Dynamic Data ExchangeManagement Library.
using themessage-based DDE protocol are fully compatible with those that use the DDEML;
The Dynamic DataExchange Management Library (DDEML) provides an interface that simplifies thetask of adding DDE capability to an application. Instead of sending, posting,and processing DDE messages directly, an application uses the functionsprovided by the DDEML to manage DDE conversations.
DDE是对剪切板机制的延伸,提供持续不断通讯的机制。DDEML是对DDE动能以类库的形式加以包装从而更加方便使用。
以了解为主,毕竟有很多更新更方便是有的新技术。
Using Data Copy for IPC
Data copyenables an application to send information to another application using theWM_COPYDATA message. This methodrequires cooperation between the sending application and the receivingapplication. The receiving application must know the format of the informationand be able to identify the sender.
To useWM_COPYDATA, SendMessage is called as follows :
SendMessage(hwReceiver, WM_COPYDATA, (WPARAM) hwSender, (LPARAM) pData );
KeyPoint: Datacopy can be used to quickly send information to another application usingWindows messaging
例子:
Click "Send Copy Msg" menu to send one WM_COPYDATA message with custom data:MYREC MyRec to the destination window. The destination window's title is "WinTitleInOtherProcess"
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
//......
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case ID_FILE_SEND:
{
#define MYDISPLAY 1
typedef struct tagMYREC
{
wchar_t s1[80];
wchar_t s2[80];
DWORD n;
} MYREC;
COPYDATASTRUCT MyCDS;
MYREC MyRec;
HRESULT hResult;
wchar_t szFirstName[100] = _T("Nick");
wchar_t szLastName[100] = _T("Liu");
int nAge = 37;
hResult = StringCbCopy(MyRec.s1, sizeof(MyRec.s1), szFirstName);
hResult = StringCbCopy(MyRec.s2, sizeof(MyRec.s2), szLastName);
MyRec.n = nAge;
//
// Fill the COPYDATA structure
//
MyCDS.dwData = MYDISPLAY; // function identifier
MyCDS.cbData = sizeof(MyRec); // size of data
MyCDS.lpData = &MyRec; // data structure
//
// Call function, passing data in &MyCDS
//
//Retrieves a handle to the top - level window(can across process) whose class name and window name match the specified strings.
HWND hwDispatch = FindWindow(NULL, _T("WinTitleInOtherProcess"));
if (hwDispatch != NULL)
SendMessage(hwDispatch,
WM_COPYDATA,
(WPARAM)(HWND)hWnd,
(LPARAM)(LPVOID)&MyCDS);
else
MessageBox(hWnd, _T("Can't send WM_COPYDATA"), _T("MyApp"), MB_OK);
break;
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
BOOL CMFCDLGApplicationDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
// TODO: Add your message handler code here and/or call default
#define MYDISPLAY 1
typedef struct tagMYREC
{
wchar_t s1[80];
wchar_t s2[80];
DWORD n;
} MYREC;
switch (pCopyDataStruct->dwData)
{
case MYDISPLAY:
MYREC* pData = (MYREC *)(pCopyDataStruct->lpData);
SetDlgItemText(IDC_EDIT_FIRSTNAME, pData->s1);
SetDlgItemText(IDC_EDIT_LASTNAME, pData->s2);
char CStr[25];
_itoa_s(pData->n, CStr, 10);
size_t len = strlen(CStr) + 1;
size_t converted = 0;
wchar_t *WStr;
WStr = (wchar_t*)malloc(len*sizeof(wchar_t));
mbstowcs_s(&converted, WStr, len, CStr, _TRUNCATE);
SetDlgItemText(IDC_EDIT_AGE, WStr);
}
return CDialogEx::OnCopyData(pWnd, pCopyDataStruct);
}