VB and Pointers

  • Thread starter Thread starter Denis C
  • Start date Start date
D

Denis C

Hi,

I'm having a problem using a C native library with my VB
code. All the C functions I am trying to use return
struct pointers. I have dllImport statements for all the
functions I want to access but I don't know what VB type
they should be returning? A generic object? The Type
object?

Denis
 
Denis C said:
I'm having a problem using a C native library with my VB
code. All the C functions I am trying to use return
struct pointers. I have dllImport statements for all the
functions I want to access but I don't know what VB type
they should be returning? A generic object? The Type
object?

As they return pointers, I'd declare the return value as IntPtr. If
necessary, have a look at the methods of
System.Runtime.InteropServices.Marshal.
 
* "Denis C said:
I'm having a problem using a C native library with my VB
code. All the C functions I am trying to use return
struct pointers. I have dllImport statements for all the
functions I want to access but I don't know what VB type
they should be returning? A generic object? The Type
object?

The solution depends on the concrete implementation, but you may declare
the return value as 'IntPtr' and use
'System.Runtime.InteropServices.Marshal.PtrToStructure' to dereference
the pointer. Have a look at the documentation in order to be sure you
understand how to use this method.
 
I used the PtrToStructure sub but I'm still having
problems. I think the problem now is that my VB structure
and my C struct aren't exactly equivalent in memory. I
wrote them equivalently but if I set cstruct.lval = 5 in
the C dll and return a pointer to struct, then in VB when
i try to access vbstruct.lval it just gives me garbage
which i'm assuming is from some other place in memory.
Should I be marshaling the vbstruct as the cstruct when I
declare it in the VB?

Thanks for the help
 
* "Denis C said:
I used the PtrToStructure sub but I'm still having
problems. I think the problem now is that my VB structure
and my C struct aren't exactly equivalent in memory. I
wrote them equivalently but if I set cstruct.lval = 5 in
the C dll and return a pointer to struct, then in VB when
i try to access vbstruct.lval it just gives me garbage
which i'm assuming is from some other place in memory.
Should I be marshaling the vbstruct as the cstruct when I
declare it in the VB?

Post the definition of the structure in C and VB.NET. Maybe somebody
here will find the bug.
 
Back
Top