C# 調(diào)用Delphi dll 實例代碼
delphi dll 源碼:
library dllres;
type
char10 = array[0..9] of char;
TMydata = packed record
id: Integer;
name: char10;
married: Boolean;
salary: Double;
end;
PMydata = ^TMydata;
const
RESSTR: array[0..4] of string = ('HELLO', 'COLOR', 'DELPHI', 'shared', 'library');
NO_RESULT= 'no result';
var
mydata: TMydata;
{$R *.res}
// 返回字符串指針
function getResStr(aindex: Integer): PChar; stdcall;
begin
if aindex < Length(RESSTR) then
begin
Result := pchar(RESSTR[aindex]);
end
else
begin
Result := pchar(NO_RESULT);
end;
end;
// 返回結(jié)構(gòu)體指針
function getMydata: PMydata; stdcall;
begin
with mydata do
begin
id := 123;
name := 'obama';
married := false;
salary := 1200;
end;
Result := @mydata;
end;
exports getResStr, getMydata;
begin
end.
C# 調(diào)用示例:
class Invoke_Delphi_Dll_Exam
{
[DllImport("dllres.dll", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr getResStr(int index);
[DllImport("dllres.dll", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr getMydata();
public struct Mydata
{
public int id; //0
public string name; //4
public bool married; //24
public double salary; //25
public Mydata(byte[] data)
{
if (data != null && data.Length == 33) {
id = BitConverter.ToInt32(data, 0);
name = Encoding.Unicode.GetString(data, 4, 20).Replace("\0",""); // 去掉尾部的0字符
married = BitConverter.ToBoolean(data, 24);
salary = BitConverter.ToDouble(data, 25);
}
else {
id = 0;
name = String.Empty;
married = false;
salary = 0;
}
}
public override string ToString()
{
return String.Format("id: {0}, name: {1}, married: {2}, salary: {3}",
id, name, married, salary);
}
}
private static void Main(string[] args)
{
Console.WriteLine(Marshal.PtrToStringAuto(getResStr(0)));
byte[] data = new byte[33];
Marshal.Copy(getMydata(), data, 0, 33);
Mydata mydata = new Mydata(data);
Console.WriteLine(mydata);
}
}
相關(guān)文章
C#實例代碼之抽獎升級版可以經(jīng)表格數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫,抽獎設(shè)置,補抽
這篇文章主要介紹了C#實例代碼之抽獎升級版可以經(jīng)表格數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫,抽獎設(shè)置,補抽 的相關(guān)資料,需要的朋友可以參考下2016-01-01C#實現(xiàn)計算一個點圍繞另一個點旋轉(zhuǎn)指定弧度后坐標值的方法
這篇文章主要介紹了C#實現(xiàn)計算一個點圍繞另一個點旋轉(zhuǎn)指定弧度后坐標值的方法,涉及C#針對坐標的數(shù)學(xué)運算相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08C#使用SqlServer作為日志數(shù)據(jù)庫的設(shè)計與實現(xiàn)
這篇文章主要給大家介紹了關(guān)于C#使用SqlServer作為日志數(shù)據(jù)庫的設(shè)計與實現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01基于C#實現(xiàn)12306的動態(tài)驗證碼變成靜態(tài)驗證碼的方法
這篇文章主要介紹了基于C#實現(xiàn)12306的動態(tài)驗證碼變成靜態(tài)驗證碼的方法的相關(guān)資料,需要的朋友可以參考下2015-12-12