Jump to content

Array of Strings and DllStruct


wegstar
 Share

Recommended Posts

Hello,

I am currently making a mysql UDF which directly calls functions in libmysql.dll. However, I am having trouble handling an array of strings (char**, char*[], char [][]) that is returned by the mysql_fetch_row() function.

As you can see in my code, I've figured out how to deal with returned char* strings...:

CODE
Func mysqlQuery($query)

$queryprep = DllStructCreate("char[" & (StringLen($query)+1) & "]")

DllStructSetData($queryprep, 1, $query)

$queryptr =DllStructGetPtr($queryprep, 1)

# we'll add on 100 to be sure

$bufferprep = DllStructCreate("char[" & (StringLen($query)+100) & "]")

$buffer =DllStructGetPtr($bufferprep, 1)

Local $temp = DllCall($dll, "int", "mysql_real_escape_string", "ptr", $fd, "ptr", $buffer, "ptr", $queryptr, "int", StringLen($query)+1)

; now, we have the real size, so fix up the data

; I wonder why I have to subtract 1 to get rid of extra stuff at end of string...

$bufferfix = DllStructCreate("char[" & ($temp[0]-1) & "]")

; copy string over, which will truncate extra 100

DllStructSetData($bufferfix, 1, DllStructGetData($bufferprep, 1))

However, I am unable to 'process' char** array of strings. Here is what I have tried so far:

CODE
Func mysqlFetchRow()

Local $temp = DllCall($dll, "ptr", "mysql_fetch_row", "ptr", $res)

MsgBox(1, "hmm", $temp[0])

$datastruct = DllStructCreate($temp[0]) ; $temp is char**

MsgBox(1, "hmm", DllStructGetData($datastruct, 1))

Local $datatemp = DllStructCreate(DllStructGetData($datastruct, 1))

MsgBox(1, "hmm", DllStructGetData($datatemp, 1))

EndFunc

As you can see, I assumed that DllStructGetData would form an array of char* pointers accessible by by DllStructGetData($datastruct, 1-2-3..etc.). But this does not seem to be the case.

Any ideas how to implement this? Thanks in advance.

Link to comment
Share on other sites

CODE
Func mysqlFetchRow()

Local $temp = DllCall($dll, "ptr", "mysql_fetch_row", "ptr", $res)

MsgBox(1, "hmm", $temp[0])

$datastruct = DllStructCreate($temp[0]) ; $temp is char**

MsgBox(1, "hmm", DllStructGetData($datastruct, 1))

Local $datatemp = DllStructCreate(DllStructGetData($datastruct, 1))

MsgBox(1, "hmm", DllStructGetData($datatemp, 1))

EndFunc

As you can see, I assumed that DllStructGetData would form an array of char* pointers accessible by by DllStructGetData($datastruct, 1-2-3..etc.). But this does not seem to be the case.

Any ideas how to implement this? Thanks in advance.

That doesn't make sense to me.

Local $temp = DllCall($dll, "ptr", "mysql_fetch_row", "ptr", $res)

You are expecting a ptr to be retured

$datastruct = DllStructCreate($temp[0]) ; $temp is char**;- no, it's a pointer, but anyway you don't create a struct like that

what you mean here is perhaps

$datastruct = DllStructCreate('ptr',$temp[0])

But if you knew there were 3 items you could say

$datastruct = DllStructCreate('ptr[3]',$temp[0])

But I would expect that the return is a pointer to the sructure for the array of strings. What you need to find out is how the structure is formed so you can read it. Presumably this is well known but not by me I'm afraid. If I couldn't find out I would read the memory location starting at $Temp[0] and see if I could understand it. The structure could be an array of strings, an array of pointers to strings (which is what I think you expect, but how do you know how many elements?), a value of the number of strings followed by pointers etc etc.

So what I think will be needed is

DllStructCreate($Structure,$Temp[0]) ;once you know the structure

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

here's a concept that should work...

#include<sqlite.au3>
$ppszRow = ;mysql_fetch_row(res)
$uiLen = ;mysql_num_fields(res)
dim $aRow[$uiLen],$vPointers = DllStructCreate("ptr[" & $uiLen & "]",$ppszRow)
Do
   $uiLen -= 1
   $aRow[$uiLen] = _szStringRead(DllStructGetData($vPointers,1,$uiLen+1))
Until $uiLen = 0


Func _szStringRead($pszString, $uiLen = -1)
    Local $aStrLen, $vszString
    If $pszString = 0 Then Return ""
    If $uiLen < 1 Then
        $aStrLen = DllCall("msvcrt.dll", "int:cdecl", "strlen", "ptr", $pszString)
        If @error Then Return SetError(1, 0, "")
        $uiLen = $aStrLen[0] + 1
    EndIf
    $vszString = DllStructCreate("char[" & $uiLen & "]", $pszString)
    If @error Then Return SetError(2, 0, "")
    Return SetError(0, $iLen, DllStructGetData($vszString, 1))
EndFunc
Edited by piccaso
CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...