Click here to Skip to main content
14,330,663 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a structure of arrays that are fairly big. I keep getting the exception "Exception thrown(vcruntime140d.dll Access violation writing location" This happens on the description member.

What I have tried:

   <DllImport("ConvertRaw.dll", CallingConvention:=CallingConvention.Cdecl)>
   Public Function OpenRawFile(<MarshalAs(UnmanagedType.LPStr)> x As String, ByRef CurrentStamp As RawDataHeader) As Integer
   End Function


<StructLayout(LayoutKind.Sequential)>
   Public Structure RawDataHeader
      Public numChannels As Integer
      Public syncpattern As Integer
      Public runnumber As Integer
      Public muxpitch As Integer
      <MarshalAs(UnmanagedType.ByValArray, SizeConst:=4096)>
      Public channels() As Integer
      <MarshalAs(UnmanagedType.ByValArray, SizeConst:=508)>
      Public description() As Byte
      Public scanlistblocks As Integer

End Structure
      Dim stamp1 As New RawDataHeader

      Dim result As Integer
      Dim sizeOfHeader As Integer = Marshal.SizeOf(GetType(RawDataHeader))
      Dim pHeader As IntPtr = Marshal.AllocHGlobal(sizeOfHeader)
      Dim i As Integer
      Dim x As String

      Try
         result = OpenRawFile("C:\Raw_Data\H12091.raw", pHeader)
         stamp1 = DirectCast(Marshal.PtrToStructure(pHeader, GetType(RawDataHeader)), RawDataHeader)
      Finally
         Marshal.FreeCoTaskMem(pHeader)
      End Try


This is my c++ struct

typedef struct {
   int  numChannels;
   int	syncpattern;						
   int	run;							
   int	datastream;
   int  channels[4096];	
   char description[508]		
   int	scanlistblocks;						

}RawDataHeader;	
Posted
Updated 7-Oct-19 7:54am
v6
Comments
Gerry Schmitz 3-Oct-19 13:03pm
   
Do you have any idea "how much" might be "too large"? Or, start with "one" int and keep adding until it breaks.
Md23 3-Oct-19 13:10pm
   
ScanList() works but then if I add in DataWidths() it does not. I get that error about the structure being too big
Gerry Schmitz 3-Oct-19 13:29pm
   
64KB has always been a "magic number", don't know if it applies to you, but each of your "4096" blocks is taking up 16KB.
Md23 3-Oct-19 13:35pm
   
Unfortunately that's the size they all need to be...

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

According to this thread on the MSDN forums[^], you need to change the parameter to be an IntPtr, allocate the memory to store the structure, and then use PtrToStructure to read the data into your structure.
<DllImport("ConvertRaw.dll", CallingConvention:=CallingConvention.Cdecl)>
Public Function OpenRawFile(<MarshalAs(UnmanagedType.LPStr)> x As String, ByVal CurrentStamp As IntPtr) As Integer
End Function

...

Dim result As Integer
Dim stamp1 As RawDataHeader
Dim sizeOfHeader As Integer = Marshal.SizeOf(GetType(RawDataHeader))
Dim pHeader As IntPtr = Marshal.AllocCoTaskMem(sizeOfHeader)
Try
    result = OpenRawFile("C:\Raw_Data\H12091.raw", pHeader)
    stamp1 = DirectCast(Marshal.PtrToStructure(pHeader, GetType(RawDataHeader)), RawDataHeader)
Finally
    Marshal.FreeCoTaskMem(pHeader)
End Try
NB: This will involve copying the data returned from the C++ method, so the performance might not be great.
   
v2
Comments
Md23 3-Oct-19 16:50pm
   
I did this and everything worked until I got to the description in the c++ file. I got an exception Exception thrown at 0x0F35337E (vcruntime140d.dll) in ConsoleApp3.exe: 0xC0000005: Access violation writing location 0x00DD2D7C.
Richard Deeming 4-Oct-19 7:48am
   
Are you sure your VB.NET structure definition matches what's in the question? I was getting a "no meaningful size or offset can be computed" error on the Marshal.SizeOf line until I removed the MarshalAs attribute from the entries member.

According to your C++ structure, the member is called streamtype, and is not an array. You've declared it as a single Integer, but added an attribute to marshal it as an array of 4096 ints.
Md23 4-Oct-19 9:20am
   
I fixed that on here but I actually had that part commented out in my code when I ran it. For some reason it is only the description
Md23 7-Oct-19 9:58am
   
I have updated my code on here. I am getting an error on the description memeber
Richard Deeming 7-Oct-19 13:25pm
   
Where is the error being thrown? I thought you said it was from your C++ code, which you haven't shown.
Richard Deeming 7-Oct-19 13:26pm
   
And again, your C++ structure doesn't match your VB.NET structure.
Md23 7-Oct-19 13:54pm
   
I figured it out. I needed to pass the IntPtr by value now.
Richard Deeming 7-Oct-19 13:56pm
   
Good catch. I've updated my answer. :)
KarstenK 4-Oct-19 5:34am
   
This should be a bug in your application code. Fizzle it out ;-)
Md23 4-Oct-19 6:53am
   
It is only on that one structure member. All the others work fine

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100