Greetings
I've asked before on other forums but so far I'm stumped.
I have a Delphi DLL library that communicates with an MCU and it works fine as it is. However I'm not into pascal and would rather continue learning C#. I'm trying to use the Delphi DLL from within the C# structure as it already handles the communication with the mcu.
List of functions and procedures provided by the DLL
Code:
Type TData = array [0..255] of byte;
function Receive(Adr, Len: Integer): TData; stdcall;
This dll reads data from the mcu.
Adr= Starting address
Len= Total length of the data being read
The function returns a TData array where the item with index 0 contains the starting address and the item with index 1 contains the length of the data received.
Following items (index 2 and higher) contain the data received. If the Receive operation fails, the length value returned in the array is set to zero.
I have been trying the following without success.
Code:
[DllImport("mrds.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static unsafe extern byte[] Receive(int Adr, int Len);
private void btnReceive_Click(object sender, EventArgs e)
{
txtBox1.Text = "";
StatusLbl1.Text = "";
byte[] Data = new byte[8];
int i, Adr =0x02, Len = 8;
string s = "";
for (i = 0; i <= 7; i ++ ) Data[i] = 0;
Data[0] = 0x02;
Data= Receive(Adr, Len);
for (i = 1; i <= 8; i++)
s = s + Data[i + 1].ToString();
txtBox1.Text = s;
}
Everything works up to
Code:
Data= Receive(Adr, Len);
where it returns an error "MarshalDirectiveException was unhandled"
I'm lost, should I be using PInvoke or do I use a Marshal?