shenghuo_li
shenghuo_li
2020-06-26 20:25
采纳率: 0%
浏览 458

如何强制关闭运行的DLL

使用C#调用C的DLL程序,dll的程序可能运行很长时间,有办法在c代码中强制停止dll的运行吗?我试了thread.abort只是把调用者退出了,dll还在运行。

DLL程序

CalcLib.h

#define RETI __declspec(dllexport)
RETI void SetA(int a);
RETI void SetB(int b);
RETI int Add();

CalcLib.c

int va = 0;
int vb = 0;
RETI void SetA(int a)
{
     va = a;
}

RETI void SetB(int b)
{
     vb = b;
}
RETI int Add()
{
     Sleep(5000);
     printf("Add Result\n");
     return va + vb;
}

C#程序
NativeMethod.cs

public static class NativeMethod
{
     [DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
     public static extern int LoadLibrary(
             [MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);

     [DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
     public static extern IntPtr GetProcAddress(int hModule,
             [MarshalAs(UnmanagedType.LPStr)] string lpProcName);

     [DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
     public static extern bool FreeLibrary(int hModule);
}

InteropLib.cs

 public class InteropLib
  {
         private string _dllName;
         int _hModule = 0;
         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
         private delegate int DoAction();

         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
         private delegate void SetVariable(int va);

         private DoAction _doAction;

         private Dictionary<string, SetVariable> _setVariables = new Dictionary<string,SetVariable>();

         public InteropLib(string dll)
         {
             _dllName = dll;
             _hModule = NativeMethod.LoadLibrary(_dllName);
         }

         public int AddSettings(string name,string function)
         {
             IntPtr intPtr = NativeMethod.GetProcAddress(_hModule, function);
             SetVariable setFuntion = (SetVariable)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(SetVariable));
             _setVariables.Add(name,setFuntion);

             return 0;
         }


         public int setDoAction(string function)
         {
             IntPtr intPtr = NativeMethod.GetProcAddress(_hModule, function);
             _doAction = (DoAction)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(DoAction));


             return 0;
         }

         public void SetFunction(string key,int value)  
         {
             _setVariables[key](value);
         }
         public int Action()
         {
             return _doAction();
         }

     }

测试程序

class MainClass

{

     static void Main(string[] args)
     {
         int ret = 111;

         ret = CallWithTimeout2(FiveSecondMethodRet, 6000);
         Console.WriteLine(ret);

         ret = CallWithTimeout2(FiveSecondMethodRet, 4000);
         Console.WriteLine(ret);

         ret = CallWithTimeout2(DllTest, 7000);
         Console.WriteLine(ret);
         ret = CallWithTimeout2(DllTest, 1000);
         Console.WriteLine(ret);
         Console.Read();
     }


     static int FiveSecondMethodRet()
     {
         Thread.Sleep(5000);
         Console.WriteLine("FiveSecond");
         return 0;
     }

     static int DllTest()
     {
         var il = new InteropLib("CalcLib.dll");
         il.AddSettings("a", "SetA");
         il.AddSettings("b", "SetB");
         il.setDoAction("Add");
         il.SetFunction("a", 7);
         il.SetFunction("b", 2);

         int ret = il.Action();
         Console.WriteLine("ret = " + ret);
         return ret;
     }


     public delegate int DoHandler2();

     public static int CallWithTimeout2(DoHandler2 action, int timeoutSecond)
     {
         Thread threadToKill = null;
         DoHandler2 wrappedAction = () =>
         {
             threadToKill = Thread.CurrentThread;
             int ret = action();
             return ret;
         };
         IAsyncResult result = wrappedAction.BeginInvoke(null, null);
         if (result.AsyncWaitHandle.WaitOne(timeoutSecond))
         {
             return wrappedAction.EndInvoke(result);
         }
         else
         {
             threadToKill.Abort();
             return 222;

         }
     }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • hxycsdn9159
    hxycsdn9159 2020-06-28 10:39

    在你使用完这个dll之后调用NativeMethod.FreeLibrary( _ hModule)

    点赞 评论

相关推荐