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

Client Implementation<br />

Sample 15.2\DDE\Client is implemented as a dialog based application using Application Wizard.<br />

Similar to the server, here an edit control is included in the dialog template that will be used to display<br />

DDE activities. The DDE initialization procedure is implemented in function<br />

CDDEDialog::OnInitDialog(). There is also another edit box and a button labeled “Connect” in the dialog<br />

box. When the user clicks this button, the application will retrieve the string from this edit box, use it as the<br />

topic name and call function ::DdeConnect(…) to connect to the server:<br />

……<br />

void CDDECliDlg::OnButtonConnect()<br />

{<br />

if(m_hConvClient == 0)<br />

{<br />

UpdateData();<br />

if(m_szTopic.IsEmpty())<br />

{<br />

AfxMessageBox("Please input a topic name!");<br />

return;<br />

}<br />

m_hszTopicName=ObtainHsz(m_szTopic);<br />

m_hConvClient=::DdeConnect<br />

(<br />

m_dwInst, m_hszServiceName, m_hszTopicName, (PCONVCONTEXT)NULL<br />

);<br />

if(m_hConvClient == FALSE)<br />

{<br />

UnobtainHsz(m_hszTopicName);<br />

Printf("Unable to connect server!\r\n");<br />

}<br />

else Printf("Connected to server!\r\n");<br />

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

}<br />

}<br />

The value returned by function ::DdeConnect(…) is a handle used for conversation. Every time the<br />

client want to make a transaction to the server, it must present this handle. By using the handle, the server<br />

knows with whom it is talking with.<br />

In case the connection is not successful, function ::DdeConnect(…) will return a FALSE value.<br />

Confirm Connection<br />

On the server side, if the connection is successful, it will further receive an XTYP_CONNECT_CONFIRM<br />

message from the client. In this case, apart from hszTopic and hszAppName parameters, hconv is also used<br />

to provide the conversation handle that should be used by the server to make further conversation with the<br />

client. The following code segment shows how this message is processed on the server side:<br />

……<br />

……<br />

case XTYP_CONNECT_CONFIRM:<br />

{<br />

m_hConvServer=hConv;<br />

Printf("Handle saved for conversation!\r\n");<br />

return (HDDEDATA)NULL;<br />

}<br />

Variable m_hConvServer is a static variable declared in class CMainFrame and is initialized to NULL in<br />

the constructor.<br />

DDE Disconnection<br />

After the connection is set up, the client and server can proceed to initiate various types of transactions.<br />

We will show this in later sections. After all the transactions are finished, or if one side wants to exit, the<br />

463

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

Saved successfully!

Ooh no, something went wrong!