C# 调用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;

  // 返回结构体指针
  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); } }

 

转载于:https://www.cnblogs.com/csMapx/p/csharp_delphi_dll.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi和C都是不同的编程语言,它们各自使用不同的编译器和运行时环境。由于语言和环境的差异,Delphi调用C代码时需要一些额外的步骤。 要在Delphi调用C代码,首先需要编写一个适当的接口来与C代码进行交互。这通常包括定义C函数的名称、参数和返回类型。接口可以使用Delphi的外部函数声明来实现。 在Delphi中,必须使用external关键字来声明外部函数。这样Delphi编译器就知道这个函数是在外部编写的,需要通过导入库来访问它。在外部函数声明中,需要指定C函数的名称、参数和返回类型,以便与C代码进行匹配。 为了在Delphi调用C代码,还需要将C代码编译为动态链接库(DLL)或共享库,以便Delphi可以在运行时加载和调用其中的函数。在Delphi中使用LoadLibrary函数加载这个库,并使用GetProcAddress函数获取其中的函数地址。 一旦C函数的地址被获取,就可以使用Delphi中的函数指针类型来声明一个相应的函数指针变量。然后,可以通过这个指针调用C函数,将Delphi中的数据传递给C代码,执行相应的操作,并从C代码接收返回的结果。 总之,要在Delphi调用C代码,必须编写适当的接口来声明C函数,并将C代码编译为动态链接库。然后,可以通过加载库和获取函数地址的方式来调用C函数,并与Delphi中的数据进行交互。这样,我们可以实现Delphi和C之间的无缝集成和互操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值