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

hconv<br />

hszTopic<br />

hszAppName<br />

hData<br />

lData1<br />

lData2<br />

Not used<br />

Handle of the topic name, must be obtained by calling ::DdeCreateStringHandle()<br />

Handle of service name, must be obtained by calling ::DdeCreateStringHandle()<br />

Not used<br />

Can be neglected<br />

Can be neglected<br />

In the sample of the previous section, a service name “Server” is registered on the server side. To let<br />

the connection be set up between the server and the client, we also need to prepare a topic name that will be<br />

used by both sides. In the sample 15.2\DDE\Server, string “Topic” is used as the topic name, whose string<br />

handle is obtained in function CMainFrame::Hszize() and freed in function CMainFrame::UnHszize().<br />

The client should also obtain a string handle for the topic name and use it to make connection. The<br />

server will receive the handles of service name and topic name together with message XTYP_CONNECT. Upon<br />

receiving this message, the server needs to check if the service name and topic name requested by the client<br />

are supported by itself.<br />

To compare two strings by their handles, we can call function ::DdeCmpStringHandles(…), which will<br />

compare two DDE strings. When calling this function, we need to pass the string handles to its two<br />

parameters. The function will return -1, 0 or 1 indicating if the first string is less than, equal to, or greater<br />

than the second string. The following is the format of this function:<br />

int ::DdeCmpStringHandles(HSZ hsz1, HSZ hsz2);<br />

Now it is clear what the server should do after receiving XTYP_CONNECT message: comparing<br />

hszAppName with its registered service name, and comparing hszTopic with its supported topic names by<br />

calling function ::DdeCmpStringHandles(…). If the comparisons are successful, the callback function<br />

should return TRUE, this indicates the connection request is accepted. Otherwise it should return FALSE,<br />

in which case the connection request is rejected. The following code fragment shows how this is<br />

implemented in sample 15.2\DDE\Server:<br />

……<br />

……<br />

HDDEDATA CALLBACK CMainFrame::DdeCallback<br />

(<br />

UINT wType,<br />

UINT wFormat,<br />

HCONV hConv,<br />

HSZ hszTopic,<br />

HSZ hszAppName,<br />

HDDEDATA hData,<br />

DWORD lData1,<br />

DWORD lData2<br />

)<br />

{<br />

switch(wType)<br />

{<br />

}<br />

case XTYP_CONNECT:<br />

{<br />

if<br />

(<br />

!::DdeCmpStringHandles(hszTopic, m_hszTopicName) &&<br />

!::DdeCmpStringHandles(hszAppName, m_hszServiceName)<br />

)<br />

{<br />

Printf("Connect!\r\n");<br />

return (HDDEDATA)TRUE;<br />

}<br />

else<br />

{<br />

Printf("Connection refused, do not support this topic!\r\n");<br />

return (HDDEDATA)FALSE;<br />

}<br />

}<br />

462

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

Saved successfully!

Ooh no, something went wrong!