c# – Connecting Delphi dll to C # project

Question:

There is a library CheckUfa.dll In the developer's help it is written that to connect the functions I need to the Delphi project like this:

function CheckFile(fname,reportName,typRep:PChar;fox:integer):integer; StdCall;
function CheckFile2(fname,typRep:PChar;Precheck:integer):WideString; StdCall;
function CheckFile3(fname,reportName,typRep:PChar;fox:integer):wideString; StdCall;

Changed LPStr, LPWStr. I removed and installed different MarshalAs. IntPtr. Nothing.

One example of what I've tried (with regards to CheckFile2):

[DllImport(@"D:\CheckUfa.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static extern IntPtr CheckFile2(
            [MarshalAs(UnmanagedType.LPWStr)]
            StringBuilder fname,
            [MarshalAs(UnmanagedType.LPWStr)]
            StringBuilder typRep,
            [MarshalAs(UnmanagedType.I4)]
            int Precheck);

...
StringBuilder sb1 = new StringBuilder(...
StringBuilder sb2 = new StringBuilder(...

return Marshal.PtrToStringUni(CheckFile2(sb1, sb2, 0));
...

Answer:

Thanks everyone for the help.

Let me summarize. The Delphi compiler converts a function into a procedure. That is why you need to use void in C #, and out the result variable.

PChar was successfully imported only into IntPtr, other types were with errors.

The CheckFile2 function I needed in the toga worked like this:

[DllImport(@"D:\CheckPfr\CheckUfa.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, EntryPoint = "#2")]

        public static extern void CheckFile2(
            [MarshalAs(UnmanagedType.BStr)]
            out string result,
            IntPtr fname,
            IntPtr typRep,
            [MarshalAs(UnmanagedType.I4)]
            int Precheck);

Note that fname is essentially a string, so I used Marshal when calling CheckFile2

CheckFile2(out result, Marshal.StringToBSTR(fname), Marshal.StringToBSTR(typRep), 0);
Scroll to Top