Since I have to write a small library in Assembler that accesses a Sqlite3 Database, I have started a search on how to use the sqlite3.dll. I have found a way to do that in fasm (I have to use masm32 for numerous reasons that do not contribute to solving the problem, it simply is a necessity) via cinvoke and referencing the library which is not available as it seems.
What I would basically like to know is whether it is possible for me to do a similar thing in masm or if I have to get the Addresses of every function I need to call individually via GetProcAddress.
2 Answers
Why wouldn't it? It is simple, as simple as the following:
.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
I use a makefile, and Geany as my editor. The zip includes the test db, sqlite3.inc, but not the sqlite3.dll just unzip files, plop the sqlite3.dll into project directory and you will be good to go. I also do not use the MS link, instead I use GoLink as it does not need import libs, but if you have to use the MS Link, I have a SQLite import library around here somewhere.
Salam,
It's my example code :)
.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
It will create database file with name 'file.db', create table named 'DBI', and insert data as many three rows. After insert data three times, it will show the data with MessageBoxA. nID as caption and szName as text.
Have fun with my code :),
Please add INCLUDE, LIB, and BIN environment to path of MASM installation.
Example:
INCLUDE = C:\MASM32\include
LIB = C:\MASM32\lib
BIN = C:\MASM32\bin
And make sure there are paths above when you type set on command prompt.
Wassalam.