Home
Forums
New posts
Search forums
Online Courses
2022 Rankings
2022 MFE Programs Rankings Methodology
Reviews
Latest reviews
Search resources
Tracker
What's new
New posts
New media
New media comments
New resources
New profile posts
Latest activity
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
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!
Home
Forums
Quant discussion
Computing
Integrate C++ program into VBA
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Ilya W" data-source="post: 2117" data-attributes="member: 26"><p>Hi Bridget,</p><p>My name is Ilya I'm a part time student on the program since september 2004. I'm working on the same problem now, and can show you simple example how to integrate c++ dll file and vba excel. I'm working with c++.net and vba embedded in excel 2003. Here is simple method how to import one dimensional array from VBA to C++ and process it there. At least you will have some idea how to work with it.</p><p> </p><p>C++ CODE:</p><p> </p><p>1. open empty windows 32 project</p><p>2. insert new .h file and call it MyDll.h</p><p>3. insert .DEF file which links your c++ functions with VBA functions MyDll.Def</p><p>4. insert .cpp file MyDll.cpp</p><p> </p><p>you can send array from VBA to C++ using the first element of array on VB side or using SAFEARRAY structure, here is simple example</p><p> </p><p>also in VB and C++ Double have the same size, but integer in VB is short in C++</p><p> </p><p>[code]//prototype in MyDll.h</p><p>#include <windows.h></p><p>short __stdcall Double_Array(double *arr, short size);</p><p> </p><p>//MyDll.Def</p><p>EXPORT</p><p>Double_Array</p><p> </p><p>//MyDll.cpp simple code to change all elements to 3</p><p>short __stdcall Double_Array(double *arr, short size){</p><p>int i;</p><p>for (i=0; i<size; i++)</p><p>arr[I]=3;</p><p>return(0);</p><p>}[/code]</p><p> </p><p><em>VBA CODE: </em></p><p>[code=vba]</p><p>Option Explicit</p><p> </p><p>'path to library</p><p>Private Declare Function Double_Array _</p><p>Lib "C:your_path\MyStDll.dll" (ByRef arr As Double, ByVal size As Integer) As Integer</p><p> </p><p>Private Sub Command1_Click()</p><p>Dim arr(1 To 5) as Double, lbsum As Double, lasum As Double</p><p>Dim arr_size as Integer, k as Integer, i As Integer</p><p>arr(1) = 2</p><p>arr(2) = 2</p><p>arr(3) = 2</p><p>arr(4) = 2</p><p>arr(5) = 2</p><p> </p><p>'Sum before call to c++</p><p>For i = 1 To UBound(arr)</p><p>lbsum = lbsum + arr(i)</p><p>Next i</p><p> </p><p>arr_size = CInt(UBound(arr()))</p><p>k = Double_Array(arr(1), arr_size)</p><p> </p><p>'Sum after call to c++</p><p>For i = 1 To UBound(arr)</p><p>lasum = lasum + arr(i)</p><p>Next i</p><p>MsgBox "Sum of the elements before= " & lbsum & " " & _</p><p>"Sum of the elements after= " & lasum</p><p>End Sub[/code]</p><p><em>good luck</em></p></blockquote><p></p>
[QUOTE="Ilya W, post: 2117, member: 26"] Hi Bridget, My name is Ilya I'm a part time student on the program since september 2004. I'm working on the same problem now, and can show you simple example how to integrate c++ dll file and vba excel. I'm working with c++.net and vba embedded in excel 2003. Here is simple method how to import one dimensional array from VBA to C++ and process it there. At least you will have some idea how to work with it. C++ CODE: 1. open empty windows 32 project 2. insert new .h file and call it MyDll.h 3. insert .DEF file which links your c++ functions with VBA functions MyDll.Def 4. insert .cpp file MyDll.cpp you can send array from VBA to C++ using the first element of array on VB side or using SAFEARRAY structure, here is simple example also in VB and C++ Double have the same size, but integer in VB is short in C++ [code]//prototype in MyDll.h #include <windows.h> short __stdcall Double_Array(double *arr, short size); //MyDll.Def EXPORT Double_Array //MyDll.cpp simple code to change all elements to 3 short __stdcall Double_Array(double *arr, short size){ int i; for (i=0; i<size; i++) arr[I]=3; return(0); }[/code] [I]VBA CODE: [/I] [code=vba] Option Explicit 'path to library Private Declare Function Double_Array _ Lib "C:your_path\MyStDll.dll" (ByRef arr As Double, ByVal size As Integer) As Integer Private Sub Command1_Click() Dim arr(1 To 5) as Double, lbsum As Double, lasum As Double Dim arr_size as Integer, k as Integer, i As Integer arr(1) = 2 arr(2) = 2 arr(3) = 2 arr(4) = 2 arr(5) = 2 'Sum before call to c++ For i = 1 To UBound(arr) lbsum = lbsum + arr(i) Next i arr_size = CInt(UBound(arr())) k = Double_Array(arr(1), arr_size) 'Sum after call to c++ For i = 1 To UBound(arr) lasum = lasum + arr(i) Next i MsgBox "Sum of the elements before= " & lbsum & " " & _ "Sum of the elements after= " & lasum End Sub[/code] [I]good luck[/I] [/QUOTE]
Verification
Post reply
Home
Forums
Quant discussion
Computing
Integrate C++ program into VBA
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top