Serving the Quantitative Finance Community

 
User avatar
seb82
Topic Author
Posts: 0
Joined: October 24th, 2008, 2:16 pm

how to create an excel add-in from c++ via a .dll

October 27th, 2008, 9:33 am

Hello there.I am having huge problems in creating an Excel add-in using my C++ code from Visual Studio 6. Actually I have a completely running C++ program and I can create a .dll and an Excel add-in. However, when calling the function from within Excel (the function is known to Excel) it does not carry out any computations and gives the error message #VALUE. So I think that there is a problem in handing over the actual code to Excel/VBA.My code looks as follows (simplified testing version, as the real problem I am looking to solve is a MC simulation):#include "stdafx.h"_declspec(dllexport) double _stdcall fkt(double w);BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ){ return TRUE;}_declspec(dllexport) double _stdcall fkt(double w){ return w*w;}Since this code compiles I then use the resulting .dll and create an Excel add-in from VBA using the linePublic Declare Function fkt Lib "Test3.dll" (ByVal W As Double) As DoubleDoes anybody see any obvious mistakes in there? I am running out of ideas of what I could have done wrong.Thanks for your help in advance. I appreciate it very much!
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

how to create an excel add-in from c++ via a .dll

October 27th, 2008, 11:10 am

Are you at JP Morgan, I though they were the only bank using a version of Visual Studio that is older than many quants ?As it happens I'm 47 today, so I remember Visual Studio 6, that was 4 whole versions ago.you need dumpbin.exeThis will tell you if you've compiled it properly. dumpbin /EXPORTS test3.dllFirstly you've probably hit name mangling, you need "extern "C", else the command above will show that the name is not what you expected it to be.Also you need to put it in a .DEF file as part of the linking. The 1980s documentation you have access to says you don't need a DEF file.That's a lie.Also you need to be straight in your paths, else the DLL simply won't be found.You also need to send me your CV.Any shit firm that makes you use a stupid old fuckwitted version of VC++ is doomed.
Last edited by DominicConnor on October 26th, 2008, 11:00 pm, edited 1 time in total.
 
User avatar
seb82
Topic Author
Posts: 0
Joined: October 24th, 2008, 2:16 pm

how to create an excel add-in from c++ via a .dll

October 27th, 2008, 12:17 pm

I am a PhD student, which explains the use of the antiquated software...I think the main problem in my case is the .def file. Where do I have to put this file in my Visual C++ project? And of which type is this file? Is it a text file? In the menu I cannot choose .def file. And then how can I tell the linker that there is such a file? The linker does not give any hints or warnings that such a file is missing.Thanks for your help!
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

how to create an excel add-in from c++ via a .dll

October 27th, 2008, 12:35 pm

You need to go to the project window and add a .DEF fileIt's also known as a module definition file.It only needs to contain:LIBRARY DCFC.DLLEXPORTSMyFun @1MyOtherFun @2etc.Also as a PhD student you should be aware that Microsoft will give you a full version of a the current Visual Studio for free?As it happens I write occasional tech articles and you will find out more about the free MS dev tools for Windows atThe Register
 
User avatar
seb82
Topic Author
Posts: 0
Joined: October 24th, 2008, 2:16 pm

how to create an excel add-in from c++ via a .dll

October 27th, 2008, 4:37 pm

Thanks a lot. I have finally figured out how to get a program running.But that raises another question. In my Monte Carlo simulation I am encoutering run-time errors every time I start the program. Then I found out that it can be avoided if I replace the pointers in my C++ program by standard arrays. However, this will prevent me from being able to have a variable number of sample paths I can choose as an input variable in Excel since the array size must be known at compile time.Is there a way to circumvent this problem and to still be able to use pointers? Does anybody know about this?
 
User avatar
daveangel
Posts: 5
Joined: October 20th, 2003, 4:05 pm

how to create an excel add-in from c++ via a .dll

October 27th, 2008, 9:30 pm

yes its quite simple allocate on the heap using the "new" operator.double mc(long n){double *vec = new double[n];use vecdelete [] vec;}
Last edited by daveangel on October 27th, 2008, 11:00 pm, edited 1 time in total.
knowledge comes, wisdom lingers
 
User avatar
katastrofa
Posts: 7954
Joined: August 16th, 2007, 5:36 am
Location: Event Horizon

how to create an excel add-in from c++ via a .dll

October 27th, 2008, 10:12 pm

Oh God."Use vec" throws an exception and you get a memory leak, if you're not extra careful.Use at least std::auto_ptr, or just std::vector<double>.
 
User avatar
daveangel
Posts: 5
Joined: October 20th, 2003, 4:05 pm

how to create an excel add-in from c++ via a .dll

October 28th, 2008, 5:51 am

yes - good point
knowledge comes, wisdom lingers
 
User avatar
seb82
Topic Author
Posts: 0
Joined: October 24th, 2008, 2:16 pm

how to create an excel add-in from c++ via a .dll

October 28th, 2008, 3:51 pm

