2011-10-30 76 views
1

我想用C#編寫DMX Lightcontrol軟件。我的問題是,我必須重寫Delphi到C#的DLL調用。下面的代碼顯示了我的嘗試:在c#中重寫Delphi DLL-調用

//德爾福代碼:

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返回的字符指針轉換( )to a字符串

感謝您的幫助!

+1

你有這個: http://stackoverflow.com/questions/508227/how-to-import-const-char-api-to-c 聽起來完美的你... – Stef

回答

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); 
+0

@pbcoder:我剛剛編輯了我碼。讓我知道它是否工作... – Marco

+0

public static extern StringBuilder GetDMXInterface();確實有用!謝謝! –

+0

您編輯的代碼版本不起作用! –