PBDR.COM

About   -   Contact   -   Purchase   -   Search   -   What's New

 
 
Passing a Structure to a DLL
This tip was submitted by Edward Hoo.

Passing a structure or string pointer to a DLL's function is pretty strait forward, just declare it with the ref keyword, but...

1) How can you pass a pointer inside another structure?

or...

2) How can you initialize the long parameter to a predefined SDK msg to a pointer to a string or structure?

Well... remember the old 'C' strcpy function. Yeap! that little sucker that returns a pointer to the target string????? It's ALIVE! ...and it's called lstrcpy and it lives on kernel32.dll!!!!

Function pointer

create a user object and declare the following local external function

> function long lstrcpy(ref string lpString1,ref string lpString2) library "kernel32.dll"

declare the following user object function

 > long of_StringPointer(ref string ps_string)
 > {
 > string foo
 >
 > foo = ps_string
 >
 > return lstrcpy(foo, ps_string)
 > }

Structure pointer

create a user object and declare the following local external function

 > function long lstrcpy( &
     ref MyOversizedStructure lpMyOversizedStructure,&
     ref string lpString2) library "kernel32.dll"

declare the following user object function

 > long of_MyStructurePointer( &
      ref MyOversizedStructure ps_structure)
 > {
 > string foo
 >
 > foo = ""
 >
 > return lstrcpy(ps_structure, foo)
 > }

Now you are going to tell me that the "return lstrcpy(ps_structure, ps_string)" overwrites the first byte of ps_structure. Well... why do you think we named your structure as MyOversizedStructure? Cuz it has an extra field at the beginning which is a long... Now your MyOversizedStructure looks like this:

 > typedef MyOversizedStructure
 > {
 > long il_i_will_be_overwitten
 > [datatype] [yours structure 1st field]
 > [datatype] [yours structure 2nd field]
 > [datatype] [yours structure 3rd field]
 > ...
 > }

now the function looks like this...

 > long of_MyStructurePointer( &
     ref MyOversizedStructure ps_structure)
 > {
 > string foo
 >
 > foo = ""
 >
 > return lstrcpy(ps_structure, foo) + 4
 > }

and the '+4' takes care of the offset of the il_i_will_be_overwitten dummy field

Remember that the address is only good as far as you do not assign values to fields within the structure i.e

 > pointer_before = of_MyStructurePointer(my_structure)
 > my_structure.my_field = some_value
 > pointer_after = of_MyStructurePointer(my_structure)

'pointer_before' might be different from 'pointer_after'

 

Top of Page

Legal Notice

Ken Howe 2011