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

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 .. 71 72 73 74 75 76 < 77 > 78 79 80 81 82 83 .. 168 >> Next

8.3 THE Excel4v() C API FUNCTION
The syntax for Excel4v() is:
int stdcall Excel4v(int xlfn, xloper *RetVal, int count,
xloper *opers[]);
which returns the same values as Excel4().
Table 8.4 Excel4v() arguments
Argument Meaning Comments
int xlfn A number corresponding to a function or command recognised by Excel as part of the C API. Must be one of the predefined constants defined in the SDK header file xlcall .h.
xloper *RetVal A pointer to an xloper that will contain the return value of the function xlfn if Excel4v ( ) was able to call it. If Excel4v () was unable to call the function, the contents of this are unchanged.
If a return value is not required by the caller, NULL (zero) can be passed. Excel allocates memory for certain return types. It is the responsibility of the caller to know when and how to tell Excel to free this memory. (See xlFree and xlbitXLFree.)
int count The number of arguments to xlfn being passed in this call to Excel4v(). As with Excel4 () the maximum value is 30.
xloper *opers[] An array, of at least count elements, of pointers to xlopers containing the arguments for xlfn.
Accessing Excel Functionality Using the C API
The following example simply provides a worksheet interface to Excel4v() allowing the function number and the arguments that are appropriate for that function to be passed in directly from the sheet. This can be an extremely useful tool but also one to be used with great care. This section outlines some of the things this enables you to do, but first here’s the code with comments that explain what is going on.
xloper * stdcall XL4(int xlfn, xloper *arg0, xloper *arg1,
xloper *arg2, xloper *arg3, xloper *arg4,
xloper *arg5, xloper *arg6, xloper *arg7,
xloper *arg8, xloper *arg9, xloper *arg10,
xloper *arg11, xloper *arg12, xloper *arg13,
xloper *arg14, xloper *arg15, xloper *arg16,
xloper *arg17, xloper *arg18)
xloper *arg_array[19]; static xloper ret_xloper;
// Fill in array of pointers to the xloper arguments ready for the
// call to Excel4v()
arg_array[0] = arg0; arg_array[1] = arg1; arg_array[2] = arg2; arg_array[3] = arg3; arg_array[4] = arg4; arg_array[5] = arg5; arg_array[6] = arg6; arg_array[7] = arg7; arg_array[8] = arg8; arg_array[9] = arg9; arg_array[10] = arg10; arg_array[11] = arg11; arg_array[12] = arg12; arg_array[13] = arg13; arg_array[14] = arg14; arg_array[15] = arg15; arg_array[16] = arg16; arg_array[17] = arg17; arg_array[18] = arg18;
// Find the last non-missing argument for(int i = 19; --i >= 0;)
if(arg_array[i]->xltype l= xltypeMissing) break;
// Call the function
int retval = Excel4v(xlfn, &ret_xloper, i + 1, arg_array);
if(retval l= xlretSuccess)
// If the call to Excel4v() failed, return a string explaining why // and tell Excel to call back into the DLL to free the memory // about to be allocated for the return string.
ret_xloper.xltype = xltypeStr I xlbitDLLFree; ret_xloper.val.str = new_xlstring(Excel4_err_msg(retval));
// Tell Excel to free up memory that it might have allocated for
Excel Add-in Development in C/C++
// the return value.
ret_xloper.xltype |= xlbitXLFree;
return &ret_xloper;
The function Excel4_err_msg() simply returns a string with an appropriate error message should the call to Excel4v() fail, and is listed below. The function new_xlstring() creates a byte-counted string from this.
char *Excel4_err_msg(int err_num) {
switch(err num) { case xlretAbort: return "XL4 macro halted";
case xlretlnvXlfn: return "XL4 invalid function number
case xlretlnvCount: return "XL4 invalid number of args"
case xlretlnvXloper: return "XL4 invalid oper structure"
case xlretStackOvfl: return "XL4 stack overflow";
case xlretUncalced: return "XL4 uncalced cell";
case xlretFailed: return "XL4 command failed";
default: return NULL
The function XL4() takes 20 arguments (one for the C API function code, and up to 19 function arguments). The Excel worksheet limit for any function is 30 arguments, but the means by which functions are registered (see section 8.5 below) imposes this limit on exported XLL functions.
The C API was designed to be called from DLL functions that have themselves been called by Excel while executing commands, during worksheet recalculations or during one of the Add-in Manager’s calls to one of the xlAuto- functions. DLL routines can be called in other ways too: the DllMain() function is called by the operating system; VB can call exported DLL functions that have been declared within the VB module; the DLL can set up operating system call-backs, for example, at regular timed intervals; the DLL can create background threads.
Excel is not always ready to receive calls to the Excel4() or Excel4v() functions. The following table summarises when you can and cannot call these functions safely.
Accessing Excel Functionality Using the C API
Table 8.5 When it is safe to call the C API
Previous << 1 .. 71 72 73 74 75 76 < 77 > 78 79 80 81 82 83 .. 168 >> Next