Monitoring Applications

The API elements of the Dynamic Data Exchange Management Library (DDEML) can be used to create an application that monitors Dynamic Data Exchange (DDE) activity in the system. Like any DDEML application, a DDE monitoring application contains a DDE callback function. The DDEML notifies a monitoring application's DDE callback function whenever a DDE event occurs, passing information about the event to the callback function. The application typically displays the information in a window or writes it to a file.

To receive notifications from the DDEML, an application must have registered as a DDE monitor by specifying the APPCLASS_MONITOR flag in a call to the DdeInitialize function. In this same call, the application can specify one or more monitor flags to indicate the types of events for which the DDEML is to notify the application's callback function. The following monitor flags can be specified by an application:

MF_CALLBACKSNotifies the callback function whenever a transaction is sent to any DDE callback function in the system.
MF_CONVNotifies the callback function whenever a conversation is established or terminated.
MF_ERRORSNotifies the callback function whenever a DDEML error occurs.
MF_HSZ_INFONotifies the callback function whenever a DDEML application creates, frees, or increments the usage count of a string handle or whenever a string handle is freed as a result of a call to the DdeUninitialize function.
MF_LINKSNotifies the callback function whenever an advise loop is started or ended.
MF_POSTMSGSNotifies the callback function whenever the system or an application posts a DDE message.
MF_SENDMSGSNotifies the callback function whenever the system or an application sends a DDE message.


The following example shows how to register a DDE monitoring application so that its DDE callback function receives notifications of all DDE events.

DWORD idInst; 
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; 

The DDEML informs a monitoring application of a DDE event by sending an XTYP_MONITOR transaction to the application's DDE callback function. During this transaction, the DDEML passes a monitor flag that specifies the type of DDE event that has occurred and a handle to a DDE object that contains detailed information about the event. The DDEML provides a set of structures that the application can use to extract the information from the DDE object. There is a corresponding structure for each type of DDE event.

MONCBSTRUCT Contains information about a transaction.
MONCONVSTRUCT Contains information about a conversation.
MONERRSTRUCT Contains information about the latest DDE error.
MONLINKSTRUCT Contains information about an advise loop.
MONHSZSTRUCT Contains information about a string handle.
MONMSGSTRUCT Contains information about a DDE message that was sent or posted.


The following example shows the DDE callback function of a DDE monitoring application that formats information about each string handle event and then displays the information in a window. The function uses the MONHSZSTRUCT structure to extract the information from the DDE object.

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"; 
                            case MH_KEEP: 
                                szAction = "Incremented"; 
                            case MH_DELETE: 
                                szAction = "Deleted"; 
                            case MH_CLEANUP: 
                                szAction = "Cleaned up"; 
                                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.
                        // Display text or write to a file. 
#undef PHSZS 
                    // Process other MF_* flags. 
            // Free the global memory object. 
    return (HDDEDATA) 0;