So I have been scratching my head over this for a while. I have built a C++ dll to use in a VB.net project. The C++ source code is shown below.
C++
#include "stdafx.h"
#include "mydll.h"
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <fstream>
extern "C" __declspec(dllexport) void __cdecl ExtractVolumeDataC(std::string path, std::string fileName, bool chkVol, std::string txtKeyName)
{
std::string line;
std::vector<std::vector<std::string>> ValuesCSV;
std::replace(path.begin(), path.end(), '\\', '/'); //replace backslash with forward slash
std::ifstream in(path + fileName);
while (std::getline(in, line)) {
std::string phrase;
std::vector<std::string> row;
std::stringstream ss(line);
while (std::getline(ss, phrase, ',')) {
row.push_back(std::move(phrase));
}
ValuesCSV.push_back(std::move(row));
}
}
The code i am using in VB.net is the following
VB.net
Public Class Form1
<Runtime.InteropServices.DllImport("mydll.dll")> _
Public Shared Sub ExtractVolumeDataC(ByVal path As String, ByVal fileName As String, ByVal txtKeyName As String)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ExtractVolumeDataC("C:\\users\me\\documents\\", "VOL.CSV", "01100872")
End Sub
End Class
One observation I made is that I don't get this error with primitives, but including STL elements like string
and vector
. I get the error. I am sorry if this a dumb question I haven't looked at a C++ code in like 15 years.
VB.net doesn't pass strings as c++ std::string
, ByVal
Strings are passed as pointers to chars (i.e. old C style strings). You can use LPCSTR
as the parameter type for this. In your case this would be:
extern "C" __declspec(dllexport) void __cdecl ExtractVolumeDataC(LPCSTR path, LPCSTR fileName, bool chkVol, LPCSTR txtKeyName)
See This Microsoft support page for more info.