Man of Honour
- Joined
- 13 Nov 2009
- Posts
- 11,662
- Location
- Northampton
This one is definitely not my strong suit.
I'm using a Third Party DLL for a USB interface that I'm trying to work with in .NET
There's a ReadData and WriteData function provided by the DLL which both take a structure pointer as a parameter
In C the structure looks like this
and in C#
And I'm importing to two DLL functions as follows
Now here's where I'm stuck. Calling the write function works perfectly fine, the USB interface takes my call does exactly as it should. The read function on the other hand never seems to write it's data into the Msg stuct.
It's not the DLL, I can call the read function in c++ or Delphi and it does exactly as it should. If I give the Read function a byte array by changing it's declaration like below it also does exactly as it should. There's clearly something I'm missing as far as how I should be passing/Marshalling my struct between managed/unmanaged code
Using the out keyword Gives me a Attempted to read or write protected memory error.
I'm using a Third Party DLL for a USB interface that I'm trying to work with in .NET
There's a ReadData and WriteData function provided by the DLL which both take a structure pointer as a parameter
In C the structure looks like this
Code:
typedef struct _msg
{
unsigned int ProtocolID;
unsigned int RxStatus;
unsigned int TxFlags;
unsigned int Timestamp;
unsigned int DataSize;
unsigned int ExtraDataIndex;
unsigned char Data[PM_DATA_LEN]
} msg;
and in C#
Code:
[StructLayout(LayoutKind.Sequential, Size = 4152)]
public class Msg
{
[MarshalAs(UnmanagedType.U4)] public PassThruProtocol ProtocolID;
[MarshalAs(UnmanagedType.U4)] public PassThruRxStatus RxStatus;
[MarshalAs(UnmanagedType.U4)] public J2534_TxFlags TxFlags;
[MarshalAs(UnmanagedType.U4)] public UInt32 Timestamp;
[MarshalAs(UnmanagedType.U4)] public UInt32 DataSize;
[MarshalAs(UnmanagedType.U4)] public UInt32 ExtraDataIndex;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4128)] public byte[] Data = new byte[4128];
}
And I'm importing to two DLL functions as follows
Code:
[DllImport(MyDLL)]
public static extern UInt32 WriteMsg(Msg pMsg);
[DllImport(MyDLL)]
public static extern UInt32 ReadMsg(Msg pMsg);
Now here's where I'm stuck. Calling the write function works perfectly fine, the USB interface takes my call does exactly as it should. The read function on the other hand never seems to write it's data into the Msg stuct.
It's not the DLL, I can call the read function in c++ or Delphi and it does exactly as it should. If I give the Read function a byte array by changing it's declaration like below it also does exactly as it should. There's clearly something I'm missing as far as how I should be passing/Marshalling my struct between managed/unmanaged code
Code:
[DllImport(MyDLL)]
public static extern UInt32 ReadMsg(byte[] pMsg);
Using the out keyword Gives me a Attempted to read or write protected memory error.
Last edited: