Delphi调用VC++6.0编写的Dll

这篇博客介绍了如何在Delphi中调用使用VC++6.0编写的名为ff.Dll的动态链接库,该库包含一个减法函数subtract(int a, int b)。博客详细展示了VC++6.0中Dll的源代码,并在Delphi中创建一个简单应用程序,通过外部调用来实现减法操作。" 119576630,11323129,Ansible自动化运维基础教程,"['linux', '自动化运维', '配置管理', 'Ansible']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用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目录下运行即可。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值