Problem with Marshalling and Structures 
Author Message
 Problem with Marshalling and Structures

I need to pass a structure to an API call. So, I declare
it as below.

Public Structure st1
 <MarshalAs(UnManagedType.ByValArray, sizeconst:=600)>
Public mem1() as Byte
End Structure

Public Sub Proc1()

        Dim v1 as st1
        Redim v1.mem1(599)
        Dim tempstr as string

        Make the call
        tempstr = System.Text.Encoding.ASCII.GetString
(v1.mem1, 23, 3)
        Msgbox(tempstr)

End Sub

This works good. I am supposed to get value of "123" and I
am getting it.

However, if I move this to a structure, I am having
difficulties.

Modified Version
----------------

Public Structure st1
 <MarshalAs(UnManagedType.ByvalArray, sizeconst:=600)>
Public mem1() as Byte
End Structure

<StructLayout(LayoutKind.Sequential, pack=1,
Charset:=Charset.ANSI)> Public Structure st2
 <MarshalAs(UnManagedType.ByValTStr, sizeconst:=23)>
Public mem1 as string
 <MarshalAs(UnManagedType.ByValTStr, sizeconst:=3)> Public
mem2 as string
 <MarshalAs(UnManagedType.ByValTStr, sizeconst:=574)>
Public mem3 as string
End Structure

Public Sub Proc1()

        Dim v1 as st1
        Redim v1.mem1(599)
        Dim tempstr as string

        Dim v2 as st2
        Dim iptr as IntPtr
        Dim h as GCHandle

        Make the call

        tempstr = System.Text.Encoding.ASCII.GetString
(v1.mem1, 23, 3)
        Msgbox(tempstr)         ' I get 123, which is good

        ' Now move this data to a structure

        h = GCHandle.Alloc(v1.mem1, GCHandleType.Pinned)
        iptr = h.AddrOfPinnedObject
        v2 = CType(Marshal.PtrToStructure(iptr,GetType
(st2)), st2)
        h.free()

        tempstr = v2.mem2
        Msgbox(tempstr)         ' I get 12 only, which is
not correct

End Sub

The last character is missing. If I change the sizeconst
from 3 to 4 of mem2, then I get 123. But, I think it is
not the correct fix. It also alters the size and might
cause other problems.

Could anyone please provide more details on this and tell
me what I am missing.

Thanks,
Seth.



Sat, 05 Mar 2005 06:40:32 GMT  
 Problem with Marshalling and Structures
Hi,

I think the problem seems relate to the marshal type:
UnManagedType.ByValTStr. String always has a NULL terminate character in
the end. This should be one bye length. Therefore, you should add one to
the length when using ByValTStr instead of ByvalArray.

I hope this helps. If you have any questions, please reply to this post.

Best Regards,

Jun Su
Microsoft Support

---
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| Content-Class: urn:content-classes:message


| Subject: Problem with Marshalling and Structures
| Date: Mon, 16 Sep 2002 15:40:32 -0700
|
| I need to pass a structure to an API call. So, I declare
| it as below.
|
| Public Structure st1
|  <MarshalAs(UnManagedType.ByValArray, sizeconst:=600)>
| Public mem1() as Byte
| End Structure
|
| Public Sub Proc1()
|
|       Dim v1 as st1
|       Redim v1.mem1(599)
|       Dim tempstr as string
|
|       Make the call
|       tempstr = System.Text.Encoding.ASCII.GetString
| (v1.mem1, 23, 3)
|       Msgbox(tempstr)
|
| End Sub
|
| This works good. I am supposed to get value of "123" and I
| am getting it.
|
| However, if I move this to a structure, I am having
| difficulties.
|
| Modified Version
| ----------------
|
| Public Structure st1
|  <MarshalAs(UnManagedType.ByvalArray, sizeconst:=600)>
| Public mem1() as Byte
| End Structure
|
| <StructLayout(LayoutKind.Sequential, pack=1,
| Charset:=Charset.ANSI)> Public Structure st2
|  <MarshalAs(UnManagedType.ByValTStr, sizeconst:=23)>
| Public mem1 as string
|  <MarshalAs(UnManagedType.ByValTStr, sizeconst:=3)> Public
| mem2 as string
|  <MarshalAs(UnManagedType.ByValTStr, sizeconst:=574)>
| Public mem3 as string
| End Structure
|
|      
| Public Sub Proc1()
|
|       Dim v1 as st1
|       Redim v1.mem1(599)
|       Dim tempstr as string
|
|       Dim v2 as st2
|       Dim iptr as IntPtr
|       Dim h as GCHandle
|
|       Make the call
|
|       tempstr = System.Text.Encoding.ASCII.GetString
| (v1.mem1, 23, 3)
|       Msgbox(tempstr)         ' I get 123, which is good
|
|         ' Now move this data to a structure
|      
|       h = GCHandle.Alloc(v1.mem1, GCHandleType.Pinned)
|       iptr = h.AddrOfPinnedObject
|       v2 = CType(Marshal.PtrToStructure(iptr,GetType
| (st2)), st2)
|       h.free()
|
|       tempstr = v2.mem2
|       Msgbox(tempstr)         ' I get 12 only, which is
| not correct
|
| End Sub
|
| The last character is missing. If I change the sizeconst
| from 3 to 4 of mem2, then I get 123. But, I think it is
| not the correct fix. It also alters the size and might
| cause other problems.
|
| Could anyone please provide more details on this and tell
| me what I am missing.
|
| Thanks,
| Seth.
|
|



