Newbie to C#. I have written a WDF driver and DLL that work. I am creating an application in C# to access the hardware through the DLL. There is a specific function that is causing an ExecutionEngineException soon after it is first called. Here is the function definition from the DLL:
DECLDIR int ReadDatagram(int channel, unsigned long *msgID, unsigned int *msgType, int *msgLen, unsigned int *data);
In my C# application code, I import this function with the following lines:
[DllImport("pcmcanDLL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
internal static extern int ReadDatagram(int channel, ref uint msgID, ref uint msgType, ref int msgLen, uint[] data);
When I start the application and open a channel, this function is periodically called by a timer. After a short indefinite time, I get the following exception message. If I comment out he call of this function, the application never has an issue.
Mesage: An unhandled exception of type 'System.ExecutionEngineException' occurred in mscorlib.dll
My application code is here. I believe I am handling the pointer arguments correctly because occasionally this will work a few times and the data is good in those ceases. Appreciate any insights.
private void rcvTimer_Tick(object sender, EventArgs e)
{
int channel = 1;
String dsplyString = "Packet Received\n";
uint msgID = 0, msgType = 0;
int msgLen = 0;
uint[] data = new uint[8];
ErrorTypes dllReturn = ErrorTypes.RCV_BUFFER_EMPTY;
do
{
dllReturn = (ErrorTypes)NativeMethods.ReadDatagram(channel, ref msgID, ref msgType, ref msgLen, data);
if (dllReturn != ErrorTypes.SUCCESS && dllReturn != ErrorTypes.RCV_BUFFER_EMPTY)
{
MessageBox.Show("Error receiving packet.", "Receipt Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
else if (dllReturn == ErrorTypes.SUCCESS)
{
dsplyString = String.Format("{0} {1} {2} {3}\n", channel, msgID, msgType, msgLen);
}
} while (dllReturn != ErrorTypes.RCV_BUFFER_EMPTY);
}
Try following
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
}
public enum ErrorTypes : int
{
RCV_BUFFER_EMPTY = 0,
SUCCESS = 1
}
public class Test
{
[DllImport("pcmcanDLL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
internal static extern int ReadDatagram(int channel, ref uint msgID, IntPtr msgType, ref int msgLen, IntPtr dataPtr);
private void rcvTimer_Tick(object sender, EventArgs e)
{
int channel = 1;
String dsplyString = "Packet Received\n";
uint msgID = 0;
uint msgType = 0;
int msgLen = 0;
uint[] data = new uint[8];
ErrorTypes dllReturn = ErrorTypes.RCV_BUFFER_EMPTY;
IntPtr dataPtr = Marshal.AllocHGlobal(Marshal.SizeOf(data));
IntPtr msgTypePtr = Marshal.AllocHGlobal(Marshal.SizeOf(msgType));
do
{
Marshal.StructureToPtr(msgType, msgTypePtr, true);
Marshal.StructureToPtr(data, dataPtr, true);
dllReturn = (ErrorTypes)ReadDatagram(channel, ref msgID, msgTypePtr, ref msgLen, dataPtr);
if (dllReturn != ErrorTypes.SUCCESS && dllReturn != ErrorTypes.RCV_BUFFER_EMPTY)
{
MessageBox.Show("Error receiving packet.", "Receipt Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
else if (dllReturn == ErrorTypes.SUCCESS)
{
dsplyString = String.Format("{0} {1} {2} {3}\n", channel, msgID, msgType, msgLen);
}
} while (dllReturn != ErrorTypes.RCV_BUFFER_EMPTY);
Marshal.FreeHGlobal(dataPtr);
Marshal.FreeHGlobal(msgTypePtr);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.