Re: Pointers to structures



"toxicated101" <matt.wheatley@xxxxxxxxx> wrote in
news:1122364871.318007.198240@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:

> Hi
>
> I have a function declared as
>
> Public Function SomeFunction(byref somesturcture as long) as long
>
> The somefunction wants the pointer to the structure rather than the
> structure, how do I call this. I have been trying the following
>
> Dim TheStructure as somesturctureDef
> Dim iRetVal as long
>
> TheStructure.a = 1
> TheStructure.b = 2
>
>
> iRetVal = SomeFunction(VarPtr(TheStructure))
>
> But it doesn't appear to work, can anybody help me with this little
> problem
>
> Thanks.
>

First change the declare to:

Public Function SomeFunction(ByRef SomeStructure As SomeStructureDef) As
Long

Next, Visual Basic takes care of the rest.

Dim TheStructure As SomeStructureDef
Dim iRetVal As Long

TheStructure.A = 1
TheStructure.B = 2

iRetVal = SomeFunction(TheStructure)

Note: In your later post you mentioned using Any in place of
SomeStructureDef. While this will work it can slow your application down
a little. If speed is a priority, then you should avoid using the Any
keyword and specify exactly what it is your passing. This allows Visual
Basic to early-bind your code.

The Any keyword should be reserved for using the Windows API.
.


Quantcast