Nerd610.10.2, thank you for the code snippet, it gets me starting, while I would have been too lazy to do the whole research myself...
I tried to improve it (I think you got lost with pointer handling), but I keep getting error 1043: Bad handshake. It seems it has something to do with hashing of passwords, different when going to versions 4.1.0 and up.
Well, I give my code snippet, you might try to run it on your system or see if something is wrong (it can be my install of MySQL, done by EasyPHP).
Code:
appTitle = MySQL Test
hModule := DllCall("LoadLibrary"
, "Str", "C:\Program Files\EasyPHP\php\libmySQL.dll")
; Snip! See below...
Kerry, you might be interested by mysql_ping() function too.
Oh, just before sending this message, I checked a
VB wrapper for MySQL. It has the good idea to include a libmySQL.dll which works for me!
Excellent, plus I get a list of values for constants.
It is strange anyway, as PHP works on my localhost... Oh well, let's go on.
Code:
appTitle = MySQL Test
hModule := DllCall("LoadLibrary"
, "Str", ".\libmySQL.dll")
If (hModule = 0)
{
MsgBox 16, %appTitle%, Can't load libmySQL.dll
ExitApp
}
mySQL := DllCall("libmySQL.dll\mysql_init"
, "UInt", 0)
If (mySQL = 0)
{
MsgBox 16, %appTitle%, No enough memory to connect to MySQL
ExitApp
}
connection := DllCall("libmySQL.dll\mysql_real_connect"
, "UInt", mySQL
, "Str", "localhost" ; host name
, "Str", "upl" ; user name
, "Str", "uplp" ; password
, "Str", "s9y" ; database name
, "UInt", 3306 ; port
, "UInt", 0 ; unix_socket
, "UInt", 0) ; client_flag
If (connection = 0)
{
errorMsg = Cannot connect to database
Goto HandleMySQLError
}
serverVersion := DllCall("libmySQL.dll\mysql_get_server_info"
, "UInt", mySQL
, "Str")
MsgBox % "Ping database: " . DllCall("libmySQL.dll\mysql_ping"
, "UInt", mySQL) . "`nServer version: " . serverVersion
resultString := MySQL_ProcessQueryWithResults(mySQL, "SHOW TABLES")
MsgBox Tables in chosen database:`n%resultString%
resultString := MySQL_ProcessQueryWithResults(mySQL, "SELECT * FROM s9y_config LIMIT 0, 30")
MsgBox Config:`n%resultString%
Return
HandleMySQLError:
errorCode := DllCall("libmySQL.dll\mysql_errno"
, "UInt", mySQL)
errorStr := DllCall("libmySQL.dll\mysql_error"
, "UInt", mySQL
, "Str")
MsgBox 16, %appTitle%, %errorMsg% (%errorCode%):`n%errorStr%
ExitApp
GetUIntAtAddress(_addr, _offset)
{
local addr
addr := _addr + _offset * 4
Return *addr + (*(addr + 1) << 8) + (*(addr + 2) << 16) + (*(addr + 3) << 24)
}
; TODO: use a less brutal error handling...
MySQL_ProcessQueryWithResults(_mySQL, _query)
{
local resultString, result, requestResult, fieldCount
local row, lengths, length, fieldPointer, field
result := DllCall("libmySQL.dll\mysql_query"
, "UInt", _mySQL
, "Str", _query)
If (result != 0)
{
errorMsg = Error while running request:`n%request%`n
Goto HandleMySQLError
}
requestResult := DllCall("libmySQL.dll\mysql_store_result"
, "UInt", mySQL)
If (requestResult = 0)
{
errorMsg = Error while storing request result:`n%request%
Goto HandleMySQLError
}
fieldCount := DllCall("libmySQL.dll\mysql_num_fields"
, "UInt", requestResult)
; OutputDebug fc: %fieldCount%
Loop
{
row := DllCall("libmySQL.dll\mysql_fetch_row"
, "UInt", requestResult)
If (row = 0)
Break
; Get a pointer on a table of lengths (unsigned long)
lengths := DllCall("libmySQL.dll\mysql_fetch_lengths"
, "UInt", requestResult)
; OutputDebug r: %row% / ls: %lengths%
Loop %fieldCount%
{
length := GetUIntAtAddress(lengths, A_Index - 1)
fieldPointer := GetUIntAtAddress(row, A_Index - 1)
; OutputDebug l: %length% / fp: %fieldPointer%
VarSetCapacity(field, length)
DllCall("lstrcpy", "Str", field, "UInt", fieldPointer)
resultString := resultString . field
If (A_Index < fieldCount)
resultString := resultString . " | "
}
resultString := resultString . "`n"
}
Return resultString
}