2012-08-13 43 views
1

由於我必須在彙編器中編寫一個訪問Sqlite3數據庫的小型庫,因此我開始搜索如何使用sqlite3.dll。我已經找到了一種方法來做到這一點在fasm(我必須使用masm32的許多原因,這不會有助於解決問題,它只是一種必然)通過cinvoke和引用看起來不可用的庫。
我基本上想知道的是,我是否可以在masm中做類似的事情,或者我需要通過GetProcAddress單獨調用每個函數的地址。如何在Windows上使用sqlite3和masm32

回答

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

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' 表,然後將數據爲三行。插入數據三次後,它將顯示MessageBoxA的數據。作爲標題的nID和作爲文本的szName。

有樂趣我的代碼:),

請添加包括LIB和BIN環境MASM安裝的路徑。

例子:

INCLUDE = C:\ MASM32 \包括

LIB = C:\ MASM32 \ LIB

BIN = C:\ MASM32 \ BIN

並確保有當您在命令提示符處鍵入設置時,上面的路徑是。

Wassalam。

相關問題