I need to pass a structure from VB.NET program to C++ program in order to modify the members of the structure.
I had the DLL set up and working in my VB.Net program before the need to modify the structure but I needed to.
Here is how I define the DLL in VB.NET
What I have tried:
<DllImport("ConvertRaw.dll", CallingConvention:=CallingConvention.Cdecl)>
Public Shared Function OpenRawFile(<MarshalAs(UnmanagedType.LPStr)> x As String, ByRef CurrentStamp As RawDataHeader) As Integer
End Function
Here is the structure in VB.NET
Public Structure RawDataHeader
Public syncpattern As Integer
Public runnumber As As Integer
Public muxpitch As Integer
Public ThisChannel() As Integer
End Structure
Here is the structure in c++
typedef struct {
int syncpattern;
int run;
int datastream;
std::vector<int> channels;
}RawDataHeader;
Here is the c++ code
MYDLL_API int OpenRawFile(const char* fileName, RawDataHeader &stamp)
{
long offset;
long size;
pfile.open(fileName, ios::binary);
if (!pfile.is_open())
{
std::cout << "ERROR";
}
pfile.seekg(0, pfile.end);
length = pfile.tellg();
pfile.seekg(0, pfile.beg);
offset = length - stampSize;
pfile.seekg(0, pfile.end);
pfile.seekg(-4, pfile.end);
pfile.read((char*)&numChannels, sizeof(numChannels));
stamp.channels.resize(numChannels);
pfile.seekg(offset, pfile.beg);
pfile.read((char*)&stamp.syncpattern, sizeof(int));
pfile.read((char*)&stamp.run, sizeof(int));
pfile.read((char*)&stamp.datastream, sizeof(int));
for (int i = 0; i < numChannels; i++)
{
pfile.read((char*)&stamp.channels[i], sizeof(int));
}
pfile.close();
return 0;
}
It produces an exception:
Exception thrown at 0x0F068B47 (ConvertRaw.dll) in ConsoleApp3.exe: 0xC0000005: Access violation reading location 0x0000000C.
I figured this has something to do with the vector. I ReDim in VB.Net in order to set the size of channels. What am I missing?