笔记备忘: 在 C# 中进行 win32 dll 的动态加载, 调用, 和释放
来源:互联网 发布:云和数据培训 编辑:程序博客网 时间:2024/05/02 04:21
///
/// win32 dll 函数动态封装调用
///
///
/// bool flag = false;
/// // 构造时导致 dll 被加载, 借助 using 块, 在离开时释放 dll
/// using (CDCWrapper wrapper = new CDCWrapper())
/// {
/// flag = wrapper.GetLastVerFile(163, "192.168.0.135", "", 0, "ccir", "1B2M2Y8AsgTpgAmY7PhCfg==", "d://1.dwg");
/// MessageBox.Show(flag.ToString());// 这个提示的时候 dll 还在内存中
/// }
/// MessageBox.Show(flag.ToString());// 这个提示时, dll 已经被卸载出内存了
///
class CDCWrapper : IDisposable
{
public CDCWrapper()
{
loadLibray();
}
/// win32 dll 函数动态封装调用
///
///
/// bool flag = false;
/// // 构造时导致 dll 被加载, 借助 using 块, 在离开时释放 dll
/// using (CDCWrapper wrapper = new CDCWrapper())
/// {
/// flag = wrapper.GetLastVerFile(163, "192.168.0.135", "", 0, "ccir", "1B2M2Y8AsgTpgAmY7PhCfg==", "d://1.dwg");
/// MessageBox.Show(flag.ToString());// 这个提示的时候 dll 还在内存中
/// }
/// MessageBox.Show(flag.ToString());// 这个提示时, dll 已经被卸载出内存了
///
class CDCWrapper : IDisposable
{
public CDCWrapper()
{
loadLibray();
}
#region public function wrappers
public bool GetLastVerFile(int nFileId, string strServerIP, string strProxyPort, int nProxyPort, string strUserNo, string strPasswd, string strSavePath)
{
if (libHandle == IntPtr.Zero)
loadLibray();
public bool GetLastVerFile(int nFileId, string strServerIP, string strProxyPort, int nProxyPort, string strUserNo, string strPasswd, string strSavePath)
{
if (libHandle == IntPtr.Zero)
loadLibray();
if (F1Invoker == null)
{
IntPtr f1Handle = GetProcAddress(libHandle, F1NAME);
if (f1Handle == IntPtr.Zero)
throw new EntryPointNotFoundException(F1NAME);
F1Invoker = (FGetLastVerFile)Marshal.GetDelegateForFunctionPointer(f1Handle, typeof(FGetLastVerFile));
}
bool flag = F1Invoker(nFileId, strServerIP, strProxyPort, nProxyPort, strUserNo, strPasswd, strSavePath);
return flag;
}
#endregion
{
IntPtr f1Handle = GetProcAddress(libHandle, F1NAME);
if (f1Handle == IntPtr.Zero)
throw new EntryPointNotFoundException(F1NAME);
F1Invoker = (FGetLastVerFile)Marshal.GetDelegateForFunctionPointer(f1Handle, typeof(FGetLastVerFile));
}
bool flag = F1Invoker(nFileId, strServerIP, strProxyPort, nProxyPort, strUserNo, strPasswd, strSavePath);
return flag;
}
#endregion
#region IDisposable Members
void IDisposable.Dispose()
{
if (libHandle != IntPtr.Zero)
FreeLibrary(libHandle);
}
#endregion
void IDisposable.Dispose()
{
if (libHandle != IntPtr.Zero)
FreeLibrary(libHandle);
}
#endregion
#region function prototypes
[UnmanagedFunctionPointerAttribute(CallingConvention.StdCall)]
delegate bool FGetLastVerFile(int nFileId, string strServerIP, string strProxyPort, int nProxyPort, string strUserNo, string strPaswd, string strSavePath);
#endregion
[UnmanagedFunctionPointerAttribute(CallingConvention.StdCall)]
delegate bool FGetLastVerFile(int nFileId, string strServerIP, string strProxyPort, int nProxyPort, string strUserNo, string strPaswd, string strSavePath);
#endregion
#region windows kernel
[DllImport("kernel32", CharSet = CharSet.Ansi)]
extern static IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
extern static IntPtr LoadLibrary(string lpLibFileName);
[DllImport("kernel32", CharSet = CharSet.Auto)]
static extern int FreeLibrary(IntPtr hLibModule);
#endregion
[DllImport("kernel32", CharSet = CharSet.Ansi)]
extern static IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
extern static IntPtr LoadLibrary(string lpLibFileName);
[DllImport("kernel32", CharSet = CharSet.Auto)]
static extern int FreeLibrary(IntPtr hLibModule);
#endregion
void loadLibray()
{
if (libHandle == IntPtr.Zero)
{
libHandle = LoadLibrary(LIBNAME);
if (libHandle == IntPtr.Zero)
throw new DllNotFoundException(LIBNAME + " 加载失败, 请检查");
}
}
{
if (libHandle == IntPtr.Zero)
{
libHandle = LoadLibrary(LIBNAME);
if (libHandle == IntPtr.Zero)
throw new DllNotFoundException(LIBNAME + " 加载失败, 请检查");
}
}
#region fields
const string LIBNAME = "CDCommu.dll";
IntPtr libHandle = IntPtr.Zero;
const string F1NAME = "_GetLastVerFile@28";
FGetLastVerFile F1Invoker = null;
#endregion
}
const string LIBNAME = "CDCommu.dll";
IntPtr libHandle = IntPtr.Zero;
const string F1NAME = "_GetLastVerFile@28";
FGetLastVerFile F1Invoker = null;
#endregion
}
- 笔记备忘: 在 C# 中进行 win32 dll 的动态加载, 调用, 和释放
- .NET 中动态加载DLL ,调用完之后及时释放。
- 在C#中动态调用native dll的导出函数
- C#中动态加载和卸载DLL
- C#中动态加载和卸载DLL
- C#中动态加载和卸载DLL
- C#中动态加载和卸载DLL
- C#中动态加载和卸载DLL
- C#中动态加载和卸载DLL
- C#中动态加载和卸载DLL
- C#中动态加载和卸载DLL
- C#中动态加载和卸载DLL
- C#中动态加载和卸载DLL
- C#中动态加载和卸载DLL
- 在C#中使用P/Invoke调用Win32 DLL
- 在 C# 中通过 P/Invoke 调用Win32 DLL
- 在 C# 中通过 P/Invoke 调用Win32 DLL
- 在 C# 中通过 P/Invoke 调用Win32 DLL
- 读文件(java)
- android service
- 不要优柔寡断...
- 敏捷开发之源
- Persistence.xml 配置说明
- 笔记备忘: 在 C# 中进行 win32 dll 的动态加载, 调用, 和释放
- 高级http.conf中文版
- windows mobile 调试EVDO模块的RIL超时问题
- 周期图法 periodogram
- SQL中使用WITH AS提高性能-使用公用表表达式(CTE)简化嵌套SQL
- 实习之出乎意料
- FileSystemObject,ADODB.Stream 能否操作UTF-8格式的文件
- VC 6 Error spawning cl.exe, VC 6 打开一个文件或者打开一个目录 崩溃
- 生活的真谛