C# 調(diào)用Delphi dll 實(shí)例代碼
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#實(shí)現(xiàn)多個(gè)計(jì)時(shí)器記錄不同定時(shí)時(shí)間
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)多個(gè)計(jì)時(shí)器記錄不同定時(shí)時(shí)間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12C#?BitArray(點(diǎn)矩陣)轉(zhuǎn)換成int和string的方法實(shí)現(xiàn)
BitArray?類(lèi)管理一個(gè)緊湊型的位值數(shù)組,它使用布爾值來(lái)表示,本文主要介紹了C#?BitArray(點(diǎn)矩陣)轉(zhuǎn)換成int和string的方法實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2022-05-05Unity shader實(shí)現(xiàn)多光源漫反射以及陰影
這篇文章主要為大家詳細(xì)介紹了shader實(shí)現(xiàn)多光源漫反射以及陰影,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07C#使用LINQ查詢(xún)表達(dá)式的基本子句總結(jié)
這篇文章主要介紹了C#使用LINQ查詢(xún)表達(dá)式的基本子句總結(jié),在C#程序中我們可以使用LINQ基本查詢(xún)表達(dá)式模式來(lái)查詢(xún)和轉(zhuǎn)換SQL數(shù)據(jù)庫(kù)、ADO.NET數(shù)據(jù)集、XML文檔和流以及.NET集合中的數(shù)據(jù),需要的朋友可以參考下2016-03-03C#使用迭代器實(shí)現(xiàn)文字動(dòng)態(tài)效果的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#如何通過(guò)使用迭代器實(shí)現(xiàn)文字動(dòng)態(tài)效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02通過(guò)VS中的數(shù)據(jù)源選擇對(duì)話(huà)框簡(jiǎn)單實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接配置[圖]
通過(guò)VS中的數(shù)據(jù)源選擇對(duì)話(huà)框簡(jiǎn)單實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接配置[圖]...2007-03-03