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 />

hszAppName Handle of the item name, must be obtained by calling function<br />

::DdeCreateStringHandle()<br />

The server must check if it supports the topic and the specific item, as well as the data format. If it<br />

supports all of them, the server must prepare data using the required format and send the data back to client.<br />

Preparing Data<br />

One way to send data to the client is to prepare data in server’s local buffers, then create a handle for<br />

this data, and send the handle to the client. The data handle can be obtained by calling function<br />

::DdeCreateDataHandle(…), which has the following format:<br />

HDDEDATA DdeCreateDataHandle<br />

(<br />

DWORD idInst,<br />

LPBYTE pSrc,<br />

DWORD cb,<br />

DWORD cbOff,<br />

HSZ hszItem,<br />

UINT wFmt,<br />

UINT afCmd<br />

);<br />

The data can also be sent through a pointer. We will discuss this in a later section.<br />

The following table explains the meanings of the above parameters:<br />

Parameter<br />

idInst<br />

pSrc<br />

cb<br />

cbOff<br />

hszItem<br />

wFmt<br />

afCmd<br />

Meaning<br />

Instance identification obtained from DDE initialization<br />

Pointer to the buffers which hold the data<br />

The length of the data in bytes<br />

Specify the offset from the beginning of the buffer<br />

String handle, indicating the item name<br />

Indicates the data format<br />

The creation flags which indicates who is responsible for freeing the data<br />

We can use standard clipboard format such CF_TEXT, CF_DIB to pass data. If we define a special data<br />

format, we must register it before passing the data.<br />

Receiving Data<br />

Because data is not sent directly, the client must obtain the required data from the handle first. For<br />

synchronous transmission mode, the data handle will be returned directly from function<br />

::DdeClientTransacton(). In case the transaction is not successful, a NULL value will be returned.<br />

After receiving the handle, the client must first call function ::DdeAccessData(…) to access the data.<br />

After the data is processed, function ::DdeUnaccessData(…) must be called to “unaccess” the data. If the<br />

data is created with HDATA_APPOWNED flag, the client should not free the data. Otherwise, it can release the<br />

data by calling function ::DdeFreeDataHandle(…).<br />

Samples<br />

In the sample application, an item “Time” is supported by the server under “Topic” topic name. When<br />

the client request an XTYPE_REQUEST transaction on this item, the server get the current system time and<br />

send it to the client. The client then displays the time in one of its edit box.<br />

Compared with the samples in the previous section, a new edit box and a button labeled “Request” are<br />

added to the dialog box (Figure 15-3). The edit box is read-only, which will be used to display the time<br />

obtained from the server. The button is used to let the user initiate the request transaction.<br />

468

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

Saved successfully!

Ooh no, something went wrong!