Interfacing C++ with Excel by XLOPER type

Hello, i'm a beginner of C++ and i want to interface C++ and Excel, in this way: i want to write a C++ function that takes a string of an Excel file (XLL format), and verifies if this string is equal to an extablished word, for example, "dog". If the Excel cell contains the word "dog", the C++ function returns a double value equal to 1, if the cell contains a word different from "dog", the function returns another double value, for example 2. I decided to call this function "acquire" People that know C++ better than me has explained to me, that in this case i must use XLOPER format, that is a structure with multiple types. The program is ok, and works correctly if i write "dog" in a cell of Excel sheet for example cell A1, then in another cell, for example A2, i write "=acquire(A1)" and in cell A2, appears me number 1. I tried to write a code. This compiles correctly, but if i call acquire function, i don't see the 1 value but, instead, the program stops and give error (in cell A2 appears #VALUE!). The code i wrote is the following. Can someone help me and tell me where i'm wrong??

EXPORT_EXTERN_C xloper *acquire(xloper *xargs)
{
char *r;
char *s;
r = new char[50];
s = new char[50];
int i;
double a, b;
double x[2];
x[0] = 1;
x[1] = 2;
s = "dog";

xloper *m;
m = new xloper[1];
m->xltype = xltypeMulti;

m->val.array.lparray = new xloper[1];
m->val.array.lparray[0].xltype = xltypeNum;

i=0;
while((xargs->val.array.lparray[i].val.str!='\0')&&(i<50))
{
xargs->val.array.lparray[i].xltype = xltypeStr;
r = xargs->val.array.lparray[i].val.str;
i++;
}
a=strlen(r);
b=strlen(s);

if(a!=b)
{m->val.array.lparray[0].val.num = x[0];}

else
{
if(strcmp(r, s)!=0)
{m->val.array.lparray[0].val.num = x[0];}
else
{m->val.array.lparray[0].val.num = x[1];}
}

m->val.array.lparray[0].xltype |= xlbitDLLFree;
m->xltype |= xlbitDLLFree;

delete[] r;
delete[] s;

return m;
}
Last edited on
Topic archived. No new replies allowed.