Thanks a lot for your hints. But I haven't understood, why simple pointers likedouble* pointer = new double[N]//source codedelete pointer;can pose that many problems. Have I understood it correctly that using the STL vector class can help overcome those problems?And then I am experiencing another problem. I my MC simulation I am using the random number generator rand(). I am not initializing it via srand() in order to make sure that I always obtain the same sequence of random numbers for testing purposes. And hence I should always obtain the same option value. As a standard C++ console application everything works fine. However, after writing an Excel add-in, I always get different results depsite the fact of using the same set of parameters. My source code is as follows:double normal_dist(double expectation, double variance){ double help1 = (double)rand()/RAND_MAX; double help2 = (double)rand()/RAND_MAX; if (help1==0) help1+=0.00001; return expectation + sqrt(variance)*sqrt(-2*log(help1))*cos(2*PI*help2);}extern "C" _declspec(dllexport) double _stdcall BS_MC(double r, double X, double S0, double T, double sigma, int N){ //double* payoff = new double[N]; double payoff[M]; double value = 0; for(int i=0; i<M; i++){ payoff = exp(-r*T)*MAX(S0*exp((r-0.5*sigma*sigma)*T+normal_dist(0,sigma*sigma*T))-X,0.0); value+=payoff; } value/=M; //delete payoff; return value;}double MAX(double a, double b){ if(a>b) return a; else return b;}Does anyone see any major mistake I make? Or is there an explanation of whether or not calls from within Excel can generate different sequences of random numbers and if so, how this can be avoided? Thanks a lot in advance! At the moment all that seems really weird to me.
 
User avatar
bojan
Posts: 0
Joined: August 8th, 2008, 5:35 am

how to create an excel add-in from c++ via a .dll

October 28th, 2008, 4:06 pm

delete[] is needed to delete object allocated like new double[10], i.e., with array notation.But std::vector<double> is much better because the object is automatically deleted, look up "exceptions in C++".For your random number trouble see http://www.bnikolic.co.uk/blog/cpp-rr-rand.html
 
User avatar
jjyu
Posts: 0
Joined: September 28th, 2005, 12:34 pm

how to create an excel add-in from c++ via a .dll

October 28th, 2008, 4:12 pm

You said in your previous message that you observed pointer would work while fixed size array failed. However, you like to be able to predefine the number of paths (the size of the pointer) when using pointer.katastrofa and daveangel recommended to create the array of pointers (on the heap) and subsequently use auto pointer to take care of memory deallocation issues.Without spending time to do it myself, I think you should be able to solve the problem when following issues are clear:1. array of pointers are pretty much the same as "array". When declaring the array, you need to know the size in advance and the variable could be an automatic one, it gets out of scope after the function returned, on the other hand, a pointer representation allows you to define the "size" on the fly, or dynamically. They look slightly different.2. pointer can point to object of any type, this raises the need of "de-allocating", especially when the object itself contains pointers or memory allocation issue. Auto pointer now comes in to play, it's supposed to make the deallocation transparent to the user.Hope this helps.
 
User avatar
daveangel
Posts: 5
Joined: October 20th, 2003, 4:05 pm

how to create an excel add-in from c++ via a .dll

October 28th, 2008, 6:58 pm

some general advice on your code.1. I would change the normal_dist() function so that it returns a normal deviate (0,1). this will save some arithmetic. 2. make MAX an inline function 3. I would change the main function so that the discounting only takes place once so you don't make repeated calls to exp() which is very expensive4. some error handling would be nice for example make sure that N is a positive integer.5. strictly speaking "payoff" does not need to be an array at all - it doesn't seem to be returned or used anywhere
Last edited by daveangel on October 27th, 2008, 11:00 pm, edited 1 time in total.
knowledge comes, wisdom lingers
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

how to create an excel add-in from c++ via a .dll

October 28th, 2008, 7:05 pm

QuoteOriginally posted by: katastrofaOh God."Use vec" throws an exception and you get a memory leak, if you're not extra careful.Use at least std::auto_ptr, or just std::vector<double>.Indeed.auto_ptr is a bit old hat (and not safe). Better is boost smart pointers.And for arrays, there no need to create raw pointers. STL::vector does the job much better than I could ever do. Why make life difficult for yourself?? QuoteIs there a way to circumvent this problem and to still be able to use pointers? Use pointers only when you know them inside out. Besides, they can be replaced as already mentioned.
Last edited by Cuchulainn on October 27th, 2008, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

how to create an excel add-in from c++ via a .dll

October 28th, 2008, 7:22 pm

Does your code run?? the payoff is an array, right? You assign a pointer to MAX(...). BTW this MAX function is partly loop-independent so take it outside the loop?? (an optimising compiler might do this automagically).
Last edited by Cuchulainn on October 27th, 2008, 11:00 pm, edited 1 time in total.
 
User avatar
mj
Posts: 12
Joined: December 20th, 2001, 12:32 pm

how to create an excel add-in from c++ via a .dll

October 28th, 2008, 9:49 pm

to echo everything that's already been said:Don't dynamically allocate memory (i.e. don't use new) unless you really have to. Use STL container classes such as vector instead. The latest editions of visual studio are free for everyone in express editions. So learn VC9.If your objective is simply to interface EXCEL with C++, why go via VBA? use xlw instead.