110,536
社区成员
发帖
与我相关
我的任务
分享
namespace webServer.Classes
{
public class PreProcDllLoader
{
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string strDllFile);
[DllImport("kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr hModule, string strProcName);
[DllImport("kernel32.dll")]
private static extern IntPtr FreeLibrary(IntPtr hModule);
[DllImport("kernel32.dll")]
private static extern int GetLastError();
// 委托声明
delegate bool DATAPREPROC(string strDataFile, StringBuilder strError, bool bLogEnabled, string strLogFile, int iLogLevel);
// 预处理动态接口句柄
private IntPtr hModule;
// 函数地址
private IntPtr pDataPreProc;
// 函数接口(函数指针)
private DATAPREPROC InDirectDataPreProc;
// 构造函数
public PreProcDllLoader()
{
hModule = IntPtr.Zero;
pDataPreProc = IntPtr.Zero;
InDirectDataPreProc = null;
}
// 析构函数
~PreProcDllLoader()
{
if (hModule != IntPtr.Zero)
FreeLibrary(hModule);
hModule = IntPtr.Zero;
}
public bool Load(String strDllFile, String strFuncName, StringBuilder strError)
{
String strString;
strString = string.Empty;
// 间接调用
//*/
if (!File.Exists(strDllFile))
{
strString = string.Empty;
strString = string.Format("接口文件不存在!");
strError.Clear();
strError.Append(strString);
return false;
}
if (strFuncName == string.Empty)
{
strString = string.Empty;
strString = string.Format("接口函数为空!");
strError.Clear();
strError.Append(strString);
return false;
}
try
{
hModule = IntPtr.Zero;
hModule = LoadLibrary(strDllFile);
if (hModule == IntPtr.Zero)
{
strString = string.Empty;
strString = string.Format("加载接口失败: {0}", GetLastError());
strError.Clear();
strError.Append(strString);
return false;
}
pDataPreProc = IntPtr.Zero;
pDataPreProc = GetProcAddress(hModule, strFuncName);
if (pDataPreProc == IntPtr.Zero)
{
strString = string.Empty;
strString = string.Format("加载函数失败: {0}", GetLastError());
strError.Clear();
strError.Append(strString);
return false;
}
InDirectDataPreProc = null;
InDirectDataPreProc = (Delegate)Marshal.GetDelegateForFunctionPointer(pDataPreProc, typeof(DATAPREPROC)) as DATAPREPROC;
if (InDirectDataPreProc == null)
{
strString = string.Empty;
strString = string.Format("委托函数失败!");
strError.Clear();
strError.Append(strString);
return false;
}
}
catch (Exception ex)
{
strString = string.Empty;
strString = string.Format("发生异常: {0}", ex.Message);
strError.Clear();
strError.Append(strString);
return false;
}
//*/
return true;
}
public bool ExecuteFunction(string strDataFile, StringBuilder strError, bool bLogEnabled, string strLogFile, int iLogLevel)
{
string strString;
strString = string.Empty;
try
{
//*/
if (InDirectDataPreProc == null)
{
strString = string.Empty;
strString = string.Format("未加载接口!");
strError.Clear();
strError.Append(strString);
return false;
}
return InDirectDataPreProc(strDataFile, strError, bLogEnabled, strLogFile, iLogLevel);
//*/
}
catch (Exception ex)
{
strString = string.Empty;
strString = string.Format("发生异常: {0}", ex.Message);
strError.Clear();
strError.Append(strString);
return false;
}
}
public void Free()
{
if (hModule != IntPtr.Zero)
FreeLibrary(hModule);
hModule = IntPtr.Zero;
}
}
}