Serving the Quantitative Finance Community

 
User avatar
SteveG
Topic Author
Posts: 0
Joined: September 26th, 2002, 2:43 pm

Excel Formulas with no input Params

December 9th, 2002, 11:55 am

Does anyone know how to make Excel 97/2K behave correctly when you have a formula with no parameters.What I have is a function which returns the current NT user, so I don't want to pass any parameters into the function, but when you click on the fx button, to get the Paste Function Wizard, and select the "NT User" formula it displays a dialog asking for one parameter, and if any value is entered into this field it fails to return the NT user name.Also does any one know what is happening when you use the formula in an Excel Cell as =NTUSER without the (Parenthesis), as I get a value of 330498049?The function is written aschar* WINAPI NTUSER(void){ DWORD iMaxLen = UNLEN+1; //UNLEN - largest possible NT User name. static char UserName[UNLEN+1]; if (GetUserName(&UserName[0],&iMaxLen)) return UserName; else return "";}and is regestered with Excel like this:-err = Excel(xlfRegister, 0, 20, (LPXLOPER)&xDll, (LPXLOPER)" NTUSER", (LPXLOPER)" C!", (LPXLOPER)" NTUSER", (LPXLOPER)" ", (LPXLOPER)" 1", (LPXLOPER)" NT User", (LPXLOPER)" ", (LPXLOPER)" ", (LPXLOPER)" Returns the Current Logged on User.", (LPXLOPER)" ", (LPXLOPER)" ", (LPXLOPER)" ", (LPXLOPER)" ", (LPXLOPER)" ", (LPXLOPER)" ", (LPXLOPER)" ", (LPXLOPER)" ", (LPXLOPER)" ", (LPXLOPER)" ", ); Cheers for all the help people provide on this forum.Steve.
 
User avatar
RedeR

Excel Formulas with no input Params

December 10th, 2002, 9:13 am

Forget registration when using system calls, use declare instead.However the solution to your problem is here, if you dare go to the "evil empire" as some people call it:http://support.microsoft.com/default.as ... -us;152970&
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

Excel Formulas with no input Params

December 10th, 2002, 9:20 am

As far as I can work it out ,the value you're getting is an address.I think the problem is the way you specify the type signature of the function.I use " C", note the space there.This works for me, rather than "C!" which in my framework just doesn't register the fuction at all.I also tend to use the more trendy char * __stdcall FunctionName (void)I call this from Excel 2K and it works first time.Calling it without parameters, does not cause it to be invoked, Excel is simply not smart enough to recognise nonadic functions. It looks likeyou're getting the same result you would get in C++.Code like y = f(x) ; Invokes the functiony = f ; looks more like copying the address of the function, so I'd assume that you're getting the location within Excel's memory space for the function.DominiConnor
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

Excel Formulas with no input Params

December 10th, 2002, 9:32 am

QuoteHowever the solution to your problem is here, if you dare go to the "evil empire" as some people call it:A good reason to call it the evil empire is that this is not a solution.Not always anyway The code at this location will usually work. Alas, the maximum length of an NT User Name is 255 characters (OK, I'm a geek), and it sets it to 25This works on my system for me, probably for you, but one day some "clever" person will create a user name like "Ha ha ha ! I can crash your software just to assuage my all poweful ego. Fools ! you should not follow the impure path of Q152970"I do this a lot
 
User avatar
RedeR

Excel Formulas with no input Params

December 10th, 2002, 9:58 am

Well I had a look a the no arg question.The value returned does not seem to be an adress, rather an internal code refering to the function.. it is more or less constant between Excel sessions (number changing by 1 or 2 , and depending on the number of add-ins). Maybe a table look up (?)Also the function is not being called.Just adding () is okHowever Excel refuses to give the number for =Sum aso.
 
User avatar
RedeR

Excel Formulas with no input Params

December 10th, 2002, 12:54 pm

The number is in fact the reference used by Excel for user defined function you need to use when you call xcel4(xlUDF,...):http://msdn.microsoft.com/library/defau ... 2.aspshort WINAPI xlUDFExample(void){ XLOPER xMacroName, xMacroRef, xRes; xMacroName.xltype = xltypeStr; xMacroName.val.str = "\05NTUSER"; Excel4(xlfEvaluate, &xMacroRef, 1, (LPXLOPER)&xMacroName); Excel4(xlUDF, &xRes, 1, (LPXLOPER)&xMacroRef); return 1;}The result you get in xMacroRef.val.num is the number you get by evaluating =NTUSERThis paves the way for VTABLES in Excel cells ! Great.
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

Excel Formulas with no input Params

December 10th, 2002, 2:36 pm

Respect.
 
User avatar
SteveG
Topic Author
Posts: 0
Joined: September 26th, 2002, 2:43 pm

Excel Formulas with no input Params

December 11th, 2002, 9:18 am

Ok, I am happy writing the code in VBA land, but does anyone know how I am get the code to fire from the function Paste wizard from under by own Category option and not the default "User Defined", and will this fix my problem with Function Paste wizard then asking me for an input param.I see Microsoft has somehow managed to get around this problem, if you go to the Function Paste (Fx) button and select the Now formula from Date & Time, it displays the dialog which would normally ask for the params, but it does not have any input fields, so it must be possible, just need to work out how.Will try also a message on the Microsoft Excel news group.Thanks for all your help so far.Steve.
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

Excel Formulas with no input Params

December 11th, 2002, 10:17 am

I'm nearly sure you can't do that.I suppose that creating a button on your toolbar isn't good enough for your purposes ?