请问如何在程序中动态调用DLL?

KissRei 2009-02-14 10:19:10
我写了一个程序,把加密算法部分单独写成一个类库,然后编译成一个DLL文件。我的想法是希望当这个DLL在本台电脑上的时候,程序能够正常运行,而当DLL不再这台电脑上的时候,程序是无法正常运行的,请问高手这样如何实现?能给个思路也好,非常感谢。
...全文
36 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
KissRei 2009-02-14
  • 打赏
  • 举报
回复
非常感谢。。。。可惜我已经没有分了,所以,只能够很真诚地对LS说一声thanks!
mywisest 2009-02-14
  • 打赏
  • 举报
回复
应该有多种方法,可以判断文件是否存在,也可以试着加载函数。


[DllImport("Kernel32")]
internal static extern int GetProcAddress(int handle, String funcname);
[DllImport("Kernel32")]
internal static extern int LoadLibrary(String funcname);
[DllImport("Kernel32")]
internal static extern int GetModuleHandle(String funcname);
[DllImport("Kernel32")]
internal static extern int FreeLibrary(int handle);
//定义该方法的委托
public delegate int GetNativeSystemInfo (int hwnd, bool is64bit);

public void LoadLib(string dllName,string methodName)
{
int handle = GetModuleHandle(dllName);
int handle = LoadLibrary(dllName);
int p = GetProcAddress(handle, methodName );
if (p > 0) //如果加载了该函数
{
GetNativeSystemInfo SysInfo = (GetNativeSystemInfo)Marshal.GetDelegateForFunctionPointer(new IntPtr(p), typeof(GetNativeSystemInfo));
SysInfo(ref _sysInfo);
}
else
GetSystemInfo(ref _sysInfo);
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