Serving the Quantitative Finance Community

 
User avatar
jabs
Topic Author
Posts: 0
Joined: June 12th, 2003, 1:45 pm

Declaring DLL functions(from C++) in VBA

June 12th, 2003, 2:22 pm

Hi All,I have a function in C++ that takes in a variable length array (read in as something[]) as an argument. I have created a dll for the function and would like to call the function from Excel/VBA. My question regards the function declaration statement in VBA.I tried the following:Declare Function something Lib "c:\...." (x as Range) as doubleand that didn't work.I also tried:Declare Function something Lib "c:\...." (Paramarray x() as Variant) as doubleand that didn't work either.What is the right way to do this? ... I would greatly appreciate any help you might offer.Thank you,jabs
 
User avatar
Cppfiend
Posts: 1
Joined: February 24th, 2003, 5:01 pm

Declaring DLL functions(from C++) in VBA

June 12th, 2003, 2:34 pm

DCFC gave an excellent seminar on this last night. If you ask him really nicely then he might put his powerpoint slides online I guess... Too bad he can't put some more of that free beer online as well.
 
User avatar
luizvs
Posts: 0
Joined: May 23rd, 2003, 6:13 pm

Declaring DLL functions(from C++) in VBA

June 12th, 2003, 5:07 pm

Hi JabsAll you have done could work. It depends on how you call the dll funciton you declared.If you useDeclare Function something Lib "c:\...." (Paramarray x() as Variant) as double... you should delcare an argument as Variant array. Somthing like:Dim arg_x() as VariantDim result as doubleresult = something(arg_x)Doing this, you will have no problem with arguments.
 
User avatar
Student

Declaring DLL functions(from C++) in VBA

June 12th, 2003, 9:47 pm

hianother way would be passing the first element of the array by reference(and the size of the array as another argument)Declare Function something Lib "c:\...." (x as double) as double...Dim a(0 to 9) as doubleDim result as doubleresult = something(a(0))referencehttp://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support ... bContent=1 edit: address of the reference should be all in one line
Last edited by Student on June 11th, 2003, 10:00 pm, edited 1 time in total.
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

Declaring DLL functions(from C++) in VBA

June 13th, 2003, 8:35 am

I've uploaded my notes.Any questions, please post in the Software forum.
 
User avatar
jabs
Topic Author
Posts: 0
Joined: June 12th, 2003, 1:45 pm

Declaring DLL functions(from C++) in VBA

June 16th, 2003, 12:22 pm

Many thanks to DCFC, Student, luizvs and Cppfriend for all their help. Student's web link provided a solution that worked well for my particular problem. I will also check out DCFC's notes as he was kind enough to post them.Thanks again,Jabs
 
User avatar
liefje
Posts: 0
Joined: September 26th, 2002, 2:11 pm

Declaring DLL functions(from C++) in VBA

June 19th, 2003, 1:25 pm

HiJust a question, I searched the site for the upload area without results.where can I find this uploadthx in advance
 
User avatar
Student

Declaring DLL functions(from C++) in VBA

June 19th, 2003, 2:53 pm

it's here
 
User avatar
ychen025
Posts: 0
Joined: July 10th, 2003, 6:12 pm

Declaring DLL functions(from C++) in VBA

July 10th, 2003, 6:18 pm

Hi all,This is a really emergency problem. I have been stuck here for three days, and I need to get my job done.I am trying to pass an array of string in VBA/EXCEL to a DLL. However, in my DLL, it's a function accepting parameters of Variant type.So the array is been passed as Variant into the DLL.The problem is it's not been recognized as any type. It's been treated as trash variable.Does any one have any clue about what can I do??Please help....Thanks a lot
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

Declaring DLL functions(from C++) in VBA

July 11th, 2003, 1:13 pm

I take it you've sussed out the MS documentation is maliciously wrong ?Declare Sub Stringer Lib "util.dll" (v As Variant) dim v as variantv = Array("thing", "other thing", "third thing", "not any of the other things")void __stdcall Stringer (VARIANT *v){ CString vbStuff; long index; VARIANT myv; for (index = v->parray->rgsabound->lLbound; index != v->parray->rgsabound->lLbound + v->parray->rgsabound->cElements; index++) { SafeArrayGetElement(v->parray, &index, &myv); vbStuff =myv.bstrVal; TRACE1("%s\n",vbStuff); } }
 
User avatar
Forde
Posts: 1
Joined: November 27th, 2002, 7:45 pm

Declaring DLL functions(from C++) in VBA

August 19th, 2003, 8:40 am

does anyone have a C++ to Excel dll example which uses Crank Nicolsen to price e.g. vanillas/ExoticsI wrote sth a while back to do just this, but ran into memory problems when the size of the discretizations in the mesh became small
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

Declaring DLL functions(from C++) in VBA

August 19th, 2003, 3:06 pm

No I don't have code for this, but it shouldn't be too hard unless your mesh is pathologically small.I suspect you memory problem was more to do with stack than free store. Excel has a limited stack, but you can fix this if you really want to.Although I've not seen your code, I rather feel that your architectiure for C-N is not optimal, if you're making multiple eval calls.This looks like the ideal candidate for a separate process.
 
User avatar
Forde
Posts: 1
Joined: November 27th, 2002, 7:45 pm

Declaring DLL functions(from C++) in VBA

August 19th, 2003, 3:09 pm

thx ... can u recommend a good textbook or website which explains how to fix these kinda problems - I'm better at Maths than I am at fixing memory leakageM
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

Declaring DLL functions(from C++) in VBA

August 20th, 2003, 9:42 am

Do we know it is a memory leak, or that you're allocating a structure too big to fit ?
 
User avatar
Lucetios
Posts: 1
Joined: March 27th, 2003, 6:09 pm

Declaring DLL functions(from C++) in VBA

April 5th, 2005, 4:28 pm

Here is some thing I have done