Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Excel add in development in C++ Aplications in finance - Dalton S.

Dalton S. Excel add in development in C++ Aplications in finance - Wiley publishing , 2005. - 425 p.
ISBN 0-470-02469-0
Download (direct link): exceladdindevelopmentincand2005.pdf
Previous << 1 .. 55 56 57 58 59 60 < 61 > 62 63 64 65 66 67 .. 168 >> Next

void cpp_xloper::operator=(char *text) {
Free();
set_to_text(&m_Op, text); m_DLLtoFree = true;
}
How you convert it to a C/C++ data type
The following code example shows how to get at the string pointed to by the xloper. Note that, when making a copy, the code does not assume that a byte-counted string (which might have been created by Excel) is null terminated. This would be an unsafe assumption.
bool coerce_to_string(xloper *p_op, char *&text) {
char *str; xloper ret_val;
text = 0;
if(!p_op)
return false;
Passing Data between Excel and the DLL
ІЗ?
if(p_op->xltype != xltypeStr)
{
// xloper is not a string type, so try to convert it. if(!coerce_xloper(p_op, ret_val, xltypeStr)) return false;
str = ret_val.val.str;
}
else if(!(str = p_op->val.str)) // make a working copy of ptr return false;
int len = str[0];
if((text = (char *)malloc(len +1)) == NULL) // caller to free return false;
if(len)
memcpy(text, str + 1, len);
text[len] =0; // terminate the copy of the string
// Is the string from which the copy was made was created in a call // to coerce_xloper above, then need to free it with a call to xlFree if(p_op->xltype != xltypeStr)
Excel4(xlFree, 0, 1, &ret_val);
return true;
}
Using the cpp_xloper class the conversion would look like this:
// Construct an xltypeStr cpp_xloper. Destructor will clean up memory // when Oper is no longer required. cpp_xloper Oper("Test string");
char *string_copy = (char *)Oper;
// ... after using the result, free the string memory free(string_copy);
The code for the overloaded conversion operator (char *) is:
cpp_xloper::operator char *(void) {
char *p;
if(coerce_to_string(&m_Op, p)) return p;
return NULL;
і
What the memory considerations are
When Excel passes you an xltypeStr it is best to avoid doing anything other than reading it. If you need to modify it, make a copy. When you are allocating memory for
138
Excel Add-in Development in C/C++
strings to be returned to Excel, the returned pointer is forgotten about by Excel once it has copied out the text. Obviously, associated memory cannot be freed by the DLL before returning from the function. This makes returning dynamically allocated strings to Excel as char * a bad idea. Returning an xltypeStr xloper gives you the ability to instruct Excel to call back into your DLL once it has finished. Then you can release the memory. (This topic is covered in section 7.4 Getting Excel to call back the DLL to free DLL-allocated memory on page 166.)
The following example code would leak memory every time it was called with a valid value of i.
char * stdcall bad_string_example(short i)
{
if(i < 1 || i > 26) return NULL; char *rtn_string = (char *)malloc(i + 1); for(char *p = rtn_string; i; *p++ = 'A' + --i); return rtn_string;
}
Where an xloper points to a static byte-counted string, there is nothing to worry about.
How you can avoid usins it
Declare functions as taking null-terminated char * arguments and/or returning char *. Excel will do the necessary conversions, but, beware: returning dynamically allocated strings in this way will result in memory leaks.
6.8.4 Excel Boolean: xltypeBool
When you will encounter it
This xloper type is used by Excel for all Boolean (true or false) values passed from worksheets to a DLL. It is also returned by a number of the C API functions.
When you need to create it
A number of Excel’s own functions take Boolean arguments, often to trigger non-default behaviour. When calling them from within the DLL using the C API this data type should be used. (Excel will attempt to convert numeric xltypeNum or xltypeInt arguments to true or false values.) If you want your worksheet function to evaluate to TRUE or FALSE then you have no choice but to use this type.
How you create an instance of it
The code to populate an xloper of this type is:
void set_to_bool(xloper *p_op, bool b) {
if(!p_op) return; p_op->xltype = xltypeBool; p_op->val._bool = (b ? 1 : 0);
}
Passing Data between Excel and the DLL
139
Using the cpp_xloper class, creation can look like any of these:
bool x, y, z;
//...
cpp_xloper Oper1(x); // creates an xltypeBool xloper, value = x cpp_xloper Oper2 = y; // creates an xltypeBool xloper, value = y cpp_xloper Oper3; // creates an xloper of undefined type
// Change the type of Oper3 to xltypeBool, value = z, using the // overloaded operator =
Oper3 = z;
// Create xltypeBool=z using copy constructor cpp_xloper Oper4 = Oper3;
The code for the xltypeBool constructor is:
cpp_xloper::cpp_xloper(bool b) {
Clear();
set_to_bool(&m_Op, b);
}
The code for the overloaded conversion operator ‘=’ is:
void cpp_xloper::operator=(bool b) {
Free();
set_to_bool(&m_Op, b);
}
How you convert it to a C/C++ data type
The xloper, being a C structure, does not know about the C++ bool type. Its value is represented within the xloper as integer 1 (TRUE) or 0 (FALSE).
The following code example shows how to access (or convert, if not an xltypeBool) the value of the xloper:
Previous << 1 .. 55 56 57 58 59 60 < 61 > 62 63 64 65 66 67 .. 168 >> Next