1

Sqlite3データベースにアクセスする小さなライブラリをアセンブラで作成する必要があるため、sqlite3.dllの使用方法の検索を開始しました。私はfasmでそれを行う方法を見つけました(問題の解決に貢献しない多くの理由でmasm32を使用する必要があります、それは単に必要です)、cinvoke見た目どおりに利用できないライブラリを参照して参照します。
基本的に知りたいのは、masmで同様のことを実行できるかどうか、またはを介して個別に呼び出す必要のあるすべての関数のアドレスを取得する必要があるかどうかですGetProcAddress

4

2 に答える 2

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 行分のデータを挿入します。データを 3 回挿入すると、MessageBoxA でデータが表示されます。キャプションとしての nID とテキストとしての szName。

私のコードを楽しんでください:)、

MASM インストールのパスに INCLUDE、LIB、および BIN 環境を追加してください。

例:

インクルード = C:\MASM32\インクルード

ライブラリ = C:\MASM32\lib

ビン = C:\MASM32\bin

コマンドプロンプトでsetと入力するときは、上記のパスがあることを確認してください。

ワッサラーム。

于 2013-04-27T18:23:28.437 に答える
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

私はメイクファイルを使用し、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 に答える