Sun, 06 Mar 2005 09:46:31 GMT  
 Problem with Marshalling and Structures
Thanks for your response. Yes. I suspected this and tried
it. I changed the size of mem2 from 3 to 4. It did display
the full value "123". But, the first character in the next
member of the structure(mem3) is missing.

Any ideas on how to fix this.

Thanks again, for your help.

Seth.

Quote:
>-----Original Message-----
>Hi,

>I think the problem seems relate to the marshal type:
>UnManagedType.ByValTStr. String always has a NULL

terminate character in
Quote:
>the end. This should be one bye length. Therefore, you
should add one to
>the length when using ByValTStr instead of ByvalArray.

>I hope this helps. If you have any questions, please

reply to this post.
Quote:

>Best Regards,

>Jun Su
>Microsoft Support

>---
>This posting is provided "AS IS" with no warranties, and
confers no rights.

>--------------------
>| Content-Class: urn:content-classes:message


>| Subject: Problem with Marshalling and Structures
>| Date: Mon, 16 Sep 2002 15:40:32 -0700
>|
>| I need to pass a structure to an API call. So, I
declare
>| it as below.
>|
>| Public Structure st1
>|  <MarshalAs(UnManagedType.ByValArray, sizeconst:=600)>
>| Public mem1() as Byte
>| End Structure
>|
>| Public Sub Proc1()
>|
>|   Dim v1 as st1
>|   Redim v1.mem1(599)
>|   Dim tempstr as string
>|
>|   Make the call
>|   tempstr = System.Text.Encoding.ASCII.GetString
>| (v1.mem1, 23, 3)
>|   Msgbox(tempstr)
>|
>| End Sub
>|
>| This works good. I am supposed to get value of "123"
and I
>| am getting it.
>|
>| However, if I move this to a structure, I am having
>| difficulties.
>|
>| Modified Version
>| ----------------
>|
>| Public Structure st1
>|  <MarshalAs(UnManagedType.ByvalArray, sizeconst:=600)>
>| Public mem1() as Byte
>| End Structure
>|
>| <StructLayout(LayoutKind.Sequential, pack=1,
>| Charset:=Charset.ANSI)> Public Structure st2
>|  <MarshalAs(UnManagedType.ByValTStr, sizeconst:=23)>
>| Public mem1 as string
>|  <MarshalAs(UnManagedType.ByValTStr, sizeconst:=3)>
Public
>| mem2 as string
>|  <MarshalAs(UnManagedType.ByValTStr, sizeconst:=574)>
>| Public mem3 as string
>| End Structure
>|
>|  
>| Public Sub Proc1()
>|
>|   Dim v1 as st1
>|   Redim v1.mem1(599)
>|   Dim tempstr as string
>|
>|   Dim v2 as st2
>|   Dim iptr as IntPtr
>|   Dim h as GCHandle
>|
>|   Make the call
>|
>|   tempstr = System.Text.Encoding.ASCII.GetString
>| (v1.mem1, 23, 3)
>|   Msgbox(tempstr)         ' I get 123, which is good
>|
>|         ' Now move this data to a structure
>|  
>|   h = GCHandle.Alloc(v1.mem1, GCHandleType.Pinned)
>|   iptr = h.AddrOfPinnedObject
>|   v2 = CType(Marshal.PtrToStructure(iptr,GetType
>| (st2)), st2)
>|   h.free()
>|
>|   tempstr = v2.mem2
>|   Msgbox(tempstr)         ' I get 12 only, which is
>| not correct
>|
>| End Sub
>|
>| The last character is missing. If I change the
sizeconst
>| from 3 to 4 of mem2, then I get 123. But, I think it is
>| not the correct fix. It also alters the size and might
>| cause other problems.
>|
>| Could anyone please provide more details on this and
tell
>| me what I am missing.
>|
>| Thanks,
>| Seth.
>|
>|

>.



Mon, 07 Mar 2005 01:14:39 GMT  
 Problem with Marshalling and Structures
Hi,

Can you show me the C declaration of the function? The marshal result
relates to it.

I hope this helps. If you have any questions, please reply to this post.

Best Regards,

Jun Su
Microsoft Support

