2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MASM32でdllを作る その2

Last updated at Posted at 2022-02-01

#MASM32でdllを作る

dlltest.asm
comment *
ml /c /coff dlltest.asm
link /dll dlltest.obj
*

.386
.model flat

.code

DllEntry proc stdcall hinstDLL:DWORD, fdwReason:DWORD, lpReserved:DWORD
	mov eax, 1
	ret
DllEntry endp

myadd proc stdcall export a:DWORD, b:DWORD
	mov eax, a
	add eax, b
	ret
myadd endp

mysub proc stdcall export a:DWORD, b:DWORD
	mov eax, a
	sub eax, b
	ret
mysub endp

end DllEntry
dev.bat
@echo off
path %path%;C:\masm32\bin
path %path%;C:\Windows\Microsoft.NET\Framework\v4.0.30319
cmd

#C#でテスト
環境:Windows 10 Pro (64bit)
x86版のcsc.exeにpathを通す。

dlltest.cs
// csc /platform:x86 dlltest.cs

using System;
using System.Runtime.InteropServices;

class Program {
	[DllImport("dlltest.dll")]
	static extern int myadd(int a, int b);
	[DllImport("dlltest.dll")]
	static extern int mysub(int a, int b);

	static void Main() {
		Console.WriteLine("add = {0}", myadd(1, 2));
		Console.WriteLine("sub = {0}", mysub(1, 2));
	}
}

##実行例
m.png

#MASM32でテスト
dlltest.dll、dlltest.libをカレントディレクトリに用意する。

dllclt.asm
comment *
ml /c /coff dllclt.asm
link /subsystem:console dllclt
*

includelib kernel32.lib
includelib user32.lib
includelib dlltest.lib

.386
.model flat, stdcall
option casemap:none

include windows.inc
include kernel32.inc
include user32.inc

mysub proto stdcall :DWORD, :DWORD

.const
fmt	db	'sub = %d', 0dh, 0ah, 0

.data?
hStdOut	dd	?
written	dd	?
len	dd	?
buf	db	1024 dup (?)

.code
start:
	invoke	GetStdHandle, STD_OUTPUT_HANDLE
	mov	hStdOut, eax
	invoke	mysub, 1, 2
	invoke	wsprintf, addr buf, addr fmt, eax
	mov	len, eax
	cld
	invoke	WriteConsole, hStdOut, addr buf, len, addr written, NULL
	invoke	ExitProcess, 0

end start
dev.bat
@echo off
path %path%;C:\masm32\bin
set INCLUDE=C:\masm32\include
set LIB=C:\masm32\lib
cmd

##実行例
m.png

#参考
http://www.interq.or.jp/chubu/r6/masm32/tute/tute017_Jp.html

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?