用c#重写Delphi DLL调用

时间:2011-10-30 09:02:10

标签: c# delphi dll pinvoke

我想用C#编写DMX Lightcontrol软件。我的问题是我要重写从Delphi到C#的DLL调用。以下代码显示了我的尝试:

// Delphi的码:

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

//我自己的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();

下一个问题如何将从GetDMXInterface()返回的char指针转换为String

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

尝试,但我不知道它是否有效,因为我无法测试它:

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

或尝试

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

然后

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