How to use in C# function from Win32 DLL file made in Delphi. When function parameters are custom delphi objects?
Function definition in Delphi:
function GetAttrbControls( Code : PChar;
InputList: TItemList;
var Values : TValArray): Boolean; stdcall; export;
Types that use:
type
TItem = packed record
Code : PChar;
ItemValue: Variant;
end;
TItemList = array of TItem;
TValArray = array of PChar;
Example in C# (doesn’t work):
[StructLayout(LayoutKind.Sequential)]
public class Input
{
public string Code;
public object ItemValue;
};
[DllImport("Filename.dll", EntryPoint = "GetValues", CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern bool GetValues(string Code, Input[] InputList, ref StringBuilder[] Values);
This can’t be done your way, bud you have still some possibilities.
Dynamic
array(declared w/o []),string(AnsiString) andVariantare pointers to “magic” structures (they have reference count and other data on negative offsets), which are handled by compiler intrinsic.If you really want to use these types, you’ll need to serialize and materialize them around the interface (using some binary dump format, JSON, etc).
You may try to use any of the basic types (eg.
array[],ShortString,record) which will work exactly as you expect (beware of the ShortString 1-based indexing with length stored at 0) usingStructLayout, unless you mix them with managed types.Also I’ve got some good experience using interfaces (
IInterface/IDispatchviaCOMInterOp) to directly pass object references between Delphi and C# code.You are limited to calling the interfaces’ methods, of course, but the interop layer can handle at least
WideString(nicely) and some kinds ofVariant(ugly) for you.