PC Review


Reply
Thread Tools Rate Thread

C structs containing arrays to VB

 
 
Chris Tacke, eMVP
Guest
Posts: n/a
 
      25th Nov 2003
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.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


"Denis C" <(E-Mail Removed)> wrote in message
news:04cc01c3b3a3$3078f360$(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
 
Denis C
Guest
Posts: n/a
 
      25th Nov 2003
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
 
Reply With Quote
 
Guest
Posts: n/a
 
      25th Nov 2003

>-----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.
>
>--
>Chris Tacke, eMVP
>Co-Founder and Advisory Board Member
>www.OpenNETCF.org
>---
>Windows CE Product Manager


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
>
>
>"Denis C" <(E-Mail Removed)> wrote in

message
>news:04cc01c3b3a3$3078f360$(E-Mail Removed)...
>> 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

>
>
>.
>

 
Reply With Quote
 
Chris Tacke, eMVP
Guest
Posts: n/a
 
      26th Nov 2003
Take a look at several classes in OpenNETCF.WinAPI for examples of more
complex marshaling.

-Chris


<(E-Mail Removed)> wrote in message
news:113c01c3b3ab$32b973e0$(E-Mail Removed)...
>
> >-----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.
> >
> >--
> >Chris Tacke, eMVP
> >Co-Founder and Advisory Board Member
> >www.OpenNETCF.org
> >---
> >Windows CE Product Manager

>
> 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
> >
> >
> >"Denis C" <(E-Mail Removed)> wrote in

> message
> >news:04cc01c3b3a3$3078f360$(E-Mail Removed)...
> >> 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

> >
> >
> >.
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Arrays of classes or structs, which is best =?Utf-8?B?TWFjY2E=?= Microsoft C# .NET 5 27th May 2006 07:35 AM
arrays, structs, pointers, casting - help! kelli Microsoft C# .NET 4 4th Aug 2005 08:35 PM
Arrays in structs cody Microsoft C# .NET 4 16th Mar 2005 04:26 PM
Interfacing with an API - structs with arrays of structs. Dave A Microsoft Dot NET 1 29th Dec 2004 11:47 PM
Arrays inside of structs Ken Beauchesne Microsoft C# .NET 2 29th Oct 2004 02:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:31 PM.