导入C++dll时发生致命执行引擎错误
本文关键字:执行 引擎 错误 C++dll 导入 | 更新日期: 2023-09-27 18:23:59
我编写了一个应用程序,它从用C++编写的dll中调用方法。它是一个WinForms应用程序,其中有一个WCF服务自托管。WCF方法之一是周期性地在2-3秒内调用。仅执行WCF部分时不会出现问题。当只调用SDK函数时,问题不会出现。但是,如果我启动WCF客户端,它定期调用我的方法,同时我执行一些SDK函数,10秒后出现致命的ExecutionEngineError(System.ExecutionEngineException)。引发异常,应用程序崩溃。我真的不知道问题出在哪里,也不知道如何解决。我怀疑这与SDK有关,但我不确定,只是感觉阅读SO中的相关答案,也许方法导入是错误的?到目前为止,我得到的是:
WCF类:
[ServiceContract]
public partial interface IWCFSample
{
[OperationContract]
string GetData(string param);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public partial class WCFSample : IWCFSample
{
#region Delegates
public delegate string OnGetDataDelegate(object sender, string getDataParam);
#endregion Delegates
#region Events
public event OnGetDataDelegate OnGetData;
#endregion Events
public string GetData(string serializedGetDataParam)
{
string res = string.Empty;
try
{
if (OnGetData != null)
{
res = OnGetData(this, (serializedGetDataParam));
}
}
catch
{
throw;
}
return res;
}
}
像这样启动:
private ServiceHost _serviceHost;
private WCFSample _samplewcf;
private void Form1_Load(object sender, EventArgs e)
{
_samplewcf = new WCFSample();
_samplewcf.OnGetData += samplewcfOnGetData;
_serviceHost = new ServiceHost(_samplewcf);
_serviceHost.Open();
bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;
bw.DoWork += (o, args) =>
{
WCFSampleClient client = new WCFSampleClient();
while (true)
{
if (bw.CancellationPending)
{
return;
}
client.GetData(DateTime.Now.ToString());
Thread.Sleep(200);
}
};
bw.RunWorkerAsync();
}
private string samplewcfOnGetData(object sender, string getDataParam)
{
richTextBox1.Text += "GetData Called - " + getDataParam + "'n";
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.ScrollToCaret();
return getDataParam;
}
与C++相关的部分相当长,但这里是我使用的导入部分。
int OpenDevice (char* strUSBName, int iPortNum )
Imported as:
[DllImport("TP9000.dll")]
public static extern int OpenDevice(string USBNam, int portnum);
int CheckFeeder (int* nCheck)
Imported as:
[DllImport("TP9000.dll")]
public static extern int CheckFeeder(ref int pStatus);
int DP_GetSensorStatus (BYTE *pRailStatus, BYTE *pFeedRollerStatus, BYTE *pTraySensorState)
Imported as:
[DllImport("TP9000.dll")]
public static extern int DP_GetSensorStatus(ref byte pRailStatus, ref byte pFeedRollerStatus, byte[] pTraySensorState);
int PathSenser(int *pStatus)
Imported as:
[DllImport("TP9000.dll")]
public static extern int PathSenser(ref int p1);
int CheckRibbonEx( int *nRibbon, int *nTagRibbon)
Imported as:
[DllImport("TP9000.dll")]
public static extern int CheckRibbonEx(ref int nRibbon, ref int nTagRibbon);
int N_EndOfPrint(int nPanel, int *pPanelCount, int *pJobNo)
Imported as:
[DllImport("TP9000.dll")]
public static extern int N_EndOfPrint(int p1, ref int p2, ref int p3);
int N_PrintJobStatus (int *pJobStatus )
Imported as:
[DllImport("TP9000.dll")]
public static extern int N_PrintJobStatus(int[] p1);
更新:
似乎最后一次导入是错误的:
int N_PrintJobStatus (int *pJobStatus )
Imported as:
[DllImport("TP9000.dll")]
public static extern int N_PrintJobStatus(int[] p1);
如果我不在代码中调用这个函数,它就不会挂起。但我应该如何整理它。文件上写着:
它检查打印的状态。
int N_PrintJobStatus(int*pJobStatus)参数pJobStatus[out]:
- JobStatus[0]=指针状态:空闲(0x00),正在打印(0x01),错误打印停止(0xFF)
- JobStatus[1]=无备用作业(出错时,无暂停作业)
- JobStatus[2]=待机作业的数量(包括当前正在运行的作业)
- JobStatus[3]=当前运行作业的份数(打印份总数)
- JobStatus[4]=剩余作业的副本数(剩余计数)
- JobStatus[5]=是否显示重新打印检查窗口。1:自动选项显示,2:手动选项显示,0:无检查窗口
JobStatus[6]=它检查错误处理和状态消息级别(1~4),然后在重新打印检查窗口显示0t(自动选项)
- 打开打印机,然后检查色带
- 卡正在弹出
- 功能区正在同步
- 如果您想重新打印,请按"确定"按钮。(激活"确定"按钮)
返回值0:成功-1:失败。(Get_Status)
好吧,如果没有其余的代码,就不可能解决这个问题。错误代码如下:
int[] nJobSt = { 0, 0, 0, 0, 0, 0 }; //notice that the array length is 6
LastErrorCode = TP9000Dll.N_PrintJobStatus(nJobSt);
根据SDK文档,我们应该假设函数需要7的数组长度因此正确的代码是:
int[] nJobSt = { 0, 0, 0, 0, 0, 0, 0 }; //notice that the array length is 7
LastErrorCode = TP9000Dll.N_PrintJobStatus(nJobSt);
结论:每一篇文档都要读3遍,千万不要相信SDK附带的示例应用程序。