// DDEClientCons.cpp : Example program to demonstrate the use of DDEClient.dll. // To use with version 0.02 of DDEClient.dll // // IMPORTANT: You need to open Microsoft Word in order to // let this program work properly and access to write to C:\. // If you can't write to C:\ search for it in the file and // change it whatever path you want. // // // Author: Robert Matovinovic // Version: 0.01 // Date: 04.10.2006 // #pragma once #include #include #include "conio.h" #include "windows.h" // only used to display a sample error message box #include "DDEClient.h" // procedure for error handling, prints either the error message to console // or if bDCErrorExport is FALSE displays error in a message box void ErrorOccured() { if (bDCErrorExport) cprintf("\r\nError: %s\r\n",DCLastError()); else DCLastError(); } // procedure prints bytes formatted as characters to console void conBytePrint(WORD wConvNo) { BYTE* pData = DCDA[wConvNo]->pData; // pointer to DDE data for (DWORD i = 0; i < DCDA[wConvNo]->dwLen; i++) cprintf("%c",pData[i]); cprintf("\r\n"); } int _tmain(int argc, _TCHAR* argv[]) { // conversation number WORD wConvNo; char szType[] = "request"; char szData[21]; char szItem[255]; char szService[] = "WinWord"; char szTopic[255]; char szFormat[] = "CF_TEXT"; DWORD dwTimeout = 0; char szAccess[] = "byte"; sprintf(szTopic, "system"); sprintf(szItem, "SysItems"); sprintf(szData, "Data from DDE client"); bDCErrorExport = true; // print program info cprintf("This is DDEClientCons a demo for DDEClient.dll\r\nMake sure that Microsoft Word is running, that you have write permission on C:\\.\r\nThe program will perform several requests, open a new file, save it as C:\\test.doc and poke some data to it.\r\nPress Enter to continue.\r\n"); getchar(); // print version of dll cprintf("%s\r\n",DCVersion()); // Initialize DDE if(!DCInit()) {ErrorOccured();} else { // The following if-block shows a complete conversation with a // single transaction. You therefore do not need to free any memory // explicitly. // connect to server if (!DCConnect(&wConvNo,szService, szTopic)) ErrorOccured(); else { // do synchronous request transaction, wait max. 1000 ms, // return data as string if(!DCTransaction(wConvNo,"request",szItem,szData,szFormat,1000, "string")) ErrorOccured(); else { // output data to console if transaction complete // DCDA[wConvNo]->pszData is the pointer to the data string cprintf("\r\nDCTransaction synchronous string:\r\n%s\r\n",DCDA[wConvNo]->pszData); } // end conversation if(!DCDisconnect(wConvNo)) ErrorOccured(); } // The following if-block shows a conversation with several // transactions. You therefore do need to free memory // explicitly. // connect to server if (!DCConnect(&wConvNo,szService, szTopic)) ErrorOccured(); else { // do asynchronous request transaction, return data as bytes if(!DCTransaction(wConvNo,"request","SysItems",szData,szFormat,0, "byte")) ErrorOccured(); else { // wait for transaction to complete and output data to console if(!DCAsynchTransactionCompleted(wConvNo,DCDA[wConvNo]->dwTransID,true)) ErrorOccured(); else { cprintf("\r\nDCTransaction asynchronous byte:\r\n"); conBytePrint(wConvNo); // Free memory DCFreeDdeMem(wConvNo); } } // do synchronous request transaction, wait max. 1000 ms, // return data as string if(!DCRequestString(wConvNo,szItem,1000)) ErrorOccured(); else { // output data to console cprintf("\r\nDCRequestString synchronous:\r\n%s\r\n",DCDA[wConvNo]->pszData); // Free memory DCFreeDdeMem(wConvNo); } // do asynchronous request transaction, return data as string if(!DCRequestString(wConvNo,"Topics",0)) ErrorOccured(); else { // wait for transaction to complete and output data to console if(!DCAsynchTransactionCompleted(wConvNo,DCDA[wConvNo]->dwTransID,true)) ErrorOccured(); else { cprintf("\r\nDCRequestString asynchronous:\r\n%s\r\n",DCDA[wConvNo]->pszData); DCFreeDdeMem(wConvNo); } } // do synchronous request transaction, wait max. 1000 ms, // return data as bytes // Note: The topic "SysI" does not exist, therefore an error // will occur!!! if(!DCRequest(wConvNo,"SysI",szFormat,1000)) { cprintf("\r\nIntended erronous request. Topic SysI doesn't exists. The original error message is following:\r\n",DCDA[wConvNo]->pszData); ErrorOccured(); } else { // output data to console cprintf("\r\nDCRequest synchronous with intended error:\r\n"); conBytePrint(wConvNo); DCFreeDdeMem(wConvNo); } // end conversation if(!DCDisconnect(wConvNo)) ErrorOccured(); } // The following if-block shows an execute transaction. // There is no need to free any memory explicitly. cprintf("\r\nFor the following execute and poke commands it is not possible to check whether they succeeded or not.\r\n"); if (!DCConnect(&wConvNo,szService, szTopic)) ErrorOccured(); else { // asynchronous execute transaction to open a new file if(!DCTransaction(wConvNo,"execute","[filenew]",NULL,szFormat,0,"")) ErrorOccured(); else { if(!DCAsynchTransactionCompleted(wConvNo,DCDA[wConvNo]->dwTransID,true)) ErrorOccured(); else cprintf("\r\nExecute [filenew] sent.\r\n"); } // asynchronous execute transaction to save file as C:\test.doc if(!DCTransaction(wConvNo,"execute","[filesaveas \"c:\\test.doc\"]",NULL,szFormat,0,"")) ErrorOccured(); else { if(!DCAsynchTransactionCompleted(wConvNo,DCDA[wConvNo]->dwTransID,true)) ErrorOccured(); else cprintf("\r\nExecute [filesaveas] sent.\r\n"); } if(!DCDisconnect(wConvNo)) ErrorOccured(); } // The following if-block shows a poke transaction. // There is no need to free any memory explicitly. // open a conversation with the file C:\test.doc if (!DCConnect(&wConvNo,szService, "c:\\test.doc")) ErrorOccured(); else { // asynchronous poke transaction writes if(!DCTransaction(wConvNo,"poke","\\EndOfDoc","Hello this is DDEClient sample!",szFormat,0,"")) ErrorOccured(); else { if(!DCAsynchTransactionCompleted(wConvNo,DCDA[wConvNo]->dwTransID,true)) ErrorOccured(); else cprintf("\r\nPoke sent. Look in your word document for results\r\n"); } if(!DCDisconnect(wConvNo)) ErrorOccured(); } if(!DCUninit()) {ErrorOccured();} } DCFinalize(); cprintf("\r\nIf everything went fine and you want to repeat the program don't forget to delete C:\\test.doc, otherwise the program won't work properly.\r\n\r\nPress Enter to exit..."); getchar(); return 0; }