Sign in to follow this  
Followers 0
Kris123

Passing an array to a function by ref

22 posts in this topic

Hi,

How to pass an array to a function by reference and how to get the array elements. Thanks in advance.

Share this post


Link to post
Share on other sites



Share this post


Link to post
Share on other sites

I saw this help in AutoIT. But i want how to deal with arrays.

Thanks.

Share this post


Link to post
Share on other sites

Dim $MyArray[2]
;When using ByRef keyword, you're just passing a reference to the function and manipulating the actual array created earlier.
Blah($MyArray)
MsgBox(0, $MyArray[0], $MyArray[1])

Func Blah(ByRef $MyArrayRef)
Local $Dali = 2
$MyArrayRef[0] = "Hello"
$MyArrayRef[1] = $Dali
EndFunc

Share this post


Link to post
Share on other sites

Thanks for your reply.

But here in my script, i should send an array and also array length (both are by reference)to an API which is exposed in a DLL.

Thanks.

Share this post


Link to post
Share on other sites

Ok, I recommend you post all your relevant code, and elaborate exactly what you want. This piecemeal approach to getting help isn't going anywhere.

Share this post


Link to post
Share on other sites

Here i am passing an array and it's lenth to an API in DLL

$Str = "long $Length;char $Nonarray[128]" ;Creating a Structure

$a = DllStructCreate($Length)

$b = DllStructCreate($Nonarray)

DllStructSetData($a, 1, 0)

DllStructSetData($b, 1, "", 0)

$Result = DllCall($PE_Plugin, "long", "Monitor_GetSupportedNCValues", "long", $opcode, "long*", DllStructGetPtr($a, 1), "long*", DllStructGetPtr($b, 1))

After executing this, AUTO IT is stops executing.

Share this post


Link to post
Share on other sites

Someone else will have to jump in on this one. Also, what dll are you calling, what function are you calling from the dll, and what are you trying to do with your code.

Again, post ALL relevant code (and put it in

tags.) Explain what you want. Don't just post "AutoIt stops executing."

Without that, you might as well be asking how many fingers you're holding up.

Share this post


Link to post
Share on other sites

Here

i am trying to pass an array and length of array to a function.

My interest is after passing the array and length as reference, i need to to get the array and i have to process the values in it.

The function witch will return the array elements is exposed in a DLL.

This i am calling in my script.

Protocol_GetNonConValues($MONITOR_COLOR_PRESET, $arrlen, $Nonarr)

Actual function is in header file.

Func Protocol_GetNonConValues($opcode, ByRef $Length, Byref $Nonarray)
    $Str = "long $Length;char $Nonarray[128]"         ;Creating a Structure
    $a = DllStructCreate($Length)
    $b = DllStructCreate($Nonarray)
    
    DllStructSetData($a, 1, 0)
    DllStructSetData($b, 1, "", 0)

    $Result = DllCall($PE_Plugin, "long", "Monitor_GetSupportedNCValues", "long", $opcode, "long*", DllStructGetPtr($a, 1), "long*", DllStructGetPtr($b, 1))
    
    msgbox(0, "", $Result[2])
    msgbox(0, "", $Result[3])
    
EndFunc

Monitor_GetSupportedNCValues: this function will return the array of noncontinuous values and its length.

Share this post


Link to post
Share on other sites

Like this??

#include<array.au3>
dim $avArray[10]
$avArray[0] = "JPM"
$avArray[1] = "Holger"
$avArray[2] = "Jon"
$avArray[3] = "Larry"
$avArray[4] = "Jeremy"
$avArray[5] = "Valik"
$avArray[6] = "Cyberslug"
$avArray[7] = "Nutster"
$avArray[8] = "JdeB"
$avArray[9] = "Tylo"

$str=_ArrayToString($avArray,"|")
$arr=StringSplit($str,"|")
array($arr[0],$avArray)

Func array($length,$array)
    
    _ArrayDisplay($array,"")
    msgbox(4096,"","dimensions: "& $length)
