|
Hello People,
i have a code written in C++ for DDE ... my task is to read data from a EXCEL sheet at a regular interval ... i am able to do it with the following code ... but the problem is when i say A16,B67,Z63 etc.. my application gives me request fail and type some of my code to excel sheet i don't know how ... but yes when i say A1, B9, V2 etc i.e alphabet with a single digit it gives me correct answer .. but also when i hard-code A16 as "R16C1" it gives me correct answer ... the hard-coded string is the form in which excel understands .... so i have a function to convert C16,A1 to R16C3, R1C1 ...
please help me with it ... and also how to read data from different sheets from a single EXCEL ...
hoping for a positive and immediate reply ... !!
Thank you in advance
Code Build: VC++ 6.0
File Name: DDE_ExcelApplication.
Code:-
// cppDdeApplication.cpp : Defines the entry point for the console application.
//
# include "stdafx.h"
# include "windows.h"
# include "ddeml.h"
# include "stdio.h"
# include "string.h"
# include "conio.h"
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.
DWORD dwData1, // Transaction-specific data.
DWORD dwData2) // Transaction-specific data.
{/* This function is called in DdeInitialize() */
return 0;
}
void DDEExecute(DWORD idInst, HCONV hConv, char* szCommand)
{
//HDDEDATA hData = DdeCreateDataHandle(DWORD(idInst), (LPBYTE)szCommand,DWORD(szCommand)+1, 0, NULL, CF_TEXT, 0);
HDDEDATA hData = DdeCreateDataHandle(DWORD(idInst), (LPBYTE)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,LPCTSTR(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);
sDesc = "";
printf("%s%s\n", sDesc, szResult);
}
}
char* cellString(char szItem[])
{
char firstCh = szItem[0];
char strRow[2] = "";
char strCol[3] = "";
int i,j = 0;
int strLength = (int)(strlen(szItem));
for(i=1;i<strLength;)
{
strRow[j] = szItem[i];
i++;
j++;
}
strRow[j] = '\0';
switch(toupper(firstCh))
{
case 'A':strcpy(strCol,"C1");break;
case 'B':strcpy(strCol,"C2");break;
case 'C':strcpy(strCol,"C3");break;
case 'D':strcpy(strCol,"C4");break;
case 'E':strcpy(strCol,"C5");break;
case 'F':strcpy(strCol,"C6");break;
case 'G':strcpy(strCol,"C7");break;
case 'H':strcpy(strCol,"C8");break;
case 'I':strcpy(strCol,"C9");break;
case 'J':strcpy(strCol,"C10");break;
case 'K':strcpy(strCol,"C11");break;
case 'L':strcpy(strCol,"C12");break;
case 'M':strcpy(strCol,"C13");break;
case 'N':strcpy(strCol,"C14");break;
case 'O':strcpy(strCol,"C15");break;
case 'P':strcpy(strCol,"C16");break;
case 'Q':strcpy(strCol,"C17");break;
case 'R':strcpy(strCol,"C18");break;
case 'S':strcpy(strCol,"C19");break;
case 'T':strcpy(strCol,"C20");break;
case 'U':strcpy(strCol,"C21");break;
case 'V':strcpy(strCol,"C22");break;
case 'W':strcpy(strCol,"C23");break;
case 'X':strcpy(strCol,"C24");break;
case 'Y':strcpy(strCol,"C25");break;
case 'Z':strcpy(strCol,"C26");break;
}
szItem = strcpy(szItem,"R");
szItem = strcat(szItem,strRow);
szItem = strcat(szItem,strCol);
return szItem;
}
int DDECall(char szApp[],char szTopic[],char szCmd[],char szInItem[])
{
char szDesc[] = "Requested Cell Value Contains: ";
char* szItem = cellString(szInItem);
//strcpy(szItem,"R13C4");
FILE *fp;
DWORD idInst=0;
UINT iReturn;
iReturn = DdeInitialize((LPDWORD)&idInst, (PFNCALLBACK)DdeCallback,APPCMD_CLIENTONLY, 0L );
/*DdeInitialize() :-
The DdeInitialize function registers an application with the Dynamic Data Exchange Management Library (DDEML).
An application must call this function before calling any other Dynamic Data Exchange Management Library (DDEML)
function.*/
if (iReturn!=DMLERR_NO_ERROR)
{
/*If Error occurs while initialiing DDE then the error code is written to the Log.*/
printf("DDE Initialization Failed: 0x%04x\n", iReturn);
fp = fopen("DdeErrorLog.txt","w+");
fprintf(fp,"\nError Code : DMLERR_DLL_USAGE | DMLERR_INVALIDPARAMETER | DMLERR_SYS_ERROR");
fprintf(fp,"\n\n This Error occurs if Dynamic Data Exchange fails to Initialize ");
fclose(fp);
Sleep(1500);
return 0;
}
HSZ hszApp;
hszApp = DdeCreateStringHandle(DWORD(idInst),LPCTSTR("EXCEL"),CP_WINANSI);
/* DdeCreateStringHandle() :-
The DdeCreateStringHandle function creates a handle that identifies the specified string.
a Dynamic Data Exchange (DDE) client or server application can pass the string handle as
a parameter to other Dynamic Data Exchange Management Library (DDEML) functions.*/
//Start DDE Server and wait for it to become idle.
HINSTANCE hRet = ShellExecute(NULL , "open", "C:\\Test.xls", NULL,NULL, SW_NORMAL);
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.
HSZ hszTopic;
HCONV hConv;
hszApp = DdeCreateStringHandleA(idInst,LPCTSTR("EXCEL"),CP_WINANSI);
hszTopic = DdeCreateStringHandleA(idInst,LPCTSTR(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);
/*The DdeUninitialize function frees all Dynamic Data Exchange Management Library
(DDEML) resources associated with the calling application.*/
return 0;
}
DDEExecute(DWORD(idInst), hConv, szCmd);
/*Sends a command to another application, using dynamic data exchange (DDE).*/
DDERequest(idInst, hConv, szItem, szDesc);
//DDE Disconnect and Uninitialize.
DdeDisconnect(hConv);
DdeUninitialize(idInst);
Sleep(3000000);
return 1;
}
int main()
{
char szApp[] = "EXCEL";
char szTopic[] = "C:\\Test.xls";
char szCmd[] = "[APP.MINIMIZE()]";
char szItem[] = "A1";
DDECall(szApp,szTopic,szCmd,szItem);
getch();
return 0;
}
Thanks & Regards
Vishrant Shah
|
|
|
|
|
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Your problem is in this line: char strCol[3] = "";
It can only hold TWO chars, counting the null terminator. So when you do: case 'J':strcpy(strCol,"C10");break;, you're overwriting the null terminator plus whatever follows strCol in memory.
|
|
|
|
|
How to dial a phone number ???
or in other words, i want to make a call from a PC connected to the phone line via some modem, then send some string.
plz help me ...
|
|
|
|
|
You are confusing rasdial.exe with RasDial() .
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
i want to connect to the telephone the same way the dial-up internet connection works.
in my project, the server is to dial the telephone number of the client, at client side, the phone will check the caller's number, if its server's, then i want the server to communicate with the client thro the telephone line.
|
|
|
|
|
OK - that's the entire assignment. We need to know what have you accomplished in the assignment by far and what exactly do you need help with, at the moment.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
Hi!
I'm developing a VC++ Application. When I close the application(Window), it shows me the following exceptions in the output area:
First-chance exception at 0x7c81eb33 in CharacterAnimation.exe: Microsoft C++ exception: std::ios_base::failure at memory location 0x0012ef30..
First-chance exception at 0x7c81eb33 in CharacterAnimation.exe: Microsoft C++ exception: std::ios_base::failure at memory location 0x0012f034..
What might be the cause of these exceptions? How do I overcome these exceptions?
|
|
|
|
|
Err, you don't "see" it, you use it. Press F5 to start the debugger, then you will be able to put breakpoints in your code using F9. See where the crash occurs and look at the call stack to locate the problem in your sources. Then inspect the different variables before the crash occured.
|
|
|
|
|
No crash occurs. After I manually closed the application,
First-chance exception at 0x7c81eb33 in CharacterAnimation.exe: Microsoft C++ exception: std::ios_base::failure at memory location 0x0012ef30..
First-chance exception at 0x7c81eb33 in CharacterAnimation.exe: Microsoft C++ exception: std::ios_base::failure at memory location 0x0012f034..
These two lines are displayed in the Output area.
|
|
|
|
|
Communication between CSocket and BSD Socket
Hi Friends,
I wanted some help on my project as i have stuck at a place.
Basically i am developing a Client Server Application.
My Server is using a class derived from CSocket.
Wherein i wanted is it possible to communicate a Client which is based on
BSD Socket with a Server which is build using CSocket.
With Rgds
Girish
Software Developer
|
|
|
|