http://www.4ucode.com/Study/Topic/1996448VS2008中創建DLL工程
文件->新建->項目->visual c++->win32->win32控制臺應用程序(win32項目也可以)
填寫項目名稱MyDLL->確定->下一步->DLL(附加選項 對空項目打鉤)->完成。
到這里DLL工程就創建完畢了,下面新建兩個文件testDLL.cpp和testDLL.h。
C++ .h 文件
#define DLL_EXPORT __declspec(dllexport)
extern "C" DLL_EXPORT int MyMinus(int a,int b);
.cpp 文件
// testDLL.cpp : 定義 DLL 應用程序的導出函數。
//
#include "stdafx.h"
#include "testDLL.h"
int MyMinus(int a,int b)
{
return (a-b);
}
delphi調用代碼
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
edt1: TEdit;
edt2: TEdit;
lbl1: TLabel;
lbl2: TLabel;
lbl3: TLabel;
btn1: TButton;
lbl4: TLabel;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
const
TestDll = 'testDLL.dll';
function MyMinus(a:Integer;b:Integer):Integer;cdecl;external TestDll;
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
begin
lbl4.Caption := IntToStr(MyMinus(StrToInt(edt1.Text),StrToInt(edt2.Text)));
end;
end.