Serving the Quantitative Finance Community

 
User avatar
tibbar
Posts: 10
Joined: November 7th, 2005, 9:21 pm

VBA & C++ for beginners!!!

November 9th, 2005, 12:33 am

i would recommend easyXll as a framework to build XLL's in. Much easier than using the XL97 SDK.See: http://www.erik-n.net/easyxll/
 
User avatar
mj
Posts: 12
Joined: December 20th, 2001, 12:32 pm

VBA & C++ for beginners!!!

November 9th, 2005, 1:13 am

how does it compare to xlw ?
 
User avatar
tibbar
Posts: 10
Joined: November 7th, 2005, 9:21 pm

VBA & C++ for beginners!!!

November 9th, 2005, 7:31 am

i found easyXLL much simpler to setup. Basically all you need to do to create a UDF is:1) add the following to the top your .cpp file:#include <easyxll/include/types.h>#include <easyxll/include/Registrar.h>#include <easyxll/include/Bridge.h>#include <easyxll/include/Controller.h>using namespace easyxll;2) Create your UDF (here's an example of one I used)LPOPER WINAPI _r_x(LPOPER xlAge, LPOPER xlName){ static OPER xlResult; std::string servicetableName; ServiceTableListEntry* pServiceTable; double x; double r_x; try { servicetableName = BridgeValue<std::string>::get(xlName); x = BridgeValue<double>::get(xlAge); // checks on inputs if(Bridge::isVector(xlAge)) throw "ERROR: Age should be single value"; if(servicetableName.empty() == true) throw "ERROR: No table name specified"; if(x < 0) throw "ERROR: Negative age!"; // lookup table name in global array pServiceTable = g_ServiceTableList.FindTable(&servicetableName); if(pServiceTable == NULL) throw "ERROR: Could not find table"; // now we can return the requested r_x r_x = pServiceTable->pTable->r_x->l(x); BridgeValue<double>::make(&xlResult, r_x); return &xlResult; } catch(char* error) { std::string r(error); BridgeValue<std::string>::make(&xlResult, r); return &xlResult; } catch(...) { Bridge::makeError(&xlResult, Bridge::ErrRef); return &xlResult; }}3) Create a function description for the function wizard to see:Registrar rxreg( "_r_x", // "C" Function name "ActXL", // Function wizard category "Age,TableName", // Arguments help "", // Full path to a .HLP or .CHM help file 0, // Entry point number in the help file "Returns a retirement entry r_x of the service table", // Help line for this function false, // Should this function be hidden from the Function Wizard 2, // Number of arguments "Age required", // Help line for first argument "Service table name");4) Finally you must add the function name to a .def file:LIBRARYEXPORTS_r_xThat's it. It should compile to both managed or unmanaged code using c++.net (/clr flag).
 
User avatar
hammerbacher
Posts: 1
Joined: August 1st, 2005, 8:55 pm

VBA & C++ for beginners!!!

November 9th, 2005, 9:53 pm

mj,sorry for the previous question, i was planning on looking at your files later today and wanted to put that question out there to make sure i wouldn't miss anything stupid. anyways: when i build the example .xll, i can call functions from excel. but what if i want to use these functions in vba? i've done this before by making a file called "foobar.def" which says "LIBRARY foobar", and then "EXPORTS", followed by "xlfoofunc @1", or whatever number. this also requires a file "foobar_xl.h" where i put some WINAPI wrapper functions. then in VBA i have to say "Declare Function foofunc Lib "foobar.xll" (foo as Double) as Double" or something similar. do i have to do all that with the xlw framework to be able to call my functions in VBA?thanks,jeff
 
User avatar
tibbar
Posts: 10
Joined: November 7th, 2005, 9:21 pm

VBA & C++ for beginners!!!

November 9th, 2005, 10:47 pm

once you have open the XLL file that exports the UDF's, you should find they are available in the function wizard from Excel. If so, then you can directly use them in VBA - e.g. just do:x = myFoo(4)directly in VBA. No need to setup the declare function bits.You should also see the XLL UDF's in the object browser from VBA.
 
User avatar
mj
Posts: 12
Joined: December 20th, 2001, 12:32 pm

VBA & C++ for beginners!!!

November 9th, 2005, 11:28 pm

i don't use VBA so can't comment easyXLL doesn't seem any easier than xlw to me
 
User avatar
tibbar
Posts: 10
Joined: November 7th, 2005, 9:21 pm

