views:

53

answers:

1

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 occur:

  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)]
+1  A: 

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).

Stephen Cleary
I considered the exe wrapper, but the unmanaged dll must be invoked in the same process as the managed app.
Eric
Why not in the same process? - users will not know if it is so
Mark
@Mark: exe wrappers are useful whenever you have a buggy component out of your control that crashes the entire process (e.g., with OOM). Restarting a subprocess on failure is not too difficult; I had to do this years ago with an Oracle client dll that kept crashing our 24x7 factory automation system. The users don't see any difference but it makes a big difference to the OS (different exe has its own memory space, and won't crash the parent exe).
Stephen Cleary
Stephen I was agreeing with you for those reasons - my question was to @Eric
Mark
It is because both the managed app and the unmanaged dll need access to the same com objects
Eric