的P / Invoke抛出System.ExecutionEngineException [英] P/Invoke throw System.ExecutionEngineException

查看:99
本文介绍了的P / Invoke抛出System.ExecutionEngineException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个封闭源代码的非托管的DLL在C ++中,我想在C#解决方案中使用,所以我创建了使用P / Invoke来调用闭源DLL函数的包装管理DLL编码。这工作得很好无参数函数和INT的变量。不过,我运行一个更复杂的函数,采取结构作为参数数组,它包含字符串的字符数组时收到System.ExecutionEngineException。这里是我有什么:

I have a closed source unmanaged DLL coded in C++ that I wanted to use in a C# solution so I created a wrapper managed DLL that use P/Invoke to call the closed source DLL function. That works pretty well for no param function and int variables. However I get a System.ExecutionEngineException when running a more complex function that take an array of struct as parameter which contains array of char for strings. Here is what I had:

[StructLayout(LayoutKind.Sequential)]
public struct Target
{
    public int targetID;

    public string Label;
}

[DllImport("tyrfde.dll", EntryPoint = "tyrfdeGetTarget")]
public static extern int GetTarget(ref Target[] targets);



下面是我从DLL的头文件中掌握的信息:

Below is the information I have from the header file of the DLL:

#define TARGET_LBL_SIZE   (256l)

typedef struct _tyrfdeTarget
{
    TInt32 TargetID;                   // integer signed 32bits
    TCharA Label[TARGET_LBL_SIZE];     // caracter
} tyrfdeTarget;

TInt32 __stdcall tyrfdeGetTargets(tyrfdeTarget* pTargets);



不明白为什么被指定数组的大小,只要但无论如何SizeConst只需要int类型。这里是一些搜索之后就是我试图修复。

Not quite sure why the array size is specified as long but anyway SizeConst only take int. After some search here is what I tried to fix.

[StructLayout(LayoutKind.Sequential, Size = 260), Serializable]
public struct Target
{
    public int targetID;

    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string Label;
}

[DllImport("tyrfde.dll", EntryPoint = "tyrfdeGetTargets")]
public static extern int GetTarget(ref Target[] targets);



不过,我仍然有问题。我已阅读,如果使用CLR内存的功能明确的部分这个异常可以抛出。不幸的是我无法验证。有东西在我的代码,显然是错误的,并可能导致问题?

But I still have the problem. I have read that this exception can throw if the functions clear part of the memory used by CLR. Unfortunately I cannot verify that. Is there something in my code that is obviously wrong and could cause the problem?

推荐答案

嗯,我觉得你的问题是与REF 目标[]目标参数。 。AFAIR这是一个参考,这可能不是你真正想要的参考

Hm, I think your problem is with the ref Target[] targets parameter. AFAIR this is a reference to a reference, which is probably not what you actually want.

我想尝试这样的:

[DllImport("tyrfde.dll", EntryPoint = "tyrfdeGetTargets")]
public static extern int GetTarget([Out, MarshalAs(UnmanagedType.LPArray)] Target[] targets);



可能的这篇文章帮助您找到正确的声明。

Maybe this article helps you to find the correct declaration.

请注意,这个大小数组并不清楚这里,通常在这种情况下,也有一个 REF INT长度参数,然后可以在的MarshalAs 通过 SizeParameterIndex property属性。

Note that the size of the array is not clear here, usually in such cases there is also a ref int length parameter, which can then be referenced in the MarshalAs attribute via the SizeParameterIndex property.

这篇关于的P / Invoke抛出System.ExecutionEngineException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