监视应用程序
动态数据交换管理库 (DDEML) 的 API 元素可用于创建监视系统中的动态数据交换 (DDE) 活动的应用程序。 与任何 DDEML 应用程序一样,DDE 监视应用程序包含 DDE 回调函数。 每当发生 DDE 事件时,DDEML 都会通知监视应用程序的 DDE 回调函数,从而将有关该事件的信息传递给回调函数。 应用程序通常会在窗口中显示信息或将其写入文件。
若要从 DDEML 接收通知,必须在调用 DdeInitialize 函数时指定 APPCLASS_MONITOR 标志,以将应用程序注册为 DDE 监视器。 在此同一调用中,应用程序可以指定一个或多个监视器标志,以指示 DDEML 要通知应用程序的回调函数的事件类型。 应用程序可以指定以下监视器标志:
标记 | 说明 |
---|---|
MF_CALLBACKS | 每当将事务发送到系统中的任何 DDE 回调函数时,都通知回调函数。 |
MF_CONV | 每当建立或终止对话时,都通知回调函数。 |
MF_ERRORS | 每当发生 DDEML 错误时,都通知回调函数。 |
MF_HSZ_INFO | 每当 DDEML 应用程序创建、释放或递增字符串句柄的使用计数,或每当作为调用 DdeUninitialize 函数的结果释放字符串句柄时,都通知回调函数。 |
MF_LINKS | 每当启动或结束建议循环时,都通知回调函数。 |
MF_POSTMSGS | 每当系统或应用程序发布 DDE 消息时,都通知回调函数。 |
MF_SENDMSGS | 每当系统或应用程序发送 DDE 消息时,都通知回调函数。 |
以下示例演示如何注册 DDE 监视应用程序,以便其 DDE 回调函数收到所有 DDE 事件的通知。
DWORD idInst;
PFNCALLBACK lpDdeProc;
hInst = hInstance;
if (DdeInitialize(
(LPDWORD) &idInst, // instance identifier
DDECallback, // pointer to callback function
APPCLASS_MONITOR | // this is a monitoring application
MF_CALLBACKS | // monitor callback functions
MF_CONV | // monitor conversation data
MF_ERRORS | // monitor DDEML errors
MF_HSZ_INFO | // monitor data handle activity
MF_LINKS | // monitor advise loops
MF_POSTMSGS | // monitor posted DDE messages
MF_SENDMSGS, // monitor sent DDE messages
0)) // reserved
{
return FALSE;
}
DDEML 会将 XTYP_MONITOR 事务发送到应用程序的 DDE 回调函数,以通知监视应用程序发生 DDE 事件。 在此事务中,DDEML 会传递一个监视器标志,以指定发生的 DDE 事件的类型,以及 DDE 对象的句柄,该对象包含有关该事件的详细信息。 DDEML 提供了一组结构,应用程序可以使用这些结构从 DDE 对象中提取信息。 每种 DDE 事件都有相应的结构。
结构 | 说明 |
---|---|
MONCBSTRUCT | 包含有关事务的信息。 |
MONCONVSTRUCT | 包含有关对话的信息。 |
MONERRSTRUCT | 包含有关最新 DDE 错误的信息。 |
MONLINKSTRUCT | 包含有关建议循环的信息。 |
MONHSZSTRUCT | 包含有关字符串句柄的信息。 |
MONMSGSTRUCT | 包含有关已发送或已发布的 DDE 消息的信息。 |
以下示例演示 DDE 监视应用程序的 DDE 回调函数,该函数将设置有关每个字符串句柄事件的信息的格式,然后在窗口中显示此类信息。 该函数将使用 MONHSZSTRUCT 结构从 DDE 对象中提取信息。
HDDEDATA CALLBACK DDECallback(uType, uFmt, hconv, hsz1, hsz2,
hdata, dwData1, dwData2)
UINT uType;
UINT uFmt;
HCONV hconv;
HSZ hsz1;
HSZ hsz2;
HDDEDATA hdata;
DWORD dwData1;
DWORD dwData2;
{
LPVOID lpData;
CHAR *szAction;
CHAR szBuf[256];
DWORD cb;
HRESULT hResult;
switch (uType)
{
case XTYP_MONITOR:
// Obtain a pointer to the global memory object.
if (lpData = DdeAccessData(hdata, &cb))
{
// Examine the monitor flag.
switch (dwData2)
{
case MF_HSZ_INFO:
#define PHSZS ((MONHSZSTRUCT *)lpData)
// The global memory object contains
// string handle data. Use the MONHSZSTRUCT
// structure to access the data.
switch (PHSZS->fsAction)
{
// Examine the action flags to determine
// the action performed on the handle.
case MH_CREATE:
szAction = "Created";
break;
case MH_KEEP:
szAction = "Incremented";
break;
case MH_DELETE:
szAction = "Deleted";
break;
case MH_CLEANUP:
szAction = "Cleaned up";
break;
default:
DdeUnaccessData(hdata);
return (HDDEDATA) 0;
}
// Write formatted output to a buffer.
hResult = StringCchPrintf(szBuf, 256/sizeof(TCHAR),
"Handle %s, Task: %x, Hsz: %lx(%s)",
(LPSTR) szAction, PHSZS->hTask,
PHSZS->hsz, (LPSTR) PHSZS->str);
if (FAILED(hResult))
{
// TO DO: Write error handler.
return;
}
// Display text or write to a file.
break;
#undef PHSZS
// Process other MF_* flags.
default:
break;
}
}
// Free the global memory object.
DdeUnaccessData(hdata);
break;
default:
break;
}
return (HDDEDATA) 0;
}