Discussion:
returning XLOPER from a function
(too old to reply)
xll_student
2010-02-12 17:06:01 UTC
Permalink
Hi, I am new to XLL development.

I am trying to return XLOPER from the following function:

__declspec(dllexport) XLOPER* __stdcall Z_TEST2(double p_Years)
{
XLOPER arg;
arg.xltype = xltypeNum;
arg.val.num = p_Years + 1;

return &arg;
}

Function return type and arguments are described as "RB!" string.

Can you please take a look at the code and let me know what is wrong
(instead of a number, the call =Z_TEST2(15) returns me #VALUE! error).

Thank you!
lab27
2010-02-12 19:24:14 UTC
Permalink
inline
Post by xll_student
Hi, I am new to XLL development.
 __declspec(dllexport) XLOPER* __stdcall  Z_TEST2(double p_Years)
{
    XLOPER arg;
    arg.xltype = xltypeNum;
    arg.val.num = p_Years + 1;
    return &arg;
}
Function return type and arguments are described as "RB!" string.
Can you please take a look at the code and let me know what is wrong
(instead of a number, the call =Z_TEST2(15) returns me #VALUE! error).
Thank you!
This is more of a C++ issue than an XLL issue.

You're returning the address of a local variable. This is not a valid
thing to do, as the lifetime of arg (an odd name!) does not extend
beyond the scope of Z_TEST2. (You might get lucky. You might not.
Don't depend on it though!)

The usual technique prior to excel 2007 is to make 'arg' (in this
case) static, which would ensure that it had a lifetime that extended
past the scope of Z_TEST2.

Be very careful in excel 2007 if you're marking a function as safe for
multithreaded calculation ($ postfix), as obviously this will not be a
valid thing to do.
http://msdn.microsoft.com/en-us/library/bb687868.aspx describes this.

BTW - if you're not able for some reason to attach a debugger, you
might find liberal use of OutputDebugString and the dbgview tool from
sysinternals to be handy tools while you're practising with this
stuff. ;)

Rgds,

Lee.
unknown
2010-02-27 09:54:42 UTC
Permalink
Hi

If it is the second edition of my book you are waiting for, you will also
find more complete code for returning thread-safe xlopers/xloper12s. It's
not hard to implement and really unleashes the potential of multi-threading
for XLL developers.

I would caution you that there are some bugs in the example code in the
book, so please use it as intended: an example of how things can be done,
rather than a fully QA'd software product. At some point I will post some
updated code somewhere.

Regards

Steve Dalton


"lab27" <***@gmail.com> wrote in message news:ccf927b2-ce3a-4874-a6ec-***@o28g2000yqh.googlegroups.com...
inline
Post by xll_student
Hi, I am new to XLL development.
__declspec(dllexport) XLOPER* __stdcall Z_TEST2(double p_Years)
{
XLOPER arg;
arg.xltype = xltypeNum;
arg.val.num = p_Years + 1;
return &arg;
}
Function return type and arguments are described as "RB!" string.
Can you please take a look at the code and let me know what is wrong
(instead of a number, the call =Z_TEST2(15) returns me #VALUE! error).
Thank you!
This is more of a C++ issue than an XLL issue.

You're returning the address of a local variable. This is not a valid
thing to do, as the lifetime of arg (an odd name!) does not extend
beyond the scope of Z_TEST2. (You might get lucky. You might not.
Don't depend on it though!)

The usual technique prior to excel 2007 is to make 'arg' (in this
case) static, which would ensure that it had a lifetime that extended
past the scope of Z_TEST2.

Be very careful in excel 2007 if you're marking a function as safe for
multithreaded calculation ($ postfix), as obviously this will not be a
valid thing to do.
http://msdn.microsoft.com/en-us/library/bb687868.aspx describes this.

BTW - if you're not able for some reason to attach a debugger, you
might find liberal use of OutputDebugString and the dbgview tool from
sysinternals to be handy tools while you're practising with this
stuff. ;)

Rgds,

Lee.

Loading...