#include "stdafx.h"
#include "windows.h"
#include "ddeml.h"
#include "stdio.h"
#define NITEM 9 //定义ITEM的数量;
const char *pszItem[NITEM]={"Item1","Item2","Item3","Item4","Item5","Item6","Item7","Item8","Item9"}; //SERVER ITEM名称字符串数组;
DWORD idInst=0;
HSZ hszApp, hszTopic;
char szApp[] = "myapp";
char szTopic[] = "mytopic";
char szItem1[] = "R1C1"; char szDesc1[] = "A1 Contains: ";
char szItem2[] = "R2C1"; char szDesc2[] = "A2 Contains: ";
char szItem3[] = "R333"; char szDesc3[] = "R333 Contains: ";
HSZ hszItem[NITEM]; //Server ITEM字符串句柄;
HDDEDATA CALLBACK DdeCallback(UINT wType,UINT wFmt,HCONV hConv,HSZ Topic,HSZ Item,
HDDEDATA hData,DWORD lData1,DWORD lData2)
{
char tmp[255];
if(wType==XTYP_ADVDATA && wFmt==CF_TEXT)
{
for(int I=0;Iif(Item==hszItem[I])
{
DdeGetData(hData,(PBYTE)tmp,255,0);//取得数据;
printf("%s - %s\n", pszItem[I],tmp);
break;
}
}
return(0);
}
void DDEExecute(DWORD idInst, HCONV hConv, char* szCommand)
{
HDDEDATA hData = DdeCreateDataHandle(idInst, (LPBYTE)szCommand,lstrlen(szCommand)+1, 0, NULL, CF_TEXT, 0);
if (hData==NULL) {
printf("Command failed: %s\n", szCommand);
}
else {
DdeClientTransaction((LPBYTE)hData, 0xFFFFFFFF, hConv, 0L, 0,XTYP_EXECUTE, TIMEOUT_ASYNC, NULL);
}
}
void DDERequest(DWORD idInst, HCONV hConv, char* szItem, char* sDesc)
{
HSZ hszItem = DdeCreateStringHandle(idInst, szItem, 0);
HDDEDATA hData = DdeClientTransaction(NULL,0,hConv,hszItem,CF_TEXT,XTYP_REQUEST,5000 , NULL);
if (hData==NULL)
{
printf("Request failed: %s\n", szItem);
}
else
{
char szResult[255];
DdeGetData(hData, (unsigned char *)szResult, 255, 0);
printf("%s=%s\n", sDesc, szResult);
}
//改进,增加的两行
DdeFreeStringHandle(idInst,hszItem);
DdeFreeDataHandle(hData); //关键
}
void DDEPoke(DWORD idInst, HCONV hConv, char* szItem, char* szData)
{
HSZ hszItem = DdeCreateStringHandle(idInst, szItem, 0);
DdeClientTransaction((LPBYTE)szData, (DWORD)(lstrlen(szData)+1),hConv, hszItem, CF_TEXT,XTYP_POKE, 3000, NULL);
DdeFreeStringHandle(idInst, hszItem);
}
int main(int argc, char* argv[])
{
char szCmd1[] = "mycommand";
char szItem3[] = "R3C1"; char szData3[] = "Data from DDE Client";
char szCmd2[] = "mycommand2";
//DDE Initialization
UINT iReturn;
iReturn = DdeInitialize(&idInst, (PFNCALLBACK)DdeCallback, APPCLASS_STANDARD | APPCMD_CLIENTONLY, 0 );
if (iReturn!=DMLERR_NO_ERROR)
{
printf("DDE Initialization Failed: 0x%04x\n", iReturn);
Sleep(1500);
return 0;
}
//Start DDE Server and wait for it to become idle.
/*HINSTANCE hRet = ShellExecute(0, "open", szTopic, 0, 0, SW_SHOWNORMAL);
if ((int)hRet < 33)
{
printf("Unable to Start DDE Server: 0x%04x\n", hRet);
Sleep(1500); DdeUninitialize(idInst);
return 0;
}
Sleep(1000);
*/
//DDE Connect to Server using given AppName and topic.
HCONV hConv;
hszApp = DdeCreateStringHandle(idInst, szApp, 0);
hszTopic = DdeCreateStringHandle(idInst, szTopic, 0);
hConv = DdeConnect(idInst, hszApp, hszTopic, NULL);
DdeFreeStringHandle(idInst, hszApp);
DdeFreeStringHandle(idInst, hszTopic);
if (hConv == NULL)
{
printf("DDE Connection Failed.\n");
Sleep(1500); DdeUninitialize(idInst);
return 0;
}
for(int I=0;I//Execute commands/requests specific to the DDE Server.
//DDEExecute(idInst, hConv, szCmd1);
DDERequest(idInst, hConv, szItem1, szDesc1);
DDERequest(idInst, hConv, szItem2, szDesc2);
char *item="dddd";
char *itemDesc="你好";
DDERequest(idInst, hConv, item, itemDesc);
DDEPoke(idInst, hConv, szItem3, szData3);
DDEExecute(idInst, hConv, szCmd2);
for(int I=0;Iwhile( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//DDE Disconnect and Uninitialize.
DdeDisconnect(hConv);
DdeUninitialize(idInst);
Sleep(3000);
return 1;
}