开发者

How to protect an imported Win32 DLL into a .NET application from memory issues

开发者 https://www.devze.com 2022-12-30 13:37 出处:网络
I have a C# application that needs to use a legacy Win32 DLL.The DLL is almost its own application, it has dialogs, operations with hardware, etc.When this DLL is imported and used, there are a couple

I have a C# application that needs to use a legacy Win32 DLL. The DLL is almost its own application, it has dialogs, operations with hardware, etc. When this DLL is imported and used, there are a couple of problems that occu开发者_如何转开发r:

  1. Dragging a dialog (not a Windows system dialog, but one created by the DLL) across the managed code application causes the UI to not repaint. Further it generates a system out of memory exception from various UI controls.
  2. The performance is incredibly slow.
  3. There seems to be no way to unload the DLL so the memory never gets cleaned up. When we close our managed application, we get another memory exception.

At the moment we import each method call as such:

[DllImport("dllname.dll",
    EntryPoint = "MethodName", SetLastError = true,
    CharSet = CharSet.Auto, ExactSpelling = true,
    CallingConvention = CallingConvention.StdCall)]


I would create an exe wrapper (possibly unmanaged) that exposes an API for your new application to use.

Another possible solution is to create a second UI thread that just handles the troublesome DLL. I lean more towards the exe wrapper, though, because that approach deals with OOM more gracefully (you can restart the process if necessary).

0

精彩评论

暂无评论...
验证码 换一张
取 消