---
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| Content-Class: urn:content-classes:message


|
| Thanks for your response. Yes. I suspected this and tried
| it. I changed the size of mem2 from 3 to 4. It did display
| the full value "123". But, the first character in the next
| member of the structure(mem3) is missing.
|
| Any ideas on how to fix this.
|
| Thanks again, for your help.
|
| Seth.
|

Quote:
| >-----Original Message-----
| >Hi,
| >
| >I think the problem seems relate to the marshal type:
| >UnManagedType.ByValTStr. String always has a NULL
| terminate character in
| >the end. This should be one bye length. Therefore, you
| should add one to
| >the length when using ByValTStr instead of ByvalArray.
| >
| >I hope this helps. If you have any questions, please
| reply to this post.
| >
| >Best Regards,
| >
| >Jun Su
| >Microsoft Support
| >
| >---
| >This posting is provided "AS IS" with no warranties, and
| confers no rights.
| >
| >--------------------
| >| Content-Class: urn:content-classes:message


| >| Subject: Problem with Marshalling and Structures
| >| Date: Mon, 16 Sep 2002 15:40:32 -0700
| >|
| >| I need to pass a structure to an API call. So, I
| declare
| >| it as below.
| >|
| >| Public Structure st1
| >|  <MarshalAs(UnManagedType.ByValArray, sizeconst:=600)>
| >| Public mem1() as Byte
| >| End Structure
| >|
| >| Public Sub Proc1()
| >|
| >|         Dim v1 as st1
| >|         Redim v1.mem1(599)
| >|         Dim tempstr as string
| >|
| >|         Make the call
| >|         tempstr = System.Text.Encoding.ASCII.GetString
| >| (v1.mem1, 23, 3)
| >|         Msgbox(tempstr)
| >|
| >| End Sub
| >|
| >| This works good. I am supposed to get value of "123"
| and I
| >| am getting it.
| >|
| >| However, if I move this to a structure, I am having
| >| difficulties.
| >|
| >| Modified Version
| >| ----------------
| >|
| >| Public Structure st1
| >|  <MarshalAs(UnManagedType.ByvalArray, sizeconst:=600)>
| >| Public mem1() as Byte
| >| End Structure
| >|
| >| <StructLayout(LayoutKind.Sequential, pack=1,
| >| Charset:=Charset.ANSI)> Public Structure st2
| >|  <MarshalAs(UnManagedType.ByValTStr, sizeconst:=23)>
| >| Public mem1 as string
| >|  <MarshalAs(UnManagedType.ByValTStr, sizeconst:=3)>
| Public
| >| mem2 as string
| >|  <MarshalAs(UnManagedType.ByValTStr, sizeconst:=574)>
| >| Public mem3 as string
| >| End Structure
| >|
| >|        
| >| Public Sub Proc1()
| >|
| >|         Dim v1 as st1
| >|         Redim v1.mem1(599)
| >|         Dim tempstr as string
| >|
| >|         Dim v2 as st2
| >|         Dim iptr as IntPtr
| >|         Dim h as GCHandle
| >|
| >|         Make the call
| >|
| >|         tempstr = System.Text.Encoding.ASCII.GetString
| >| (v1.mem1, 23, 3)
| >|         Msgbox(tempstr)         ' I get 123, which is good
| >|
| >|         ' Now move this data to a structure
| >|        
| >|         h = GCHandle.Alloc(v1.mem1, GCHandleType.Pinned)
| >|         iptr = h.AddrOfPinnedObject
| >|         v2 = CType(Marshal.PtrToStructure(iptr,GetType
| >| (st2)), st2)
| >|         h.free()
| >|
| >|         tempstr = v2.mem2
| >|         Msgbox(tempstr)         ' I get 12 only, which is
| >| not correct
| >|
| >| End Sub
| >|
| >| The last character is missing. If I change the
| sizeconst
| >| from 3 to 4 of mem2, then I get 123. But, I think it is
| >| not the correct fix. It also alters the size and might
| >| cause other problems.
| >|
| >| Could anyone please provide more details on this and
| tell
| >| me what I am missing.
| >|
| >| Thanks,
| >| Seth.
| >|
| >|
| >
| >.
| >
|



Tue, 08 Mar 2005 09:29:48 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. marshalling cascading C structures

2. VB Debugger Causes Marshalling Problems?

3. System.TypeLoadException : Marshalling types to LPStruct is not supported on structure fields?

4. Marshalling for platform invoke

5. .NET Remoting - Marshalling Collections

6. Method Parameter Marshalling

7. API Calls and Marshalling

8. Structure within Structure

9. call VC DLL with structure contains pointer of the structure to next

10. Creating a folder structure and then assigning permissions on that structure via script

11. Error marshalling collection class

12. COM+ and ADO Command Object - Marshalling?

 

 
Powered by phpBB® Forum Software