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

如何强制关闭运行的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 2020-06-28 10:39
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料