1

由于我必须在 Assembler 中编写一个访问 Sqlite3 数据库的小型库,因此我开始搜索如何使用 sqlite3.dll。我在 fasm 中找到了一种方法(我必须使用 masm32,原因有很多,但无助于解决问题,这只是必要的)通过cinvoke并引用看起来不可用的库。
我基本上想知道的是我是否有可能在 masm 中做类似的事情,或者我是否必须通过GetProcAddress.

4

2 回答 2

0

为什么不呢?很简单,简单如下:

.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,因为它不需要导入库,但如果您必须使用 MS 链接,我在此处某处有一个 SQLite 导入库。

http://www.gunnerinc.com/files/SQLiteTest.zip

于 2012-10-28T04:38:34.137 回答
0

萨拉姆,

这是我的示例代码:)

.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

它将创建名为“file.db”的数据库文件,创建名为“DBI”的表,并插入多达三行的数据。插入数据 3 次后,会以 MessageBoxA 显示数据。nID 作为标题,szName 作为文本。

玩得开心我的代码:),

请将 INCLUDE、LIB 和 BIN 环境添加到 MASM 安装路径。

例子:

包括 = C:\MASM32\包括

LIB = C:\MASM32\lib

BIN = C:\MASM32\bin

并确保在命令提示符下键入set时上面有路径。

瓦萨拉姆。

于 2013-04-27T18:23:28.437 回答