windows服务的托管资源(C#) 静态调用 非托管资源(Delphi-dll) 开机造成无法进入桌面(一)...

 案例:

  项目中用C#开发的windows服务中调用了Delphi2007开发的一个Dll,在开机后等待2分钟左右再输入密码进入系统桌面,此时explorer.exe卡死,无法启动,在任务管理器中关闭系统服务后,正常进入系统桌面。

原因:

  经过反复测试跟踪,我大致发现两个原因:

  1. .net导入非托管DLL时释放问题
  2. .net调用的Dephi的DLL进行了多层嵌套,即Delphi的DLL有调用了一个Delphi的DLL;S    

解决方案:

  1.原因1的解决方案:将要执行的函数转换为委托

    

ContractedBlock.gif ExpandedBlockStart.gif DllInvoke.cs
 
    
1 public class DllInvoke
2 {
3 #region Win API
4 [DllImport( " kernel32.dll " )]
5 private extern static IntPtr LoadLibrary( string path);
6
7 [DllImport( " kernel32.dll " )]
8 private extern static IntPtr GetProcAddress(IntPtr lib, string funcName);
9
10 [DllImport( " kernel32.dll " )]
11 private extern static bool FreeLibrary(IntPtr lib);
12 #endregion
13
14 private IntPtr hLib;
15 public DllInvoke(String DLLPath)
16 {
17 hLib = LoadLibrary(DLLPath);
18 }
19
20 // ~DllInvoke()
21 // {
22 // FreeLibrary(hLib);
23 // }
24
25 // 将要执行的函数转换为委托
26   public Delegate Invoke( string APIName, Type t)
27 {
28 IntPtr api = GetProcAddress(hLib, APIName);
29 return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
30 }
31
32 public void Free()
33 {
34 FreeLibrary(hLib);
35 }
36 }
ContractedBlock.gif ExpandedBlockStart.gif 代码
 
    
private const string _fileDll = @" 外部dll名称.dll " ;

public delegate bool CanInstallBool_DynamicHandler();
public static bool CanInstallBool_Dynamic()
{
DllInvoke dll
= new DllInvoke(_fileDll);

CanInstallBool_DynamicHandler method
= (CanInstallBool_DynamicHandler)dll.Invoke( " 外部dll的的函数名 "
,
typeof (CanInstallBool_DynamicHandler));

bool ret = false ;

try
{
ret
= method();
}
finally
{
dll.Free();
}

return ret;
}

 注:该方法不能再线程中使用,不然会造成很严重的内存泄露.

  

  2. 原因二的解决方案是不进行嵌套,:-)



转载于:https://www.cnblogs.com/ruanzb/archive/2010/07/23/1783852.html

猜你喜欢

转载自blog.csdn.net/weixin_34126557/article/details/94018726