Assembly 如何在Windows上将sqlite3与masm32一起使用
因为我必须在汇编程序中编写一个访问Sqlite3数据库的小库,所以我开始搜索如何使用Sqlite3.dll。我在fasm中找到了一种方法(我不得不使用masm32,因为许多原因都无助于解决问题,这只是一种必要),方法是通过Assembly 如何在Windows上将sqlite3与masm32一起使用,assembly,sqlite,masm,masm32,Assembly,Sqlite,Masm,Masm32,因为我必须在汇编程序中编写一个访问Sqlite3数据库的小库,所以我开始搜索如何使用Sqlite3.dll。我在fasm中找到了一种方法(我不得不使用masm32,因为许多原因都无助于解决问题,这只是一种必要),方法是通过cinvoke并引用看起来不可用的库。 我最想知道的是,我是否可以在masm中做类似的事情,或者我是否必须通过GetProcAddress获得我需要单独调用的每个函数的地址,为什么不呢?它很简单,如下所示: .486 .model flat, stdcall option ca
cinvoke
并引用看起来不可用的库。我最想知道的是,我是否可以在masm中做类似的事情,或者我是否必须通过
GetProcAddress
获得我需要单独调用的每个函数的地址,为什么不呢?它很简单,如下所示:
.486
.model flat, stdcall
option casemap:none
include \masm32\include\masm32.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include sqlite3.inc
.data
szSQLDB db "MyDB.db3", 0
szRandQuery db "SELECT * FROM Quit ORDER BY RANDOM() LIMIT 1;", 0
.data?
hDBase dd ?
.code
START:
invoke sqlite3_open, offset szSQLDB, offset hDBase
call GetQuitMsg
invoke sqlite3_close, hDBase
invoke ExitProcess, 0
GetQuitMsg proc
local ppStmt
invoke sqlite3_prepare_v2, hDBase, offset szRandQuery, -1, addr ppStmt, 0
invoke sqlite3_step, ppStmt
invoke sqlite3_data_count, ppStmt
.if eax !=0
invoke sqlite3_column_text, ppStmt, 0
invoke MessageBoxA, 0, eax, 0,0
.endif
ret
GetQuitMsg endp
end START
我使用makefile,Geany作为我的编辑器。zip包括测试数据库sqlite3.inc,但不包括sqlite3.dll。只要解压缩文件,将sqlite3.dll放到项目目录中,就可以了。我也不使用MS链接,而是使用GoLink,因为它不需要import libs,但是如果必须使用MS链接,我在附近的某个地方有一个SQLite导入库
萨拉姆
这是我的示例代码:)
它将创建名为“file.db”的数据库文件,创建名为“DBI”的表,并插入多达三行的数据。插入数据三次后,它将使用MessageBoxA显示数据。nID作为标题,szName作为文本
享受我的代码的乐趣:)
请将INCLUDE、LIB和BIN环境添加到MASM安装路径
例如:
INCLUDE=C:\MASM32\INCLUDE
LIB=C:\MASM32\LIB
BIN=C:\MASM32\BIN
在命令提示下键入set时,请确保上面有路径
瓦萨拉姆
.386
.model flat, stdcall
option casemap:none
include windows.inc
include advapi32.inc
include comctl32.inc
include kernel32.inc
include shell32.inc
include user32.inc
includelib advapi32.lib
includelib comctl32.lib
includelib kernel32.lib
includelib shell32.lib
includelib user32.lib
.const
SQLITE_ROW equ 100
.data?
dwResult dd ?
hDB dd ?
sqlite3_close dd ?
sqlite3_column_text dd ?
sqlite3_exec dd ?
sqlite3_open dd ?
sqlite3_prepare dd ?
sqlite3_step dd ?
.data
szSQLite3Lib db "sqlite3.dll", 0h
szfnSQLite3_close db "sqlite3_close", 0h
szfnSQLite3_column_text db "sqlite3_column_text", 0h
szfnSQLite3_exec db "sqlite3_exec", 0h
szfnSQLite3_open db "sqlite3_open", 0h
szfnSQLite3_prepare db "sqlite3_prepare", 0h
szfnSQLite3_step db "sqlite3_step", 0h
szDBFile db "file.db", 0h
szSQLStmt1 db "create table DBI (nID integer primary key, szName text)", 0h
szSQLStmt2 db "insert into DBI (nID, szName) values (1, 'RizonBarns')", 0h
szSQLStmt3 db "insert into DBI (szName) values ('Rizon & Barns')", 0h
szSQLStmt4 db "insert into DBI (szName) values ('MASM32')", 0h
szSQLStmt5 db "select * from DBI", 0h
.code
main:
push offset szSQLite3Lib
call LoadLibraryA
cmp eax, 0h
je @ERROR
push offset szfnSQLite3_close
push eax
call GetProcAddress
mov sqlite3_close, eax
push offset szSQLite3Lib
call LoadLibraryA
push offset szfnSQLite3_column_text
push eax
call GetProcAddress
mov sqlite3_column_text, eax
push offset szSQLite3Lib
call LoadLibraryA
push offset szfnSQLite3_exec
push eax
call GetProcAddress
mov sqlite3_exec, eax
push offset szSQLite3Lib
call LoadLibraryA
push offset szfnSQLite3_open
push eax
call GetProcAddress
mov sqlite3_open, eax
push offset szSQLite3Lib
call LoadLibraryA
push offset szfnSQLite3_prepare
push eax
call GetProcAddress
mov sqlite3_prepare, eax
push offset szSQLite3Lib
call LoadLibraryA
push offset szfnSQLite3_step
push eax
call GetProcAddress
mov sqlite3_step, eax
push 255
push GPTR
call GlobalAlloc
mov hDB, eax
lea edx, hDB
push edx
push offset szDBFile
call sqlite3_open
push 0h
push 0h
push 0h
push offset szSQLStmt1
push hDB
call sqlite3_exec
push 0h
push 0h
push 0h
push offset szSQLStmt2
push hDB
call sqlite3_exec
push 0h
push 0h
push 0h
push offset szSQLStmt3
push hDB
call sqlite3_exec
push 0h
push 0h
push 0h
push offset szSQLStmt4
push hDB
call sqlite3_exec
push 0h
lea eax, dwResult
push eax
push offset szSQLStmt5
call lstrlenA
push eax
push offset szSQLStmt5
push hDB
call sqlite3_prepare
@@:
push dwResult
call sqlite3_step
cmp eax, SQLITE_ROW
jne @F
push 0h
push dwResult
call sqlite3_column_text
mov esi, eax
push 1h
push dwResult
call sqlite3_column_text
mov edi, eax
push 0h
push esi
push edi
push 0h
call MessageBoxA
jmp @B
@@:
push hDB
call sqlite3_close
@ERROR:
xor eax, eax
push eax
call ExitProcess
end main