15,471
社区成员
发帖
与我相关
我的任务
分享
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所有定时器通知
private delegate bool InvokeRemoveTimerByWnd(IntPtr lpHandle, IntPtr lpWnd);
// 删除所有定时器事件通知
private delegate void InvokeRemoveAllTimer(IntPtr lpHandle);
// 定时器是否正在工作
private delegate bool InvokeIsTimerInservice(IntPtr lpHandle);
// 杀死定时器
private delegate void InvokeKillBaseTimer(IntPtr lpHandle);
// 创建一个基础定时器
private delegate bool InvokeCreateBaseTimer(IntPtr lpHandle, UInt32 uDelay);
public delegate void TimerCallBack(IntPtr pWnd, UInt32 uID, UInt32 uMsg, UInt32 dw1, UInt32 dw2);
public static TimerCallBack TimerCallBackFun;// 静态
private InvokeDll _InvokeDll = null;
private IntPtr _ndllPtr = IntPtr.Zero;
private InvokeInitDll _InitDll = null;
private InvokeUnInitDll _UnInitDll = null;
private InvokeAddTimer _AddTimer = null;
private InvokeRemoveTimer _RemoveTimer = null;
private InvokeRemoveTimerByWnd _RemoveTimerByWnd = null;
private InvokeRemoveAllTimer _RemoveAllTimer = null;
private InvokeIsTimerInservice _IsTimerInservice = null;
private InvokeKillBaseTimer _KillBaseTimer = null;
private InvokeCreateBaseTimer _CreateBaseTimer = null;
#endregion
#region 构造
public InvokeTimer()
{
}
~InvokeTimer()
{
}
#endregion
#region 函数接口
/// <summary>
/// 初始化dll
/// </summary>
/// <param name="PipeDllName"></param>
/// <returns></returns>
public bool InitDll(string DllName)
{
try
{
if (_InvokeDll == null)
_InvokeDll = new InvokeDll(DllName);
if (_InvokeDll != null)
{
_InitDll = (InvokeInitDll)_InvokeDll.Invoke("InitDll", typeof(InvokeInitDll));
_UnInitDll = (InvokeUnInitDll)_InvokeDll.Invoke("UnInitDll", typeof(InvokeUnInitDll));
_AddTimer = (InvokeAddTimer)_InvokeDll.Invoke("AddTimer", typeof(InvokeAddTimer));
_RemoveTimer = (InvokeRemoveTimer)_InvokeDll.Invoke("RemoveTimer", typeof(InvokeRemoveTimer));
_RemoveTimerByWnd = (InvokeRemoveTimerByWnd)_InvokeDll.Invoke("RemoveTimerByWnd", typeof(InvokeRemoveTimerByWnd));
_RemoveAllTimer = (InvokeRemoveAllTimer)_InvokeDll.Invoke("RemoveAllTimer", typeof(InvokeRemoveAllTimer));
_IsTimerInservice = (InvokeIsTimerInservice)_InvokeDll.Invoke("IsTimerInservice", typeof(InvokeIsTimerInservice));
_KillBaseTimer = (InvokeKillBaseTimer)_InvokeDll.Invoke("KillBSTimer", typeof(InvokeKillBaseTimer));
_CreateBaseTimer = (InvokeCreateBaseTimer)_InvokeDll.Invoke("CreateBSTimer", typeof(InvokeCreateBaseTimer));
}
if (_InitDll != null)
{
_ndllPtr = _InitDll();
if (_ndllPtr != null)
{
return true;
}
}
}
catch
{
}
return false;
}
/// <summary>
/// 退出时调用
/// </summary>
public bool UnInitDll()
{
try
{
if (_UnInitDll != null)
{
if (_ndllPtr != null)
{
bool bSuccess = _UnInitDll(_ndllPtr);
_InvokeDll = null;
GC.Collect();
return bSuccess;
}
}
}
catch
{
}
return false;
}
/// <summary>
/// 添加定时器事件通知,定时器响应函数TimerCallBack中不能有耗时的操作,否则影响定时器正常工作
/// </summary>
/// <param name="lpWnd"></param>
/// <param name="uDelay"></param>
/// <param name="fun"></param>
/// <returns></returns>
public bool AddTimer(IntPtr lpWnd, UInt32 uDelay, TimerCallBack fun)
{
try
{
if (_AddTimer != null)
{
if (_ndllPtr != null)
{
TimerCallBackFun = new TimerCallBack(fun);
return _AddTimer(_ndllPtr, lpWnd, uDelay, TimerCallBackFun);
}
}
}
catch
{
}
return false;
}
/// <summary>
/// 删除定时器事件通知
/// </summary>
/// <param name="lpWnd"></param>
/// <param name="uDelay"></param>
/// <returns></returns>
public bool RemoveTimer(IntPtr lpWnd, UInt32 uDelay)
{
try
{
if (_RemoveTimer != null)
{
if (_ndllPtr != null)
{
return _RemoveTimer(_ndllPtr, lpWnd, uDelay);
}
}
}
catch
{
}
return false;
}
/// <summary>
/// 删除指定lpWnd所有定时器通知
/// </summary>
/// <param name="lpWnd"></param>
/// <returns></returns>
public bool RemoveTimerByWnd(IntPtr lpWnd)
{
try
{
if (_RemoveTimerByWnd != null)
{
if (_ndllPtr != null)
{
return _RemoveTimerByWnd(_ndllPtr, lpWnd);
}
}
}
catch
{
}
return false;
}
/// <summary>
/// 删除所有定时器事件通知
/// </summary>
public void RemoveAllTimer()
{
try
{
if (_RemoveAllTimer != null)
{
if (_ndllPtr != null)
{
_RemoveAllTimer(_ndllPtr);
}
}
}
catch
{
}
}
/// <summary>
/// 定时器是否正在工作
/// </summary>
/// <returns></returns>
public bool IsTimerInservice()
{
try
{
if (_IsTimerInservice != null)
{
if (_ndllPtr != null)
{
return _IsTimerInservice(_ndllPtr);
}
}
}
catch
{
}
return false;
}
/// <summary>
/// 杀死定时器
/// </summary>
public void KillBaseTimer()
{
try
{
if (_KillBaseTimer != null)
{
if (_ndllPtr != null)
{
_KillBaseTimer(_ndllPtr);
}
}
}
catch
{
}
}
/// <summary>
/// 创建一个基础定时器
/// </summary>
/// <param name="uDelay"></param>
/// <returns></returns>
public bool CreateBaseTimer(UInt32 uDelay)
{
try
{
if (_CreateBaseTimer != null)
{
if (_ndllPtr != null)
{
return _CreateBaseTimer(_ndllPtr, uDelay);
}
}
}
catch
{
}
return false;
}
}
#endregion
}