时间:2021-05-20
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;
// 返回结构体指针
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# 调用示例:
复制代码 代码如下:
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);
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#生成DLL文件的方法。分享给大家供大家参考。具体分析如下:VisualC#生成DLL文件VisualC、Delphi或者VB等编程语言来编写的
本文实例展示了C++与C#互调dll的实现步骤,在进行大型项目共享dll动态链接库中可以用到。具体方法如下:一、C#调用C++dll步骤(只能导出方法):1.c
Delphi用DLL实现插件的简单实例这是DLL的代码实现代码:libraryMyDll;usesSysUtils,Dialogs,Classes;proced
本文实例讲述了C#调用VB进行简繁转换的方法。分享给大家供大家参考。具体分析如下:首先在C#项目中引用Microsoft.VisualBasic.dll,版本自
本文实例讲述了C#编程调用Cards.dll实现图形化发牌功能。分享给大家供大家参考,具体如下:usingSystem;usingSystem.Collecti