Checking safety of Delphi DLL to use in C#

2019-06-10 19:46发布

问题:

I have a DLL which takes care of custom drawing for some special glass effects. I'm putting it in a DLL for three reasons: 1) So it can be easily re-used and distributed of course without weighing its host app down, 2) So I can distribute it to developers without them knowing how it works, and 3) So it can be used from C#. It currently works in Delphi, but I know I will need to do many changes to make it support C#. For example, the main DLL function includes 1 parameter (a Record) which contains a number of types I know won't work in C# (like String, and maybe TColor). Project isn't quite 100% done yet, but is working.

I need someone to point out the easiest approach to accomplishing this. The code is too large to post it all here, so here it is at Pastebin.

Here's what I need to know:

  • Should I keep using Records as I am, or use something else like Packed Record?
  • Any tricks to use something other than String or PChar in these Records?
  • How would I wrap this DLL in C#? (I know very little C# by the way)
    • How to define equivalent records to pass to DLL function?
    • How to define equivalent constants in C#? (C# version of JDGlassCommon.pas)
    • How to get canvas handle (HDC) and parent handle (HWND) to send to DLL?
    • What would be equivalent to TColor?
  • Is it safe to pass types such as TColor in the Records?
  • Do you foresee any other issues in my code?

File List:

Library: JDGlassLib.dll *

  • Unit: JDGlassCommon.pas *

Package: JDLib.bpl

  • Unit: JDGlassCommon.pas *
  • Unit: JDGlass.pas *

Program: JDLibTestApplicationD7.exe

  • Form: JDLibTestAppD7.dfm *
  • Unit: JDLibTestAppD7.pas *

    (* = code is included in above link)

    (JDGlassCommon.pas is shared in both DLL and Component)

Should look something like this:

NOTE: I'm not asking for a re-write, although you're more than welcome to. I just need some tips on how to approach this.


PS: Original glass drawing code credited to "NGLN" of StackOverflow answering a prior question of mine: Delphi custom drawing - glowing glass

回答1:

Should I keep using Records as I am, or use something else like Packed Record?

Records are good for interop. Don't pack them, that just makes interop harder.

Any tricks to use something other than String or PChar in these Records?

Don't use string. That's Delphi only and even specific to Delphi versions. PChar is fine for interop. Sometimes it can be simplest to use fixed length inline char arrays in records. It depends on the use.

How would I wrap this DLL in C#?

Call it from C# using p/invoke.

Is it safe to pass types such as TColor in the Records?

Yes that's easy to work with. Make sure it's a true RGB color rather than a special color like clWindow.

Do you foresee any other issues in my code?

The glass rendering may well be incompatible with the rendering used by the C# libraries. It could very well depend on whether or not your C# code uses WinForms or WPF. In fact you may well find that the C# developers would find it easier to use native C# code. I expect glass rendering is well supported in the common C# GUI frameworks.