线程中调用第三方DLL,WinForm程序正常,windows服务中调用报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
同一个DLL,实现同一个功能,在WinForm中使用计时器或Thread调用都正常,但是在windows服务中调用一直报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
郁闷的是,如果windows服务中取消线程直接调用也正常,希望大家帮我看下,代码入下:
public AlarmEvent()
{
InitializeComponent();
timerReAlarm = new System.Timers.Timer(20* 1000);//秒
timerReAlarm.Elapsed +=new ElapsedEventHandler(timerReAlarm_Elapsed);
timerReAlarm.AutoReset = true;
}
private void timerReAlarm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timerReAlarm.Stop();
ThreadPool.QueueUserWorkItem(new WaitCallback(ExecuteTask), null);
timerReAlarm.Start();
}
/// <summary>
/// 开始执行任务
/// </summary>
private void ExecuteTask(object status)
{
TMsgCongestion.MsgAction();
}
---------------------------------------------------------------------------------------以下是调用DLL的部分
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
public static extern int LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
public static extern IntPtr GetProcAddress(int hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);
delegate int Sends([MarshalAs(UnmanagedType.LPStr)]string par1, int par2);
Sends sends;
public void MsgAction(string content)
{
int hmodule = LoadLibrary("SSMQ2Client.dll");
IntPtr intPtrSends = GetProcAddress(hmodule, "sends");
sends = (Sends)Marshal.GetDelegateForFunctionPointer(intPtrSends, typeof(Sends));
sends(content,300);
}
每次就会在执行到 sends(content,300);的地方报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
郁闷的是,如果windows服务中取消线程直接调用也正常,希望大家帮我看下,代码入下:
public AlarmEvent()
{
InitializeComponent();
timerReAlarm = new System.Timers.Timer(20* 1000);//秒
timerReAlarm.Elapsed +=new ElapsedEventHandler(timerReAlarm_Elapsed);
timerReAlarm.AutoReset = true;
}
private void timerReAlarm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timerReAlarm.Stop();
ThreadPool.QueueUserWorkItem(new WaitCallback(ExecuteTask), null);
timerReAlarm.Start();
}
/// <summary>
/// 开始执行任务
/// </summary>
private void ExecuteTask(object status)
{
TMsgCongestion.MsgAction();
}
---------------------------------------------------------------------------------------以下是调用DLL的部分
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
public static extern int LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
public static extern IntPtr GetProcAddress(int hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);
delegate int Sends([MarshalAs(UnmanagedType.LPStr)]string par1, int par2);
Sends sends;
public void MsgAction(string content)
{
int hmodule = LoadLibrary("SSMQ2Client.dll");
IntPtr intPtrSends = GetProcAddress(hmodule, "sends");
sends = (Sends)Marshal.GetDelegateForFunctionPointer(intPtrSends, typeof(Sends));
sends(content,300);
}
每次就会在执行到 sends(content,300);的地方报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
...全文
相关推荐
实现WinForm中嵌入外部EXE程序(调用系统api,源码)
实现在自己的winform内嵌入其他人的exe程序,从而可以实现多应用程序的组合,以及对原应用程序的部分功能的附加,但有部分受保护的应用程序则不能被嵌入
window下C#(winform)调用带界面的Qt库文件(DLL)
C# 与 Qt 混合编程,开发软件。 Qt编写功能模块给C#调用。 Qt带界面的控件给C#调用的常见的办法有两种: 如果不带界面的就很简单,这篇文章是介绍带界面的Qt组件给C#调用。 (1)Qt封装成COM组件方式给C#调用。 (2)Qt直接编译成DLL,导出接口给C#调用。 COM组件方式在之前的好几篇文章都有详细讲解。这篇文章介绍Qt直接编译成DLL,导出接口给C#调用。
winform漂亮的第三方控件按钮datagridview分页等等
搜集了非常漂亮的第三方控件 DataGridView 按钮 combox 漂亮分页控件
c#调用 windows api实现WinForm中嵌入EXE程序
c#调用 windows api实现WinForm中嵌入EXE程序,不错的源码
C#调用matlab画图,解决图像嵌入Winform窗体和首次画图慢的问题
C#调用matlab画图,将图像嵌入Winform窗体,并解决首次画图慢的问题 预览和介绍请看:http://blog.csdn.net/yxy244/article/details/79305757


