Simple C++/VBA Dll Question

Hello Gods of Coding !

2 questions :

Can a C++ Dll being called from Excel VBA return final output to Excel but also produce a Text/CSV file which may contain results of intermediate calculations.

Also, can a C++ dll return the result as an array.

Any simple code examples would be much appreciated..

Can a C++ Dll being called from Excel VBA return final output to Excel but also produce a Text/CSV file which may contain results of intermediate calculations.

That would depend on the output the DLL is returning, a SOCKET struct would be of very little use to a spreadsheet cell. If you're already working with VB then don't dumb down your application by using a text delimited format, the Application.Excell namespace is much easier to work with then you might think.

Also, can a C++ dll return the result as an array.

Array's don't have "results", please rephrase.
Hi CompGeek - Here is the example. Runs fine as a console app and writes to a a file but not in Dll :( HELP !

Also, I meant return an array..sorry for the confusion

#include <direct.h> //_getcwd
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

void Print2DVec(std::vector<std::vector<double>>const& v,string op_label,string myfilename, string myfilepath);

extern "C" {
double _stdcall funct(long int x, double *v)
//[x is row/col] and [*v vector is 3x3 passed from excel = {123,456,789}
{

double temp = 0.00;

string myfilename = "testfile.csv"; //Input File Name

string myfilepath = _getcwd(NULL, 512); //Get File Path

vector<vector<double>> myVect(x, vector<double>(x));

for (long int i = 0; i < x; i++)
{
for (long int j = 0; j < x; j++)
{
myVect[j][i] = v[i + j * x];
temp = temp + myVect[j][i];
}
}

Print2DVec(myVect, "My 2D Vector", myfilename, myfilepath);//Function Call to print the input vector to a CSV (test)

return temp; //Return sum of input Vector values

}

}

//Function to Print Vector to a file with i/p filename, label to the same folder as the VS Project
void Print2DVec(std::vector<std::vector<double>>const& myVect, string op_label,
string myfilename, string myfilepath)
{

ofstream output; //define o/p stream

output.open((myfilepath+"\\"+myfilename).c_str(), std::ios_base::app); //string to char conversion

//print label
output<<(myfilepath+"\\"+myfilename)<<endl;

//print path
output<<op_label<<endl;

//loop through vector
for (size_t i = 0; i < myVect.size(); i++)
{
for (size_t j = 0; j< myVect.size();j++)
{
output<<myVect[i][j] <<",";
}
output<<std::endl;
}
}
I want to start off by saying that you're going about this in a very odd way, if your interest in this is functional as opposed to academic then I would recommend redesigning your solution from the ground up. This is something we can help with if you can provide the necessary details.

The biggest flaw with your approach here is that Excel locks the files that it works with. From what I gather you're trying to load this DLL into Excel and have it call your function so that the DLL can then write data back to that same file. Excel has ways of accomplishing what you are trying to do, but unfortunately they are no where near as straight forward as this. These methods aren't some kind of magic, and they are all as well documented as you would expect from MSDN (which is very well by the way). However you may not be ready for the learning curve. So I would again encourage you to consider letting us help you redesign your solution.
The purpose is entirely academic. Basically the Excel is calling the DLL to return only one value and that is the sum of all values of the Matrix.

However, there maybe a case when the calculations may not be so simple and may have many intermediate calculations that we may want to review (or not) to validate the final result but not necessarily return to the excel sheet.

For that purpose I was curious if I can produce a text/csv or whatever via a dll being called from vba.



Hope this makes sense :/
Last edited on
Okay I got it to work..actually wasn't that complicated :D
Topic archived. No new replies allowed.