关于C#调用DLL和C线程回调C#代码问题,大家探讨下。。。。
时间:2011-12-21
来源:互联网
我使用VC创建了一个DLL文件,这个DLL文件唯一特殊地方是它创建一个线程,该线程可以回调C#代码?
问题出现在回调几次后C#程序出错挂掉。
VC 程序如下:Test.DLL
typedef int(*InputFunction)();
typedef int(*OutputFunction)();
//全局变量
InputFunction SpexInputFunction=NULL;
OutputFunction SpexOutputFunction=NULL;
int IsStop=0;
HANDLE spexthread=NULL;
DWORD spexthreadID;
static DWORD WINAPI ThreadProc(LPVOID lpParam)
{
while(IsStop ==1)
{
Sleep(5000);
SpexInputFunction();
}
SpexOutputFunction();
return 0;
}
//初始化函数指针
int Start(SpeexInputFunction inputfun,SpeexOutputFunction outputfun)
{
SpexInputFunction=inputfun;
SpexOutputFunction=outputfun;
IsStop=1;
spexthread=CreateThread(NULL,NULL,ThreadProc,NULL,0,&spexthreadID);
}
void SpeexNativeStop()
{
IsStop=0;
WaitForSingleObject(spexthread,INFINITE);
CloseHandle(spexthread);
}
C#调用 Start C语言创建一个线程,该线程回调C#中的函数。
C#代码如下:
public delegate int DelegateInput();
public delegate int DelegateOutput();
public partial class Form1 : Form
{
[DllImport( "Test.DLL ")]
internal static extern int Start(DelegateSpeexInput inputfucntion,DelegateSpeexOutput outputfunction);
[DllImport( "Test.DLL ")]
internal static extern int Stop();
public static int Input()
{
//MessageBox.Show( "InputFunction ");
return 0;
}
public static int OutPut()
{
// MessageBox.Show( "OutPutFunction ");
return 0;
}
private DelegateInput spexinput = new DelegateInput(Input);
private DelegateOutput spexoutput = new DelegateOutput(OutPut);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int result=Start(spexinput, spexoutput);
//MessageBox.Show(result.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
Stop();
}
}
主要代码我已经写出来了,大家一起探讨下。现象是C创建的线程头几次回调是没有问题的,回调次数多了C#主程序就挂掉了。错误提示:中的 0x0365bc00 处未处理的异常: 0xC0000005: 写入位置 0x03770000 时发生访问冲突。
问题出现在回调几次后C#程序出错挂掉。
VC 程序如下:Test.DLL
typedef int(*InputFunction)();
typedef int(*OutputFunction)();
//全局变量
InputFunction SpexInputFunction=NULL;
OutputFunction SpexOutputFunction=NULL;
int IsStop=0;
HANDLE spexthread=NULL;
DWORD spexthreadID;
static DWORD WINAPI ThreadProc(LPVOID lpParam)
{
while(IsStop ==1)
{
Sleep(5000);
SpexInputFunction();
}
SpexOutputFunction();
return 0;
}
//初始化函数指针
int Start(SpeexInputFunction inputfun,SpeexOutputFunction outputfun)
{
SpexInputFunction=inputfun;
SpexOutputFunction=outputfun;
IsStop=1;
spexthread=CreateThread(NULL,NULL,ThreadProc,NULL,0,&spexthreadID);
}
void SpeexNativeStop()
{
IsStop=0;
WaitForSingleObject(spexthread,INFINITE);
CloseHandle(spexthread);
}
C#调用 Start C语言创建一个线程,该线程回调C#中的函数。
C#代码如下:
public delegate int DelegateInput();
public delegate int DelegateOutput();
public partial class Form1 : Form
{
[DllImport( "Test.DLL ")]
internal static extern int Start(DelegateSpeexInput inputfucntion,DelegateSpeexOutput outputfunction);
[DllImport( "Test.DLL ")]
internal static extern int Stop();
public static int Input()
{
//MessageBox.Show( "InputFunction ");
return 0;
}
public static int OutPut()
{
// MessageBox.Show( "OutPutFunction ");
return 0;
}
private DelegateInput spexinput = new DelegateInput(Input);
private DelegateOutput spexoutput = new DelegateOutput(OutPut);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int result=Start(spexinput, spexoutput);
//MessageBox.Show(result.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
Stop();
}
}
主要代码我已经写出来了,大家一起探讨下。现象是C创建的线程头几次回调是没有问题的,回调次数多了C#主程序就挂掉了。错误提示:中的 0x0365bc00 处未处理的异常: 0xC0000005: 写入位置 0x03770000 时发生访问冲突。
作者: smallmice 发布时间: 2011-12-21
是不是有些资源还没来得急释放掉啊 http://www.web521.com/asp.net/513368/T610357.shtml
作者: hefeng_aspnet 发布时间: 2011-12-21
以前写过一个程序,调用别人C++写的dll,里面包含了n个线程。
就是这个样子,调用几次,就挂掉。
最后也没找到什么好办法,不过项目最后不做了。
你试试,GC.KeepAlive(object),这个事件,当时是没有找到这个方法,也没有测试。
你试试看行不行。
就是这个样子,调用几次,就挂掉。
最后也没找到什么好办法,不过项目最后不做了。
你试试,GC.KeepAlive(object),这个事件,当时是没有找到这个方法,也没有测试。
你试试看行不行。
作者: mjp1234airen4385 发布时间: 2011-12-21
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28