nasm and golink create a DLL x86

制作DLL

xxx.asm:

%macro fb 0
	push ebp
	mov ebp,esp
%endmacro

%macro fa 1
	mov esp,ebp
	pop ebp
	ret %1
%endmacro

section .text
	global dllmain
	
dllmain:
	mov eax,1
	ret 12
   
f1:
	fb
	mov eax,[ebp+8]
	add eax,[ebp+12]
	fa 8

xxx_link.fil:

; > golink @xxx_link.fil
/entry dllmain
/dll 
/exports f1

xxx.obj
>nasm -f win32 xxx.asm
>golink @xxx_link.fil

制作dll后可以看下pe结构,免得的出问题

在x86汇编中调用

将xxx.dll拷贝到asm项目文件中

hello.asm:

extern MessageBoxA
extern ExitProcess
extern f1

section .data
	title   db "caption.",0
	message db "hello world....",0
	
section .text
	global main

main:
	push 1
	push 2
	call f1
	
	push 0
	push title
	push message
	push 0
	call MessageBoxA

_exit:
	push 0
	call ExitProcess
> nasm -f win32 hello.asm
> golink /entry main hello.obj kernel32.dll user32.dll xxx.dll

c++ 显示连接

将xxx.dll拷贝到c++项目文件中

#include <iostream>
#include <Windows.h>

typedef int (CALLBACK* f1_t)(int, int);

f1_t f1;

int main()
{
  HMODULE myDLL = LoadLibraryA("xxx.dll");
  f1 = (f1_t)GetProcAddress(myDLL, "f1");
  if (f1)
    printf("%d\n", f1(1, 2)); // 3
  else
    printf("dll not export f1");
  return 0;
}

See also:

posted @ 2020-09-19 15:13  Ajanuw  阅读(343)  评论(0编辑  收藏  举报