Search code examples
c#delphidllpinvoke

Rewrite Delphi DLL-calls in c#


I want to write a DMX Lightcontrol software in C#. My problem is that I've to rewrite the DLL-calls from Delphi to C#. Following code shows my attempts:

//Delphi-Code:

function GetDMXInterface: pchar; stdcall; external 'DMX510.dll';
function SetLevel(a: array of byte): boolean; stdcall; external 'DMX510.dll';
function GetMaxChannels: integer; external 'DMX510.dll';

//My own C#-Code:

[DllImport("DMX510.DLL")]
public static extern char* GetDMXInterface();
[DllImport("DMX510.DLL")]
public static extern Boolean SetLevel(Byte[] bytearray);
[DllImport("DMX510.DLL")]
public static extern int GetMaxChannels();

Next question how to convert the char pointer returned from GetDMXInterface() to a String

Thanks for your help!


Solution

  • Try, but I don't know if it works because I cannot test it:

    [DllImport("DMX510.DLL")]
    public static extern StringBuilder GetDMXInterface();
    

    Or try

    [DllImport("DMX510.DLL", CharSet = CharSet.Unicode, 
     CallingConvention = CallingConvention.StdCall)]
    public static extern IntPtr GetDMXInterface();
    

    and then

    IntPtr ptr = GetDMXInterface(); 
    string msg = Marshal.PtrToStringAuto(ptr);