Serving the Quantitative Finance Community

 
User avatar
Samsaveel
Topic Author
Posts: 33
Joined: April 20th, 2008, 5:47 am

char type in c++ dll and VBA

March 9th, 2014, 1:17 pm

hi, I created a dll and want to use it from excel using vba.one of the arguments to the c++ function is option type 'P','C' of type charmy c++ function returns a vector containing the option value,standard deviation,and standard error.c++ function:vector <double> _stdcall MonteCarloSobol(double p, double k, double vol, double rate, double div, double T, char type1, long N, long M)Vba definition:Declare Function MonteCarloSobol Lib _"C:\C++Development\Monte Carlo DLL\Debug\MonteCarlo_dll.dll" _(ByVal p As Double, ByVal k As Double, ByVal vol As Double, ByVal rate As Double, _ByVal div As Double, ByVal T As Double,byval type1 as string , ByVal N As Long, ByVal M As Long) As Doublenot sure the string in the vba works ,any ideas ? also how to get a vector output in VBa as well ?
 
User avatar
tags
Posts: 3162
Joined: February 21st, 2010, 12:58 pm

char type in c++ dll and VBA

March 9th, 2014, 6:55 pm

No one else has replied so far.My 2-cent.I think you need to use the BSTR type.You can find a bit of help in this document available at datasimfinancial.com .And take care as there may be subtleties depending the version of Excel you use (the XLOPER stuff). (?)It is explained in great length in Financial Applications using Excel Add-in Development in C / C++, 2nd Edition.
Last edited by tags on March 8th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20250
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

char type in c++ dll and VBA

March 9th, 2014, 8:14 pm

In addition to Ed's post, this nice summary by @Avt I found to be useful. Maybe alsoIn general, string, char* and stuff is tricky in Windows because so many types to choose from. Maybe use int instead of char?(?)It's not difficult, but you need to know the steps. edit: corrected link to Avt article.
Last edited by Cuchulainn on March 8th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
dd3
Posts: 4
Joined: June 8th, 2010, 9:02 am

char type in c++ dll and VBA

March 9th, 2014, 10:01 pm

I'd be very surprised if you could pass a std::vector from C++ to VBA. You might be better off by passing a Type, these are easily convertible to POD C++ structs.
Last edited by dd3 on March 8th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20250
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

char type in c++ dll and VBA

March 10th, 2014, 8:06 am

QuoteOriginally posted by: dd3I'd be very surprised if you could pass a std::vector from C++ to VBA. You might be better off by passing a Type, these are easily convertible to POD C++ structs.What about SAFEARRAYThey can be passed by the dispatch interface. AFAIR the C function expects a pointer that you fill with a std::vector in the Main.
Last edited by Cuchulainn on March 9th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
dd3
Posts: 4
Joined: June 8th, 2010, 9:02 am

char type in c++ dll and VBA

March 10th, 2014, 9:22 am

Never dealt with it. The Type way is how you'd call Windows APIs from VB. Would the SAFEARRAY way limit you to VC++?
 
User avatar
Cuchulainn
Posts: 20250
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

char type in c++ dll and VBA

March 10th, 2014, 9:28 am

QuoteOriginally posted by: dd3Never dealt with it. The Type way is how you'd call Windows APIs from VB. Would the SAFEARRAY way limit you to VC++?Offhand, I reckon so. But it *should* work with languages that support COM. Not sure what if the language uses Pascal calling convention.As you say, using Type in VBA and C structs and arrays of those guys is fine (and probably a bit more portable).
Last edited by Cuchulainn on March 9th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
Samsaveel
Topic Author
Posts: 33
Joined: April 20th, 2008, 5:47 am

char type in c++ dll and VBA

March 10th, 2014, 1:58 pm

I changed the data type of type1 to integer.the issue now is that when type1 i== -1(indicating a put ) , I get 0 as output.below is the code snippet if (type1 == 1) val = 0.5*(max(0, S1 - strike) ; else if (type1 == -1) val = 0.5*(max(0, strike - S1) ; sum1 = sum1 + val; } val = exp(-rate*T)*sum1/M; return val; }I am looking into the Dalton Book, its quite involved
 
User avatar
Cuchulainn
Posts: 20250
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

char type in c++ dll and VBA

March 10th, 2014, 3:36 pm

What is the value of sum1 when type ==1? Did you look at Avt's note?
Last edited by Cuchulainn on March 9th, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
Samsaveel
Topic Author
Posts: 33
Joined: April 20th, 2008, 5:47 am

char type in c++ dll and VBA

March 10th, 2014, 6:48 pm

Thanks cuch ,will check Avt's .double sum1 = 0.0 ;