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

VB
   <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

C++
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
[no name] 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
[no name] 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

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.
VB.NET
<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.
 
Share this answer
 
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.

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