EndFunc

[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com

Share this post


Link to post
Share on other sites

Here

i am trying to pass an array and length of array to a function.

My interest is after passing the array and length as reference, i need to to get the array and i have to process the values in it.

The function witch will return the array elements is exposed in a DLL.

This i am calling in my script.

Protocol_GetNonConValues($MONITOR_COLOR_PRESET, $arrlen, $Nonarr)

Actual function is in header file.

Func Protocol_GetNonConValues($opcode, ByRef $Length, Byref $Nonarray)
    $Str = "long $Length;char $Nonarray[128]"        ;Creating a Structure
    $a = DllStructCreate($Length)
    $b = DllStructCreate($Nonarray)
    
    DllStructSetData($a, 1, 0)
    DllStructSetData($b, 1, "", 0)

    $Result = DllCall($PE_Plugin, "long", "Monitor_GetSupportedNCValues", "long", $opcode, "long*", DllStructGetPtr($a, 1), "long*", DllStructGetPtr($a, 2))
    
    msgbox(0, "", $Result[2])
    msgbox(0, "", $Result[3])
    
EndFunc

Monitor_GetSupportedNCValues: this function will return the array of noncontinuous values and its length

There is some real confusion in there. The dollar sign "$" indicating an AutoIt variable is not appropriate for DLL struct element name, so $Str should look like this:
$Str = "long Length;char Nonarray[128]"

Then, you fail to use $Str to define your struct at all. That should be just:

$a = DLLStructCreate($Str)

A DLL struct array of char type is just a string variable in AutoIt and is not passed as an AutoIt array.

After all that it is so confusing I lost track of what you wanted to do in the first place...

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Hi Thanks for your reply.

I have tried in the same way, but when i execute the DllCall, AutoIt is stops executing.

Func Protocol_GetNonConValues($opcode, $Length, $Nonarray)
    $Str = "long Length;char Nonarray[128]"     ;Creating a Structure
    $a = DllStructCreate($Str)
    
    
    DllStructSetData($a, 1, 0)
    DllStructSetData($a, 2, "", 0)
    MsgBox(0, "", "hello")

    $Result = DllCall($PE_Plugin, "long", "Monitor_GetSupportedNCValues", "long", $opcode, "long*", DllStructGetPtr($a, 1), "long*", DllStructGetData($a, 2))
    
    msgbox(0, "", $Result[2])
    msgbox(0, "", $Result[3])
    
EndFunc

My Problem is how to get the array returned by the function Monitor_GetSupportedNCValues.

Share this post


Link to post
Share on other sites

Hi Thanks for your reply.

I have tried in the same way, but when i execute the DllCall, AutoIt is stops executing.

Func Protocol_GetNonConValues($opcode, $Length, $Nonarray)
    $Str = "long Length;char Nonarray[128]" ;Creating a Structure
    $a = DllStructCreate($Str)
    
    
    DllStructSetData($a, 1, 0)
    DllStructSetData($a, 2, "", 0)
    MsgBox(0, "", "hello")

    $Result = DllCall($PE_Plugin, "long", "Monitor_GetSupportedNCValues", "long", $opcode, "long*", DllStructGetPtr($a, 1), "long*", DllStructGetData($a, 2))
    
    msgbox(0, "", $Result[2])
    msgbox(0, "", $Result[3])
    
EndFunc

My Problem is how to get the array returned by the function Monitor_GetSupportedNCValues.

You need to post description of that function, description of parameters, description of required structure(s).

There are like zillion possible scenarios regarding informations that you provided. More is needed if you want someone to help you with that.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Func Protocol_GetNonConValues($opcode, $Length, $Nonarray)
    $Str = "long Length;char Nonarray[128]"   ;Creating a Structure
    $a = DllStructCreate($Str)
    
    
    DllStructSetData($a, 1, 0)
    DllStructSetData($a, 2, "", 0)
    MsgBox(0, "", "hello")

    $Result = DllCall($PE_Plugin, "long", "Monitor_GetSupportedNCValues", "long", $opcode, "long*", DllStructGetPtr($a, 1), "long*", DllStructGetData($a, 2))
    
    msgbox(0, "", $Result[2])
    msgbox(0, "", $Result[3])
    
EndFunc

function i am calling as

Protocol_GetNonConValues($MONITOR_COLOR_PRESET, $arrlen, $Nonarr)

This function internally invokes DllCall where the API inside that Monitor_GetSupportedNCValues will return array and it's length.

so we have to pass the array and arraylengh as passbyreference to the function.

I think so this clarifies.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

blah

Hey WTF, man??? Sorry for being rude, but by only reading this thread I got upset. Already in the almost 1st answer you have been asked to talk about what you are trying to do. "Passing an array to a function by ref" is not, I repeat NOT an answer to this question (and this won't change, no matter how often you post the same code again). If you are here in need for qualified answers (which the ppl here usually are very willing to give), tell us about your script and what it is about. If for some reason you don't want to post your whole script, at least wrap up your function to do something so we can see what it is supposed to do. Again, sorry thousandfold for being rude. I will do some massive penance now ("Dear Lord...blah...blub...blah...please forgive...blah...blub...blah...please punish...blah...blub...blah...").

Edited by cherdeg

Share this post


Link to post
Share on other sites

Func Protocol_GetNonConValues($opcode, $Length, $Nonarray)
    $Str = "long Length;char Nonarray[128]"  ;Creating a Structure
    $a = DllStructCreate($Str)
    
    
    DllStructSetData($a, 1, 0)
    DllStructSetData($a, 2, "", 0)
    MsgBox(0, "", "hello")

    $Result = DllCall($PE_Plugin, "long", "Monitor_GetSupportedNCValues", "long", $opcode, "long*", DllStructGetPtr($a, 1), "long*", DllStructGetData($a, 2))
    
    msgbox(0, "", $Result[2])
    msgbox(0, "", $Result[3])
    
EndFunc

function i am calling as

Protocol_GetNonConValues($MONITOR_COLOR_PRESET, $arrlen, $Nonarr)

This function internally invokes DllCall where the API inside that Monitor_GetSupportedNCValues will return array and it's length.

so we have to pass the array and arraylengh as passbyreference to the function.

I think so this clarifies.

I didn't meant description of your function, but description of Monitor_GetSupportedNCValues function and its parameters.

Btw, you probably need pointer to created structure there somewhere, not only one element. And if the element of structure is an array then its first element is 1 (not 0).

Just post description.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

The description of function is :

long Monitor_GetSupportedNCValues (long

lnCommand, long* o_plNCValueCount, long* o_larrNCValues)

Share this post


Link to post
Share on other sites

The description of function is :

long Monitor_GetSupportedNCValues (long

lnCommand, long* o_plNCValueCount, long* o_larrNCValues)

Bill's mother is making a cake. Says to him: "Son, go to the store and buy me 10 eggs. If there would be those little butters - buy 2". Bill goes.

After like 10 minutes Bill comes home with 2 eggs.

More is needed Srikrishna.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Monitor_GetSupportedNCValues it is an API exposed in a DLL.

It will return all the noncontinues values of the required parametre.

Here suppose i want to get all noncontinious values of color, (the values will be like 0, 5, 7, 8, 9, 11 etc) then send the colorname as long

lnCommand and we have to get all noncontinious values into an array and it is lenght also.

Now ithink so it is clear for you.

Share this post


Link to post
Share on other sites

tralla-la.

Hey: Even if I was the one being able to help you (and I'm glad I ain't), I simply wouldn't. 18 posts (where the half of them consist of almost the same words) in a thread to come to this last answer of yours (which doesn't help either, at least not me) are an impertinence. Post your whole script or leave it (my personal opinion, but maybe you'll find somebody still willing to help).

Share this post


Link to post
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
Sign in to follow this  
Followers 0