关于C#调用C ++函数的问题 [英] A question about C# invoke C++ function
问题描述
当我使用dllimport调用C ++函数时如下:
when i use dllimport to invoke C++ function as below:
[DllImport("Test.dll", EntryPoint = "Init", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern int Init();
i使用这个功能几个月,之后,我在Init中添加一行代码如下:
i use this function serveral months,after that,i add one line code in Init as below:
A* a = new A();
当我使用VS2010调试运行时模型没问题,但是当我使用双击运行exe时,它会抛出错误:
when i use VS2010 debug Run model is ok,but when i use double click to run exe,it throw error:
Try to read or write protected memory. This usually indicates that other memory is corrupt
i在代码中找不到任何问题,非常混乱。
所以发布这个问题,希望有人遇到同样的问题并帮我解决。
谢谢!
我是什么尝试过:
我添加两个messagebox如下:
i can't find any problem in the code,very confuse.
so post this question and hope someone met the same question and help me to solve.
thanks!
What I have tried:
I add two messagebox as below:
MessageBox(NULL,"111111!","OK!",MB_OK | MB_ICONINFORMATION);
A* a = new A();
MessageBox(NULL,"222222!","OK!",MB_OK | MB_ICONINFORMATION);
编译代码项目,以及双击EXE,111111可以弹出,但22222不能。
所以我确定新的A()必须是一些错误。
一个类构造函数如下:
so compile code project,and double click EXE,"111111" can popup,but "22222" cann't be.
so i'm sure that new A() must be some error in.
A class consturct function as below:
A::A()
{
MessageBox(NULL,"333333!","OK!",MB_OK | MB_ICONINFORMATION);
ctx = new des_context();
ctx3 = new des3_context();
bDESKeyExists = false;
bDES3KeyExists = false;
}
i看不到333333弹出窗口,非常混乱,找不到问题。
i cann't see "333333" popup,very confuse,cann't find the question.
推荐答案
看起来A()的构造函数有一些问题。是一个子类,它的父类有问题,或者是一些类成员的构造函数。
错误信息听起来像垃圾内存或指针上的一些读/写操作完成。
将A()构建为调试版本,并在A()构造函数上使用带有step into的调试器。它有效!!!
Looks like the constructor of A() has some issues. Is a child class and its parent has the issue or maybe some constructors of class members.
The error message sounds like some read/write on garbage memory or pointers is done.
Build the A() as debug build and use the debugger with "step into" on the A() constructor. It works!!!
这篇关于关于C#调用C ++函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!