[英]Importing c++ dll in c# project
I am importing some c++ dll into ac# project, I am using visual studio 2010. I have succeded to import function that are using built-in type, however I am getting error when I have tried to deal with structure. 我正在将一些c ++ dll导入到ac #project中,我正在使用visual studio 2010.我已经成功导入了使用内置类型的函数,但是当我尝试处理结构时遇到错误。 This is a simple example:
这是一个简单的例子:
c++ code c ++代码
typedef long int TDate;
typedef struct _TMDYDate
{
long month; /* In range [1,12] */
long day; /* In range [1-31] */
long year; /* In range [1600-] */
} TMonthDayYear;
int JpmcdsDateToMDY
(TDate date, /* (I) TDate format */
TMonthDayYear *mdyDate);
and I have translated to c# as: 我已翻译成c#:
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct TMonthDayYear {
public int month;
public int day;
public int year;
}
public partial class NativeMethods {
[System.Runtime.InteropServices.DllImportAttribute("MyDll.dll", EntryPoint="JpmcdsDateToMDY")]
public static extern int JpmcdsDateToMDY(int date, ref TMonthDayYear mdyDate) ;
}
when I try to run the function in my test program I get this error: 当我尝试在我的测试程序中运行该函数时,我收到此错误:
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. 未处理的异常:System.AccessViolationException:尝试读取或写入受保护的内存。 This is often an indication that other memory is corrupt.
这通常表明其他内存已损坏。 at CsharpWrapper.NativeMethods.JpmcdsDateToMDY(Int32 date, TMonthDayYear& mdy Date)
在CsharpWrapper.NativeMethods.JpmcdsDateToMDY(Int32日期,TMonthDayYear&mdy Date)
The struct are declare in the stack and I thought (maybe) was the problem but I am still getting the same error even though I have change TMonthDayYear to class. 结构在堆栈中声明,我认为(也许)是问题但我仍然得到相同的错误,即使我已经将TMonthDayYear更改为类。
What am I doing wrong? 我究竟做错了什么?
Thanks for you help. 谢谢你的帮助。
You have to use the CallingConvention property in the [DllImport] attribute, this is Cdecl since you didn't use __stdcall in the native function declaration. 您必须在[DllImport]属性中使用CallingConvention属性,这是Cdecl,因为您在本机函数声明中没有使用__stdcall。 While that's wrong, it is not a great explanation for the AV.
虽然这是错的,但对AV来说并不是一个很好的解释。 You need to debug the C code, the AV suggests it has a pointer bug.
你需要调试C代码,AV建议它有一个指针错误。 Project + Properties, Debug, tick "Enable unmanaged code debugging" and set a breakpoint on the C function.
Project + Properties,Debug,勾选“启用非托管代码调试”并在C函数上设置断点。
Frankly, a date conversion like this should be written in pure C#. 坦率地说,像这样的日期转换应该用纯C#编写。
本机代码中的TDate
类型为long int
但在托管代码中,它表示为int32而不是int64。
它可能相关也可能不相关,但您需要mdyDate参数上的[OutAttribute]
才能写入它。
If you are an accustomed user in c++ you are probably familiar with pointers addressing the memory directly. 如果您是c ++中的习惯用户,您可能熟悉直接寻址内存的指针。 Your error looks like a memory read/write protection related problem.
您的错误看起来像是内存读/写保护相关的问题。
This is prohibited by standard nature of C# and u have to put the compiler in Unsafe mode. 这是C#的标准性质所禁止的,你必须将编译器置于不安全模式。
If you code unsafe in c# u have to put code into unsafe mode. 如果您在c#中编码不安全,则必须将代码置于不安全模式。
unsafe
{
// unsafe things
}
unsafe class Class1 {}
static unsafe void someMethod ( int* cpi, int lngth) {...}
You also have to check the project configuration ( Build -tab) and tack the checkbox for " Allow unsafe code ". 您还必须检查项目配置( Build -tab)并添加“ 允许不安全代码 ”复选框。
I apologize if I was scrolling by some too obvious information. 如果我滚动一些太明显的信息,我道歉。 I will also state that this comment only has meaning if the situation is that C# going to address memory.
我还要说明,如果情况是C#要解决内存问题,那么这个评论只有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.