I need to get data from the DDE server. Somewhere on the 16,000 cycle aisle, I get cracks. Visual Studio 2010 Express, windows xp. There is no such problem on Delphi. What am I doing wrong?

UPD: I think the problem with memory?

#include "stdafx.h" #include <windows.h> #include "ddeml.h" #include <stdio.h> #include <string.h> #include <windows.h> #include <fstream> using namespace std; #pragma comment(lib, "ws2_32.lib") HDDEDATA CALLBACK DdeCallback( UINT uType, // Transaction type. UINT uFmt, // Clipboard data format. HCONV hconv, // Handle to the conversation. HSZ hsz1, // Handle to a string. HSZ hsz2, // Handle to a string. HDDEDATA hdata // Handle to a global memory object. ) { return 0; } char* DDERequest(DWORD idInst, HCONV hConv, char* szItem) { char *szResult = new char[255]; HSZ hszItem = DdeCreateStringHandle(idInst, szItem, 0); HDDEDATA hData = DdeClientTransaction(NULL,0,hConv,hszItem,CF_TEXT, XTYP_REQUEST,999999999 , NULL); if (hData==NULL) { printf("Request failed: %s\n", szItem); } else { DdeGetData(hData, (unsigned char *)szResult, 255, 0); // printf("%s%s\n", sDesc, szResult); } return szResult; } int __cdecl main(void) { //-----------------------------------------GET DDE DATA----------------------------------------- char szApp[] = "EXCEL"; char szTopic[] = "sheet"; int i; //DDE Initialization DWORD idInst=0; 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); }else {printf("DDE Initialization: 0x%04x\n", iReturn);} //DDE Connect to Server using given AppName and topic. HSZ hszApp, hszTopic; 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"); }else{printf("DDE Connection Succeful.\n");} //Execute commands/requests specific to the DDE Server. for (int a=0;a<999999999;a++){ printf("NUMBER ----------------------------%d-------------------------------\n",a); printf("rezult : %s\n",DDERequest(idInst, hConv, "R1C1")); } //DDE Disconnect and Uninitialize. DdeDisconnect(hConv); DdeUninitialize(idInst); system("pause"); return 1; } 

    1 answer 1

     #include "stdafx.h" #include <windows.h> #include "ddeml.h" #include <stdio.h> #include <string.h> #include <fstream> using namespace std; #pragma comment(lib, "ws2_32.lib") HDDEDATA CALLBACK DdeCallback( UINT uType, // Transaction type. UINT uFmt, // Clipboard data format. HCONV hconv, // Handle to the conversation. HSZ hsz1, // Handle to a string. HSZ hsz2, // Handle to a string. HDDEDATA hdata // Handle to a global memory object. ) { return 0; } char *szResult = new char[255]; char* DDERequest(DWORD idInst, HCONV hConv, char* szItem) { //char *szResult = new char[255]; 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 { DdeGetData(hData, (unsigned char *)szResult, 255, 0); // printf("%s%s\n", sDesc, szResult); } return szResult; } int __cdecl main(void) { //-----------------------------------------GET DDE DATA----------------------------------------- char szApp[] = "GEHCS"; char szTopic[] = "DATAGR"; int i; //DDE Initialization DWORD idInst=0; 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); }else {printf("DDE Initialization: 0x%04x\n", iReturn);} //DDE Connect to Server using given AppName and topic. HSZ hszApp, hszTopic; 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"); }else{printf("DDE Connection Succeful.\n");} //Execute commands/requests specific to the DDE Server. for (int a=0;a<9999999999;a++){ DWORD idInst=0; UINT iReturn; iReturn = DdeInitialize(&idInst, (PFNCALLBACK)DdeCallback, APPCLASS_STANDARD | APPCMD_CLIENTONLY, 0 ); hszApp = DdeCreateStringHandle(idInst, szApp, 0); hszTopic = DdeCreateStringHandle(idInst, szTopic, 0); hConv = DdeConnect(idInst, hszApp, hszTopic, NULL); DdeFreeStringHandle(idInst, hszApp); DdeFreeStringHandle(idInst, hszTopic); printf("NUMBER ----------------------------%d-------------------------------\n",a); printf("rezult : %s\n",DDERequest(idInst, hConv, "%I2")); DdeDisconnect(hConv); DdeUninitialize(idInst); } //DDE Disconnect and Uninitialize. system("pause"); return 1; }