C structs containing arrays to VB

  • Thread starter Thread starter Chris Tacke, eMVP
  • Start date Start date
C

Chris Tacke, eMVP

The CF marshaller cannot handle this type of struct. You'll have to
manually marshal the data. I'd pass it as a large byte array, then expose
properties that retrieve and cast the specific offset you're after.
 
Hi,

I have a C struct containing arrays of long, char and
bool. A pointer to this struct is returned from a
function. Is there a simple way to access them?

Here is the C struct:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval[30];
bool bval[30];
};

In the vb code I get *GetLongRT as an IntPtr. Then I
marshal it to a VB structure. Or atleast that is the
plan. What I don't know is what type the members of the
Structure should be.

Public Structure GetLongRT
Public sval As ??
Public lval As ??
Public bval As ??
end Structure

I can do it with so called blittable types within the
structure but when I have arrays of then I am unsure how
to proceed.

Thanks,

Denis
 
-----Original Message-----
The CF marshaller cannot handle this type of struct. You'll have to
manually marshal the data. I'd pass it as a large byte array, then expose
properties that retrieve and cast the specific offset you're after.

Okay, that's the idea my partner and I had seen in the
advanced p/invoke article on msdn. It uses a Memory
class and allocates the memory it's going to need.

Here's what I'm going to try:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

Public Structure GetLongRTS
Public sval As StringPtr
Public lval As Integer
End Structure

Public Structure StringPtr
Private szString As IntPtr

Public Sub New(ByVal s As Integer)
Me.szString = Memory.AllocHLocal(2 * (1 + s))
End Sub

Public Overrides Function ToString() As String
Return Marshal.PtrToStringUni(Me.szString)
End Function

Public Sub Free()
Memory.FreeHLocal(Me.szString)
End Sub
End Structure

Dim ptr As IntPtr = DBAccessFuncDecl.GetLongVal()
Dim obj As DBAccessFuncDecl.GetLongRTS
obj.sval = New DBAccessFuncDecl.StringPtr(30)
obj = CType(Marshal.PtrToStructure(ptr, GetType
(DBAccessFuncDecl.GetLongRTS)),
DBAccessFuncDecl.GetLongRTS)


Is that the idea??

And then we also have some 2d arrays, char[20][20].
Can this approach work?

Thanks again
Denis
Applied Data Systems
www.applieddata.net


Hi,

I have a C struct containing arrays of long, char and
bool. A pointer to this struct is returned from a
function. Is there a simple way to access them?

Here is the C struct:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval[30];
bool bval[30];
};

In the vb code I get *GetLongRT as an IntPtr. Then I
marshal it to a VB structure. Or atleast that is the
plan. What I don't know is what type the members of the
Structure should be.

Public Structure GetLongRT
Public sval As ??
Public lval As ??
Public bval As ??
end Structure

I can do it with so called blittable types within the
structure but when I have arrays of then I am unsure how
to proceed.

Thanks,

Denis


.
 
Take a look at several classes in OpenNETCF.WinAPI for examples of more
complex marshaling.

-Chris


-----Original Message-----
The CF marshaller cannot handle this type of struct. You'll have to
manually marshal the data. I'd pass it as a large byte array, then expose
properties that retrieve and cast the specific offset you're after.

Okay, that's the idea my partner and I had seen in the
advanced p/invoke article on msdn. It uses a Memory
class and allocates the memory it's going to need.

Here's what I'm going to try:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

Public Structure GetLongRTS
Public sval As StringPtr
Public lval As Integer
End Structure

Public Structure StringPtr
Private szString As IntPtr

Public Sub New(ByVal s As Integer)
Me.szString = Memory.AllocHLocal(2 * (1 + s))
End Sub

Public Overrides Function ToString() As String
Return Marshal.PtrToStringUni(Me.szString)
End Function

Public Sub Free()
Memory.FreeHLocal(Me.szString)
End Sub
End Structure

Dim ptr As IntPtr = DBAccessFuncDecl.GetLongVal()
Dim obj As DBAccessFuncDecl.GetLongRTS
obj.sval = New DBAccessFuncDecl.StringPtr(30)
obj = CType(Marshal.PtrToStructure(ptr, GetType
(DBAccessFuncDecl.GetLongRTS)),
DBAccessFuncDecl.GetLongRTS)


Is that the idea??

And then we also have some 2d arrays, char[20][20].
Can this approach work?

Thanks again
Denis
Applied Data Systems
www.applieddata.net


Hi,

I have a C struct containing arrays of long, char and
bool. A pointer to this struct is returned from a
function. Is there a simple way to access them?

Here is the C struct:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval[30];
bool bval[30];
};

In the vb code I get *GetLongRT as an IntPtr. Then I
marshal it to a VB structure. Or atleast that is the
plan. What I don't know is what type the members of the
Structure should be.

Public Structure GetLongRT
Public sval As ??
Public lval As ??
Public bval As ??
end Structure

I can do it with so called blittable types within the
structure but when I have arrays of then I am unsure how
to proceed.

Thanks,

Denis


.
 
Back
Top