Advanced MFC Programming

Advanced MFC Programming Advanced MFC Programming

math.hcmuns.edu.vn
from math.hcmuns.edu.vn More from this publisher
11.04.2014 Views

Chapter 16. Context Sensitive Help conversation must be terminated. Either the server or the client can terminate the conversion. This can be implemented by calling function ::DdeDisconnect(…) using the conversion handle. This function can be called either from the server or from the client side. Once the message is sent, the other side will be notified of the disconnection by receiving an XTYP_DISCONNECT message. In the sample, once the connection is set up, it can be terminated from the client side by pressing “Disconnect” button or from the server side through executing DDE | Disconnect command. This is implemented as follows: The server side: void CMainFrame::OnDdeDisconnect() { if(m_hConvServer != 0) { ::DdeDisconnect(m_hConvServer); m_hConvServer=0; Printf("Disconnected by the server.\r\n"); } } The client side: …… void CDDECliDlg::OnButtonConnect() { if(m_hConvClient == 0) { } } else { ::DdeDisconnect(m_hConvClient); m_hConvClient=0; UnobtainHsz(m_hszTopicName); m_hszTopicName=0; m_btnConnect.SetWindowText("Connect"); Printf("Disconnected by the client!\r\n"); } The following shows how message XTYP_DISCONNECT is handled in both sides: The client side: …… …… case XTYP_DISCONNECT: { m_hConvClient=0; UnobtainHsz(m_hszTopicName); m_hszTopicName=0; ((CDDECliDlg *)(AfxGetApp()->m_pMainWnd))->m_btnConnect.SetWindowText("Connect"); Printf("Disconnected by the server.\r\n"); return (HDDEDATA)NULL; } The server side: …… case XTYP_DISCONNECT: { if(m_hConvServer == hConv) { m_hConvServer=0; Printf("Disconnected by the client.\r\n"); } return (HDDEDATA)FALSE; 464

Chapter 16. Context Sensitive Help …… } Test We can test this version of DDE client and server by starting both applications, then inputting “Topic” into the edit box contained in the client dialog box, and clicking “Connect” button (see Figure 15-1). After this, we will see that the server will output some information indicating if the connection is successful. Then, we can terminate the conversation either from the client or from the server side. We can also try to use an incorrect topic name while making the connection, this will cause the connection to be unsuccessful. To prevent the application from exiting without terminating the conversation, both client and server need to check if the conversation is still undergoing while exiting. If necessary, function ::DdeDisconnect(…) will be called before the applications exit. Figure 15-2 explains the procedure of DDE connection and disconnection. Input the topic name and click Connect button Figure 15-1. Initiate connection from the client side 15.3 Transaction: Data Request Samples 15.3\DDE\Server and 15.3\DDE\Client are based on samples 15.2\DDE\Server and 15.2\DDE\Client respectively. There are several types of transactions, one of which is XTYPE_REQUEST, it can be used by the client to request the server to send up-to-date value of an item to it. We know that there are two names that are used to set up a connection: the service name and the topic name. The service name is used to locate the server and the topic name is used to specify a general topic. Under any topic there may exist several items, each item also needs an item name. When the client wants to request a transaction from the server, it needs to specify the item name. For example, if a server provides a service that sends different type of data to the client(s), there may be different topics such as “Image”, “Text” (indicating different type of data). Under each topic, there may exist different items such as “Image A”, “Image B”, “Text 1”, “Text 2”. Data Request Transaction: Client Side The client should call function ::DdeClientTransaction(…) to initiate a transaction: HDDEDATA DdeClientTransaction ( LPBYTE pData, DWORD cbData, HCONV hConv, HSZ hszItem, UINT wFmt, UINT wType, DWORD dwTimeout, LPDWORD pdwResult 465

Chapter 16. Context Sensitive Help<br />

conversation must be terminated. Either the server or the client can terminate the conversion. This can be<br />

implemented by calling function ::DdeDisconnect(…) using the conversion handle. This function can be<br />

called either from the server or from the client side. Once the message is sent, the other side will be notified<br />

of the disconnection by receiving an XTYP_DISCONNECT message.<br />

In the sample, once the connection is set up, it can be terminated from the client side by pressing<br />

“Disconnect” button or from the server side through executing DDE | Disconnect command. This is<br />

implemented as follows:<br />

The server side:<br />

void CMainFrame::OnDdeDisconnect()<br />

{<br />

if(m_hConvServer != 0)<br />

{<br />

::DdeDisconnect(m_hConvServer);<br />

m_hConvServer=0;<br />

Printf("Disconnected by the server.\r\n");<br />

}<br />

}<br />

The client side:<br />

……<br />

void CDDECliDlg::OnButtonConnect()<br />

{<br />

if(m_hConvClient == 0)<br />

{<br />

}<br />

}<br />

else<br />

{<br />

::DdeDisconnect(m_hConvClient);<br />

m_hConvClient=0;<br />

UnobtainHsz(m_hszTopicName);<br />

m_hszTopicName=0;<br />

m_btnConnect.SetWindowText("Connect");<br />

Printf("Disconnected by the client!\r\n");<br />

}<br />

The following shows how message XTYP_DISCONNECT is handled in both sides:<br />

The client side:<br />

……<br />

……<br />

case XTYP_DISCONNECT:<br />

{<br />

m_hConvClient=0;<br />

UnobtainHsz(m_hszTopicName);<br />

m_hszTopicName=0;<br />

((CDDECliDlg *)(AfxGetApp()->m_pMainWnd))->m_btnConnect.SetWindowText("Connect");<br />

Printf("Disconnected by the server.\r\n");<br />

return (HDDEDATA)NULL;<br />

}<br />

The server side:<br />

……<br />

case XTYP_DISCONNECT:<br />

{<br />

if(m_hConvServer == hConv)<br />

{<br />

m_hConvServer=0;<br />

Printf("Disconnected by the client.\r\n");<br />

}<br />

return (HDDEDATA)FALSE;<br />

464

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

Saved successfully!

Ooh no, something went wrong!