C#中呼叫Delphi寫的Dll時應當在Delphi程式中使用PAnsiChar型別字串,並且使用stdcall引數。這樣C#才可以正常呼叫。
示例程式碼
DELPHI中
function EncryVncPassStrHex(Str: PAnsiChar): PAnsiChar; stdcall;var StrResult, TempResult, Temp: String; I: Integer;begin TempResult := EncryVncPassStr(Str); StrResult := ""; for I := 0 to Length(TempResult) - 1 do begin Temp := Format("%x", [Ord(TempResult[I + 1])]); if Length(Temp) = 1 then Temp := "0" + Temp; StrResult := StrResult + Temp; end; Result := PChar(StrResult);end;
C#中:
[DllImport("des.dll", EntryPoint="EncryVncPassStrHex",CharSet=CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]public static extern string EncryVncPassStrHex( string Str);
C#中呼叫Delphi寫的Dll時應當在Delphi程式中使用PAnsiChar型別字串,並且使用stdcall引數。這樣C#才可以正常呼叫。
示例程式碼
DELPHI中
function EncryVncPassStrHex(Str: PAnsiChar): PAnsiChar; stdcall;var StrResult, TempResult, Temp: String; I: Integer;begin TempResult := EncryVncPassStr(Str); StrResult := ""; for I := 0 to Length(TempResult) - 1 do begin Temp := Format("%x", [Ord(TempResult[I + 1])]); if Length(Temp) = 1 then Temp := "0" + Temp; StrResult := StrResult + Temp; end; Result := PChar(StrResult);end;
C#中:
[DllImport("des.dll", EntryPoint="EncryVncPassStrHex",CharSet=CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]public static extern string EncryVncPassStrHex( string Str);