导入C++dll时发生致命执行引擎错误
FatalExecutionEngineError when importing C++ dll
我编写了一个应用程序,它从用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附带的示例应用程序。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- C++,系统无法执行指定的程序
- 使用C++中的模板和运算符重载执行矩阵运算
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 执行函数时导致崩溃的变量
- 无论条件是否为true,if总是在c++中执行
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 在C++中对T*类型执行std::move的意外行为
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如何在没有信号的情况下从C++执行QML插槽
- 如何确认我的constexpr表达式实际上已经在编译时执行
- C++17中的并行执行策略
- 当使用带有VS2019或VSCode的虚幻引擎4.24.2时,我如何修复这些错误的Intellisense错误
- QML按钮点击功能执行顺序
- 如何在 GLFW 中为游戏引擎执行正确的输入类
- 虚幻引擎Actor类的OnConstruction()方法未正确执行
- 通过写入全局llvm的地址从执行引擎外部初始化全局llvm值有效吗
- 导入C++dll时发生致命执行引擎错误
- LLVM执行引擎找不到我的函数
- c#致命执行引擎错误调用动态加载的.dll