Click here to Skip to main content
15,847,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;                 

Here is the c++ code
MYDLL_API int OpenRawFile(const char* fileName, RawDataHeader &stamp)

   long offset;
   long size;, ios::binary);
   //long myDataStampSize = GetDataStampSize();
   if (!pfile.is_open())
      std::cout << "ERROR";

   pfile.seekg(0, pfile.end);
   length = pfile.tellg();
   pfile.seekg(0, pfile.beg);
   offset = length - stampSize;

      // Start from the end and read
   pfile.seekg(0, pfile.end);
   pfile.seekg(-4, pfile.end);*)&numChannels, sizeof(numChannels));


   pfile.seekg(offset, pfile.beg);*)&stamp.syncpattern, sizeof(int));*)&, sizeof(int));*)&stamp.datastream, sizeof(int));

   for (int i = 0; i < numChannels; i++)
   {*)&stamp.channels[i], sizeof(int));

   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?
Updated 2-Oct-19 4:37am
Afzaal Ahmad Zeeshan 26-Sep-19 12:10pm    
Does this help?
Gerry Schmitz 26-Sep-19 12:12pm
Md23 26-Sep-19 12:43pm    
Why use a binary writer?
Gerry Schmitz 26-Sep-19 16:19pm    

The mixing of String and char-array is the problem. You cant test it, by commenting this fields out.

I would use some char or byte array or when you want to invest some efford in this issue: a _bstr_t.

PS: I read the solution 2 of Dave K. and he is absolutly right. The LONG og is 8 bytes great and so the size doesnt match to the 4 bytes of C++ integer. You need to address that issue too.
Share this answer
Md23 26-Sep-19 12:22pm    
I tried commenting them out but still got the same result
KarstenK 27-Sep-19 7:00am    
Read my added ps-remarks and the solution 2. You need to address it.
Md23 27-Sep-19 15:52pm    
I did that also but still no success
Md23 27-Sep-19 15:52pm    
I tried that but still no success
Ummm...Your structures don't match.

A Long in VB.NET is a 64-bit signed integer (8 bytes) while the corresponding variable in the C++ structure is an int, which is a 32-bit signed integer (4 bytes). If I'm not mistaken, the corresponding 64-bit integer type in C++ should be long long (yes, two of them).
Share this answer
Md23 27-Sep-19 6:52am    
So what you are saying is in the code where ever there is a long the c++ should have a long long? Since a long is 8 bytes in VB.NET and an int is 4 bytes in c++
KarstenK 27-Sep-19 7:00am    
You can use the Integer data type of VB.
Md23 27-Sep-19 7:28am    
Ok. I changed all longs to Integers but still getting the same exception.
Md23 27-Sep-19 7:31am    
Is the vector the problem do you think?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900