I am trying to get two structures to map the same data, like a union.
Having failed to get the FieldOffset attribute to work at runtime, I tried:
----------------------------------------------------------------
Option Strict On
Imports System
Imports System.Runtime.InteropServices
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _
Public Structure Struct1
<VBFixedString(6), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> _
Public f1 As String
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _
Public Structure Struct2
<VBFixedString(1), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> _
Public f1 As String
<VBFixedString(2), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2)> _
Public f2 As String
<VBFixedString(3), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> _
Public f3 As String
End Structure
Module Module1
Sub Main()
Dim s1 As Struct1
s1.f1 = "ABCDEF"
Console.WriteLine(s1.f1 + " " + (Marshal.SizeOf(s1)).ToString)
Dim ptrStruct As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(s1))
Try
Marshal.StructureToPtr(s1, ptrStruct, False)
Dim s2 As Struct2
s2 = CType(Marshal.PtrToStructure(ptrStruct, GetType(Struct2)), Struct2)
Console.WriteLine(s2.f1 + "," + s2.f2 + "," + s2.f3 + " " +
(Marshal.SizeOf(s2)).ToString)
Console.ReadLine()
Finally
Marshal.FreeHGlobal(ptrStruct)
End Try
End Sub
End Module
----------------------------------------------------------------
The first Console.Writeline outputs 'ABCDEF 6', and the second ',B,DE 6'
instead of 'A,BC,DEF 6'. Clearly, I am losing the first byte of each
string in Struct2. Any help? Thanks.
Dennis 1 1135
>I am trying to get two structures to map the same data, like a union. Having failed to get the FieldOffset attribute to work at runtime, I tried:
---------------------------------------------------------------- Option Strict On
Imports System
Imports System.Runtime.InteropServices
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _
Public Structure Struct1
<VBFixedString(6), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> _
Public f1 As String
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _
Public Structure Struct2
<VBFixedString(1), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> _
Public f1 As String
<VBFixedString(2), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2)> _
Public f2 As String
<VBFixedString(3), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> _
Public f3 As String
End Structure Module Module1
Sub Main()
Dim s1 As Struct1
s1.f1 = "ABCDEF"
Console.WriteLine(s1.f1 + " " + (Marshal.SizeOf(s1)).ToString)
Dim ptrStruct As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(s1))
Try
Marshal.StructureToPtr(s1, ptrStruct, False)
Dim s2 As Struct2
s2 = CType(Marshal.PtrToStructure(ptrStruct, GetType(Struct2)), Struct2)
Console.WriteLine(s2.f1 + "," + s2.f2 + "," + s2.f3 + " " + (Marshal.SizeOf(s2)).ToString)
Console.ReadLine()
Finally
Marshal.FreeHGlobal(ptrStruct)
End Try
End Sub
End Module
----------------------------------------------------------------
The first Console.Writeline outputs 'ABCDEF 6', and the second ',B,DE 6' instead of 'A,BC,DEF 6'. Clearly, I am losing the first byte of each string in Struct2. Any help? Thanks.
I solved this by:
1) Option Strict Off
2) replaced VBFixedString with VBFixedArray and adjusted upper bounds
3) replaced ByValTStr with ByValArray
4) replaced strings in structures with char()
The strings were being marshalled with a null terminator.
Dennis This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: kazack |
last post by:
Hi all it's me again with another question as I got further in my book. The
chapter I am in covers structres, abstract data and classes. I only read
through to the end of the coverage on...
|
by: Ivan |
last post by:
Hi All,
I have a problem with marshaling complex structures (containing numbers,
strings, arrays of another structures) to native C function in dll.
I have already posted same question to...
|
by: Thomas Paul Diffenbach |
last post by:
Can anyone point me to an open source library of /statically
allocated/ data structures?
I'm writing some code that would benefit from trees, preferably self
balancing, but on an embedded system...
|
by: Ken Allen |
last post by:
OK, I admit that I have been programming since before C++ was invented,
and I have developed more than my share of assembly language systems,
and even contributed to operating system and compiler...
|
by: pmclinn |
last post by:
I've noticed that many programmers use classes to store data about such
things like:
Class Customers
.....Phone
....ID
....Address
End Class....
|
by: thomasfarrow |
last post by:
At work, our development team has a development standards document that
insists Structures should never be used. I'm looking to change this
standard but need a suitable argument in order to make...
|
by: efrat |
last post by:
Hello,
I'm planning to use Python in order to teach a DSA (data structures
and algorithms) course in an academic institute. If you could help out
with the following questions, I'd sure...
|
by: svata |
last post by:
Hello,
I wonder how to resize such array of structures using realloc()?
#include <stdio.h>
#include <stdlib.h>
#define FIRST 7
typedef struct {
char *name;
|
by: Mik0b0 |
last post by:
Hallo to everyone.
This fall I am going to start data structures as a part of C language
course. The problem is I could not find any satisfying tutorial about
structures in C. There are plenty of...
|
by: Bob Altman |
last post by:
Hi all,
I have a structure that includes a constructor. I want to add a bunch of these
structures to an STL map (whose index is an int). If I define the map like
this:
map<int,...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| | |