11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 16. Context Sensitive Help<br />

hData=::DdeCreateDataHandle<br />

(<br />

m_dwInst,<br />

(LPBYTE)(LPCSTR)szTime,<br />

szTime.GetLength(),<br />

0,<br />

m_hszTimeItem,<br />

CF_TEXT,<br />

0<br />

);<br />

……<br />

}<br />

Printf("Responded to client's request for time item\r\n");<br />

return hData;<br />

}<br />

else<br />

{<br />

Printf("Do not support this itme!\r\n");<br />

return (HDDEDATA)FALSE;<br />

}<br />

15.4 Transaction: Advise<br />

Basics<br />

Another interesting transaction is Advise, which provides a way to let the server inform the client after<br />

an item stored on the server side has changed. The advise transaction can be initiated from the client side<br />

by initiating XTYP_ADVSTART type transaction. As the server receives this message, it keeps an eye on the<br />

item that is required by the client for advise service. If the data changes, the server will receive an<br />

XTYP_ADVREQ message indicating that the item has changed (This message is posted by the server itself,<br />

which could be triggered by any event indicating that the topic item has changed. For example, for “time”<br />

item discussed in the previous section, it could be triggered by message WM_TIMER). After receiving<br />

message XTYP_ADVREQ, the server sends an XTYP_ADVDATA message along with the handle of the updated<br />

data to the client. After the client receives this message, it updates the advised topic item.<br />

Now that we understand how XTYP_REQUEST type transaction is handled, it is easier for us to figure out<br />

how the advise transaction should be implemented. First, the client initiates advise transaction by calling<br />

function ::DdeClientTransaction(…) and passing XTYP_ADVSTART to parameter wType. Upon receiving<br />

this message, the server must return TURE if it supports the specified topic and item; otherwise, it should<br />

return FALSE. Once the data has changed, the server should call function ::DdePostAdvise(…) to let its<br />

callback function receive an XTYP_ADVREQ message. Upon receiving this message, the server needs to<br />

prepare the data and send the data handle to the client (There is no special function for doing this, all the<br />

server needs to do is returning the data handle from the callback function). Once the server has provided<br />

advise, the client will receive an XTYP_ADVDATA message along with the data handle. After receiving this<br />

message, the client should update the advised item. The client can terminate the advise service at any time<br />

by sending XTYP_ADVSTOP message through calling function ::DdeClientTransaction(…).<br />

Initiating Advise Transaction<br />

In the samples contained in 15.4\DDE\, a new topic item “Text” is added to both client and server,<br />

which will be used as an example for advise transaction. Like “Time” item, a new edit box and a button<br />

labeled “Advise” are also added to the dialog box (Figure 15-4). On the server side, a variable<br />

CMainFrame::m_szText is declared and the user can edit this string through dialog box<br />

IDD_DIALOG_ADVISE. The following code fragment shows how the client initiates XTYPE_ADVSTART<br />

transaction after the user clicks “Advise” button:<br />

470

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!