用VC++6.0编写了一个简单的dll,里面包含一个减法函数subtract(int a,int b),Dll命名为ff.Dll
代码如下:
1.ff.cpp:
// ff.cpp : Defines the entry point for the DLL application.
//
#include "StdAfx.h"
#include "ff.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This is an example of an exported variable
FF_API int nFf=0;
// This is an example of an exported function.
FF_API int fnFf(void)
{
return 42;
}
// This is the constructor of a class that has been exported.
// see ff.h for the class definition
CFf::CFf()
{
return;
}
FF_API int subtract(int a,int b)
{
return (a-b);
}
2.StdAfx.cpp:
// stdafx.cpp : source file that includes just the standard includes
// ff.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
#ifdef FF_EXPORTS
#define FF_API __declspec(dllexport)
#else
#define FF_API __declspec(dllimport)
#endif
// This class is exported from the ff.dll
class FF_API CFf {
public:
CFf(void);
// TODO: add your methods here.
};
extern FF_API int nFf;
FF_API int fnFf(void);
extern "C" FF_API int subtract(int a,int b);
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
3.ff.h:
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the FF_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// FF_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
#ifdef FF_EXPORTS
#define FF_API __declspec(dllexport)
#else
#define FF_API __declspec(dllimport)
#endif
// This class is exported from the ff.dll
class FF_API CFf {
public:
CFf(void);
// TODO: add your methods here.
};
extern FF_API int nFf;
FF_API int fnFf(void);
extern "C" FF_API int subtract(int a,int b);
4.StdAfx.h:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__D7F89C55_C2D5_4293_BD70_60C04F519B71__INCLUDED_)
#define AFX_STDAFX_H__D7F89C55_C2D5_4293_BD70_60C04F519B71__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Insert your headers here
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
// TODO: reference additional headers your program requires here
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__D7F89C55_C2D5_4293_BD70_60C04F519B71__INCLUDED_)
Delphi中:
unit M;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Label3: TLabel;
Edit2: TEdit;
Label4: TLabel;
Edit3: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//function add(a: integer; b: integer): Integer; cdecl; external 'ee.dll';
function subtract(a: integer; b: integer): Integer; cdecl; external 'ff.dll';
//function MaxNum(Num1,Num2:integer):integer;stdcall;external'Max.dll' name 'MaxNum';
procedure TForm1.Button1Click(Sender: TObject);
var
N1,N2:integer;
MX:integer;
begin
N1:=strtoint(Edit1.Text);
N2:=strtoint(Edit2.Text);
MX:=subtract(N1,N2);
Edit3.Text:=inttostr(MX);
end;
end.
将编译好的ff.dll复制到delphi的project目录下运行即可。