Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4625380
  • 博文数量: 671
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 7310
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-14 09:56
文章分类

全部博文(671)

文章存档

2011年(1)

2010年(2)

2009年(24)

2008年(271)

2007年(319)

2006年(54)

我的朋友

分类:

2007-04-21 15:36:59

作者:无域暖流

1、首先在RAdASM下编写DllTest.asm文件如下:

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 使用 nmake 或下列命令进行编译和链接:
; ml /c /coff DllTest.asm
; Link  /subsystem:windows /Dll /Def:Counter.def DllTest.obj
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  .386
  .model flat, stdcall
  option casemap :none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include 文件定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

;include user32.inc
;includelib user32.lib
;include kernel32.inc
;includelib kernel32.lib
include windows.inc
includelib DllTest.lib

  .code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; dll 的入口函数   不能少
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DllEntry  proc _hInstance,dwReason,_dwReserved
  mov eax,TRUE
  ret
DllEntry endp

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; dll 的导出函数
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_Add  proc _a,_b
  mov eax,_a
  add eax,_b  
  ret
_Add endp

  End DllEntry

2、制作DllTest.def文件导出DllTest.dll中要导出的函数:

LIBRARY DllTest
EXPORTS _Add @1

3、按照注释部分生成DllTest.dll文件和DllTest.lib文件。
 
 
 
调用DllTest.dll文件中的函数:
 
1、在汇编语言中调用:
 
汇编语言中调用要注意:
首先到如库文件  includelib DllTest.lib
在生命dll中的函数原型  _Add proto :DWORD,:DWORD
 
2、在Delphi语言中调用:
 
implementation
{$R *.dfm}
function _Add(a,b:integer):integer;stdcall;external 'dlltest.dll';
procedure TForm1.Button1Click(Sender: TObject);
var
  c:integer;
begin
  c:=_Add(2,3);
  ShowMessage(inttostr(c));
end;
 
3、在Visual C++中调用:
 
首先加入库文件:
 
#pragma comment(lib,"DllTest.lib")
 
extern "C" _declspec(dllimport) _Add(DWORD a,DWORD b);
 
void CTest2Dlg::OnButton1()
{
 // TODO: Add your control notification handler code here
 int c=_Add(2,3);
 CString str;
 str.Format("2+3=%d",c);
 AfxMessageBox(str); 
}
 
 
阅读(1461) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~