关于C#调用DLL和C线程回调C#代码问题,大家探讨下。。。。
我使用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 时发生访问冲突。
...全文
相关推荐
C# Csharp 调用 C++的DLL中的回调函数
一个是C++的DLL以及源码 一个是调用他的C#源码 都是VS2010编译测试通过
跨语言集成:从C#调用LabVIEW编译的DLL
在现代软件开发领域,不同编程语言的结合使用已成为实现复杂系统功能和性能优化的常态。特别是LabVIEW和C#的组合,因其在数据采集、仪器控制和桌面应用程序开发方面的强大能力,正受到越来越多开发者的青睐。LabVIEW以其直观的图形化界面和强大的硬件交互能力闻名,而C#则是.NET平台上广泛使用的高效编程语言。要使得这两种语言协同工作,C#调用LabVIEW编写的DLL成为实现跨语言模块化应用的关键技术。
C#与C语言DLL交互实战指南
C#与C语言DLL交互是现代软件开发中的一个重要技术点,特别是在.NET平台上。它允许开发者利用已有的C语言编写的库,扩大C#语言的使用范围,同时提高代码的重用性。理解交互机制,有助于开发者在维护遗留代码或集成第三方库时更加得心应手。在构建应用程序时,与C语言编写的DLL(动态链接库)进行交互是软件开发中的常见需求。DLL可以包含被C#等应用程序调用的代码和数据。为了实现这一点,需要创建和定义DLL中的接口。本章节将详细探讨创建C语言DLL的过程,以及如何定义接口。
C#调用C++的dll实例,回调函数,string和int数组参数传递
C#调用C++的dll实例,回调函数,string和int数组参数传递,在vs2010下调试通过,总结在这里,供大家参考。
C++调用C#的DLL实例程序
C++调用C#的DLL实例程序,压缩包中包含部分程序说明,很小的几行代码,但是C++调用C#的DLL目的已经达到。


