C#调用DLL入门示例

简单地学习了一下静态调用,动态调用貌似有点麻烦,以后再慢慢研究。

C#调用DLL代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Csharp_Delphi_DLL_DEMO1
{
    public partial class Form1 : Form
    {
        [DllImport("dll1.dll", EntryPoint = "Triple")]
        static extern int Triple(int i);
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i = int.Parse(textBox1.Text);
            label1.Text = Convert.ToString(Triple(i));
        }
    }
}

须要加入命名空间:using System.Runtime.InteropServices;

DLL文件代码:

library dll1;

uses
  SysUtils,
  Classes,
  Dialogs;

{$R *.res}
 //建立过程
function Triple(N:Integer):integer;stdcall;
begin
       result:=N+3;
end;

//输出
exports
  Triple;

begin
end.