onlyou13

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

dll代码:

mydll.dpr

library mydll;

uses
  System.SysUtils,
  System.Classes,
  uFunction in 'uFunction.pas';

{$R *.res}

exports
  AddInt,
  AddStr,
  AddDouble,
  Add;

end.
uFunction.pas
unit uFunction;

interface

uses
  System.SysUtils, System.Classes, Winapi.Messages;


function AddInt(a1: Integer; a2: Integer): Integer; stdcall;
function AddStr(a1: PWideChar; a2: PWideChar): PWideChar; stdcall;
function AddDouble(a1: Double; a2: Double): Double; stdcall;
function Add(a1: Integer; a2: Integer): PWideChar; stdcall;

implementation

function AddInt(a1: Integer; a2: Integer): Integer; stdcall;
begin
  Result := a1 + a2;
end;

function AddStr(a1: PWideChar; a2: PWideChar): PWideChar; stdcall;
begin
  Result := PWideChar(string(a1) + string(a2));
end;

function AddDouble(a1: Double; a2: Double): Double; stdcall;
begin
  result:= a1 + a2;
end;

function Add(a1: Integer; a2: Integer): PWideChar; stdcall;
begin
  Result := PWideChar((a1 + a2).ToString);
end;

end.

 

delphi调用:

unit uMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

function AddInt(a1: Integer; a2: Integer): Integer; stdcall; external 'mydll.dll';
function AddStr(a1: PWideChar; a2: PWideChar): PWideChar; stdcall; external 'mydll.dll';
function AddDouble(a1: Double; a2: Double): Double; stdcall; external 'mydll.dll';
function Add(a1: Integer; a2: Integer): PWideChar; stdcall; external 'mydll.dll';

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(AddInt(5, 6).ToString);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage(string(AddStr('hello ', 'world')));
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  ShowMessage(AddDouble(5.1, 6.1).ToString);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  ShowMessage(string(Add(5, 6)));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Position := poScreenCenter;
end;

end.
c#调用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace hello_dll
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("mydll.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
        public static extern int AddInt(int a1, int a2);


        [DllImport("mydll.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr AddStr(string a1, string a2);


        [DllImport("mydll.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
        public static extern double AddDouble(double a1, double a2);


        [DllImport("mydll.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr Add(int a1, int a2);


        private void Form1_Load(object sender, EventArgs e)
        {
            StartPosition = FormStartPosition.CenterScreen;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(AddInt(5, 6).ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IntPtr ptr = AddStr("hello ", "world");
            string str = Marshal.PtrToStringUni(ptr);
            MessageBox.Show(str);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(AddDouble(5.1, 6.1).ToString());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            IntPtr ptr = Add(5, 6);
            string str = Marshal.PtrToStringUni(ptr);
            MessageBox.Show(str);
        }
    }
}

 

 

 

posted on 2019-09-27 11:10  onlyou13  阅读(258)  评论(0编辑  收藏  举报