VBA & C++ for beginners!!!

November 9th, 2005, 11:37 pm

its a matter of personal preference, neither are particularly difficult to use.has anyone tried managedxll (http://www.stochastix.de/solutions/exce ... ll/latest/)?It looks pretty neat to me, but I've not ever played around with it. Is it worth buying?
 
User avatar
hammerbacher
Posts: 1
Joined: August 1st, 2005, 8:55 pm

VBA & C++ for beginners!!!

November 10th, 2005, 2:58 pm

tibbar--a function that shows up in the function wizard is not necessarily callable from VBA. this is a problem with a fairly complicated solution right now. have you tried it with mj's xlw solution? i'd be quite happy if what you said were true.mj--no VBA at all? not even for quick-and-dirty testing stuff in excel? what do you use for quick turnaround stuff?
 
User avatar
tibbar
Posts: 10
Joined: November 7th, 2005, 9:21 pm

VBA & C++ for beginners!!!

November 10th, 2005, 4:05 pm

ok ok, so you need to be slightly more inventive than i hinted.so here's my exported xll function: LPOPER WINAPI _a_x(LPOPER xlAge, LPOPER xlInterest, LPOPER xlPaymentType, LPOPER xlName, LPOPER xlLifeTableType)just how difficult is it to use in vba, well pretty easy, if you remember excel4 macros:Now in VBA i simply do:Sub test()x = ExecuteExcel4Macro(Evaluate("_a_x(65,0.05,0,""member_l_m"",0)"))End Subeasy eh?
Last edited by tibbar on November 9th, 2005, 11:00 pm, edited 1 time in total.
 
User avatar
hammerbacher
Posts: 1
Joined: August 1st, 2005, 8:55 pm

VBA & C++ for beginners!!!

November 10th, 2005, 5:15 pm

tibbar--now we're talkin. i like that. never seen that "executeexel4macro" function. worked like a charm.
 
User avatar
bhutes
Posts: 4
Joined: May 26th, 2005, 12:08 pm

VBA & C++ for beginners!!!

July 24th, 2006, 8:00 pm

A beginner's C++ question:In my header files, I haveTemplate<T> XYZ() {......}and:Class ABC() {}such that XYZ <ABC> would make sense when used in the main program.In my main program, I have a std::string myString="ABC";I would like to use:XYZ<myString>but this does not work, and I am unable to find a way to convert myString (which is equal to "ABC") to a valid typename accepted by template XYZ.What could I do?(In actual practice, myString is not hard-coded in the program, but comes as an argument specified at runtime. The template XYZ could accept a lot of class parameters other than ABC too i.e. XYZ<ABC> and XYZ<DEF> both are valid, but the ABC or DEF is provided only at runtime).
 
User avatar
pascalroca
Posts: 0
Joined: October 19th, 2005, 6:45 pm

VBA & C++ for beginners!!!

July 26th, 2006, 10:15 am

c++ linkgood luck !you will find there some reviews on books books Review
 
User avatar
pascalroca
Posts: 0
Joined: October 19th, 2005, 6:45 pm

VBA & C++ for beginners!!!

July 26th, 2006, 10:55 am

template <typename T> class toto{ T a;public : toto(); ~toto();};template <typename T> toto<T>::toto(){}template <typename T> toto<T>::~toto(){}template<> class toto<std::string>{ std::string a;public : toto(){ a="ABC";} // bad hardcoded :-) ~toto(){};};int _tmain(int argc, _TCHAR* argv[]){ toto<std::string> hurk; return 0;}
 
User avatar
pascalroca
Posts: 0
Joined: October 19th, 2005, 6:45 pm

VBA & C++ for beginners!!!

July 26th, 2006, 10:59 am

class StringBuilder{public: template<typename T> void Append( const T & t ) { this->oss << t; } std::string GetString() const { return this->oss.str(); }private: std:stringstream oss;};template <typename T> class toto{T a;public : toto();~toto();};template <typename T> toto<T>::toto(){}template <typename T> toto<T>::~toto(){}int _tmain(int argc, _TCHAR* argv[]){ toto<std::string> hurk; // hard code toto<StringBuilder> jgjg; // good return 0;}
 
User avatar
pascalroca
Posts: 0
Joined: October 19th, 2005, 6:45 pm

VBA & C++ for beginners!!!

July 26th, 2006, 10:59 am

then ?