求VC++ 程序 封装成支持C#多线程调用的DLL操作方法或资料!解决方法
求VC++ 程序 封装成支持C#多线程调用的DLL操作方法或资料!
求一实例程序即可,想知道怎么做的,相关资料实例均可
说明缘由:由于本人对VC并不是非常熟悉,在项目中目前遇到一个情况,使用别人做的dll和提供的demo代码,进行了VC第二次DLL封装,但是C#调用时一般只要不同时2个线程调用其DLL程序不会出现问题,目前是不知道对方给的DLL不支持还是自己编写的不支持多线程,所以求方法,我自己做之后就知道原因是什么了,谢谢,高分急求!
------解决方案--------------------
你对该dll 进行二次封装就能解决问题
比如
CTest
{
DLL a;//第三方dll库
TestFun();
}
CTestExport
{
void* InitDll()
{
CTest *pTest = new CTest ;
AddToList(pTest);// 加入队列
return pTest;
}
void TestFun(void* pDll)
{
CTest *pTest = (CTest *)FindDll(pDll);// 从队列中找到相关的CTest 指针
if(pTest) pTest->TestFun(); // 调用相关函数
}
};
------解决方案--------------------
发一个我以前c#调用VC的dll 的例子,或许对你有用:
其中
public bool AddTimer(IntPtr lpWnd, UInt32 uDelay, TimerCallBack fun)函数中实现的代码对应到VC中的代码是:
求一实例程序即可,想知道怎么做的,相关资料实例均可
说明缘由:由于本人对VC并不是非常熟悉,在项目中目前遇到一个情况,使用别人做的dll和提供的demo代码,进行了VC第二次DLL封装,但是C#调用时一般只要不同时2个线程调用其DLL程序不会出现问题,目前是不知道对方给的DLL不支持还是自己编写的不支持多线程,所以求方法,我自己做之后就知道原因是什么了,谢谢,高分急求!
------解决方案--------------------
你对该dll 进行二次封装就能解决问题
比如
CTest
{
DLL a;//第三方dll库
TestFun();
}
CTestExport
{
void* InitDll()
{
CTest *pTest = new CTest ;
AddToList(pTest);// 加入队列
return pTest;
}
void TestFun(void* pDll)
{
CTest *pTest = (CTest *)FindDll(pDll);// 从队列中找到相关的CTest 指针
if(pTest) pTest->TestFun(); // 调用相关函数
}
};
------解决方案--------------------
发一个我以前c#调用VC的dll 的例子,或许对你有用:
其中
public bool AddTimer(IntPtr lpWnd, UInt32 uDelay, TimerCallBack fun)函数中实现的代码对应到VC中的代码是:
bool __declspec(dllexport)__stdcall AddTimer(LPVOID lpHandle,LPVOID lpWnd,DWORD uDelay,TIMERCALLBACK lpTimeProc)
{
CNSHighResolutionTimer *pTimer = theApp.FindTimer(lpHandle);
if(pTimer)
{
return pTimer->AddTimerEventNotify(lpWnd,uDelay,lpTimeProc) ? true : false;
}
return false;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace NetSCADA6.TimerInvokeDll
{
class InvokeDll
{
#region Win API
[DllImport("kernel32.dll")]
private extern static IntPtr LoadLibrary(string path);
[DllImport("kernel32.dll")]
private extern static bool FreeLibrary(IntPtr lib);
[DllImport("kernel32.dll")]
private extern static IntPtr GetProcAddress(IntPtr lib, string funcName);
#endregion
private IntPtr hLib = IntPtr.Zero;
public InvokeDll(String DLLPath)
{
hLib = LoadLibrary(DLLPath);
}
~InvokeDll()
{
FreeLibrary(hLib);
}
public Delegate Invoke(string APIName, Type t)
{
IntPtr api = GetProcAddress(hLib, APIName);
if (api == IntPtr.Zero)
{
return null;
}
else
{
return Marshal.GetDelegateForFunctionPointer(api, t);
}
}
}
public class InvokeTimer
{
#region 接口
private delegate IntPtr InvokeInitDll();
private delegate bool InvokeUnInitDll(IntPtr lpParser);
// 添加定时器事件通知,定时器响应函数TimerCallBack中不能有耗时的操作,否则影响定时器正常工作
private delegate bool InvokeAddTimer(IntPtr lpHandle, IntPtr lpWnd, UInt32 uDelay, TimerCallBack fun);
// 删除定时器事件通知
private delegate bool InvokeRemoveTimer(IntPtr lpHandle, IntPtr lpWnd, UInt32 uDelay);
// 删除指定lpWnd所有定时器通知