• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Calling C++ from VBA

Hello everyone,

I would like to call a C++ function from VBA. I've been reading about DLLs and exporting C++ code but I haven't got anywhere. I'm using Microsoft Visual Studio environment.

Does anyone know how to do this? Or, what are the basic steps to achieve this? Or redirect me to an example?

Many thanks
 
There are at least few ways possible. The one I currently work with is VBA->C#->managed C++ wrapper->C++. On other hand if C++ compiled as dll and method you need takes simple type arguments you can try to declare this methods inside you vba code and call it directly. There is also xll way to do the things where you are working with a special xloper structure but unfortunately I'm not an expert in xll topic :)

Here are few links that might be handy:
For the simple Dll and Xll a very good example:
http://financelab.nctu.edu.tw/Paper/Calling C++ Developed DLL.pdf

For C# and managed wrapper try to read this book:
C# in Front Office by Xing Zhou
Chapter 1 and 4

Good Luck
 
Hey Alain, unfortunately here is the case where I can not change the C++ analytic library code which we want to use in add-inns. And I do not know simple ways how to send vectors and maps from VBA code directly to C++ :) Probably there is a way to use xlls but for me wrapper looked as a better approach since I already had an experience there. If you have any better idea gladly will take it into account :)
 

DominiConnor

Quant Headhunter
As soon as I work out how to attach files to Quantnet posts I will put up some lecture notes I did on how to call C++ from VBA.

Provided pointers don't scare you it's not too hard.
 
As soon as I work out how to attach files to Quantnet posts I will put up some lecture notes I did on how to call C++ from VBA.

Thank you Dominic, it will be interesting. Pointers does not scare me at all even though I do not see how we can pass a vector from VBA directly to C++ method if we can not change method signature, but again there are a lot of things that I do not know, so may be there is a way :)
 
Hello everyone,

Many thanks for all your replies! I will investigate the above.

DominiConnor - could you send me the lecture notes you mentioned above?
 
Hey Alain, unfortunately here is the case where I can not change the C++ analytic library code which we want to use in add-inns. And I do not know simple ways how to send vectors and maps from VBA code directly to C++ :) Probably there is a way to use xlls but for me wrapper looked as a better approach since I already had an experience there. If you have any better idea gladly will take it into account :)

You can call exposed C/C++ DLL functions from VBA (but depending on your analytic interface, you may need a C wrapper). Alternatively you can do Excel/VBA -> managed C++ -> your analytic library (i.e. skip the C# layer). Either way, you only need at most one "intermediate" layer between Excel/VBA and your C++ code.
 
Top