Marshalling structure with const char* in unmanaged code returns 'System.AccessViolationException'

  • Thread starter Thread starter krishnakumar.j
  • Start date Start date
K

krishnakumar.j

Guest
In C# the structure with string member is as below:\


public struct EDNetBuf
{


[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]

public string dummy;
public delegate void finalizeDelegate(System.IntPtr @object);
public finalizeDelegate finalize;

public readonly int _ref;
public EDNetEventType type;
public uint len;

[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public string data;
}


[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]

public struct EDNetFrame
{
public EDNetBuf buf; //*< a buffer, the base object
public uint timestamp; //*< timestamp
public byte channel; //*< channel number
public byte ptype; //*< payload, see #EDNetCodecID
public ushort flags; //*< flags, see Flag
public int fmt; //*< format, see #EDNetFrameFormat
public int width; //*< The width of video frame
public int height; //*< The height of video frame
}
I am having unmanaged c code from which I converted C# code as follows:
struct EDNetBuf
{
/**
* private data. NEVER MODIFY THIS!
*/

const char *dummy;
/**
* private data. NEVER MODIFY THIS!
*/
void (*finalize) (void *object);
/**
* private data. NEVER MODIFY THIS!
*/
int _ref;
/**
* the length of buffer data.
*/
unsigned int len;

/**
* the pointer to buffer data.
*/
char *data;
};
In C# When i marshal this using:
f = (EDNetFrame)System.Runtime.InteropServices.Marshal.PtrToStructure(param,typeof(EDNetFrame));
When marshalling as above with PtrToStructure,I am getting error An unhandled exception of type 'System.AccessViolationException' occurred in mscorlib.dll Attempted to read or write protected memory. This is often an indication that other memory is corrupt..'


I found out this is because of the structure member string dummy. How to solve this error ?

I found that using System.IntPtr for dummy instead of string does not work.

Continue reading...
 

Similar threads

R
Replies
0
Views
147
Raymond Gilbers
R
S
Replies
0
Views
106
Stan Huang at Taiwan
S
D
Replies
0
Views
126
DenisAndreevich
D
Back
Top