473,839 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Struct trouble Visual Basic.net

Hi there !!!

I browsed around the Internet in search for a solution of a little difficult
problem i have in VB.NET....

However, i cannot find a suitable anwser anywhere, so i thought i'll give it
a try here...

Okay, here's the deal:
I am trying to read from unmanaged memory with a class type struct, this
works fine as i can read the data from the unmanaged memory. The trouble
starts when i try to fill an array of structs within a struct, all the array
members get filled with the last read values from the unmanaged memory:

The structs i build for managing the unmanaged memory;
<code>
Imports System.Runtime. InteropServices

'Beckhoff TwinCat Struct

'TYPE ST_TeleMeBool :
'STRUCT
' ID:STRING;
' Value:BOOL;
' Qualify:STRING;
' TypeB:STRING;
'END_STRUCT
'END_TYPE

<StructLayout(L ayoutKind.Seque ntial, pack:=1)> _
Public Class ST_TeleMeBool
'-- enum unmanagedtype ByValStr = a fixed size string of 80 chars
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=80)> _
Public ID As String = ""
<MarshalAs(Unma nagedType.I1)> _
Public Value As Boolean
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=80)> _
Public Qualify As String = ""
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=80)> _
Public TypeB As String = ""

End Class
'Beckhoff TwinCat Struct

'TYPE ST_TelemeReal :
'STRUCT
' ID:STRING;
' Value:REAL;
' Qualify:STRING;
' TypeB:STRING;
'END_STRUCT
'END_TYPE

<StructLayout(L ayoutKind.Seque ntial, pack:=1)> _
Public Class ST_TeleMeReal

<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=80)> _
Public ID As String = ""
'-- enum unmanagedtype r4 = a 4 byte Real (aka Single / system.float)
<MarshalAs(Unma nagedType.R4)> _
Public Value As Single
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=80)> _
Public Qualify As String = ""
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=80)> _
Public TypeB As String = ""

End Class
'Beckhoff TwinCat Struct

'TYPE ST_TelemeOut :
'STRUCT
' CountBool:INT;
' CountReal:INT;
' bArray: ARRAY[0..999] OF ST_TelemeBool;
' rArray: ARRAY[0..999] OF ST_TelemeReal;
'END_STRUCT
'END_TYPE

<StructLayout(L ayoutKind.Seque ntial, pack:=1)> _
Public Class ST_TelemeOut

'-- enum unmanagedtype I2 = a 2 byte signed integer (aka short /
system.int16)
<MarshalAs(Unma nagedType.I2)> _
Public CountBool As Integer
<MarshalAs(Unma nagedType.I2)> _
Public CountReal As Integer
'-- enum unmanagedtype ByValArray = an array of which the maximum
elementsize MUST be specified (sizeconst)
<MarshalAs(Unma nagedType.ByVal Array, SizeConst:=999) > _
Public bArray(1000) As ST_TeleMeBool
<MarshalAs(Unma nagedType.ByVal Array, SizeConst:=999) > _
Public rArray(1000) As ST_TeleMeReal

End Class
</code>
Now the code to read the unmanaged code and fill my struct:

<code>

Public stTelemeOut As ST_TelemeOut = New ST_TelemeOut '--
declares a VB side St_TelemeOut struct to fill from PLC TelemeOut struct

Public stTelemeBool As ST_TeleMeBool = New ST_TeleMeBool '-- vb
sided instance of a single .bArray() struct element within St_TelemeOut, may
be useful

Public stTelemeReal As ST_TeleMeReal = New ST_TeleMeReal

For i = 0 To .CountBool - 1
stTelemeBool.ID = ADSClient.ReadA ny(hbID(i),
GetType(String) , New Integer() {80}) '--
WATCH SYNTAX TO ASK STRINGS!!!!
stTelemeBool.Va lue = ADSClient.ReadA ny(hbValue(i),
GetType(Boolean ))
stTelemeBool.Qu alify = ADSClient.ReadA ny(hbQualify(i) ,
GetType(String) , New Integer() {80})
stTelemeBool.Ty peB = ADSClient.ReadA ny(hbTypeB(i),
GetType(String) , New Integer() {80})
.bArray(i) = stTelemeBool
Next i
For i = 0 To .CountReal - 1
stTelemeReal.ID = ADSClient.ReadA ny(hrID(i),
GetType(String) , New Integer() {80})
stTelemeReal.Va lue = ADSClient.ReadA ny(hrValue(i),
GetType(Single) )
stTelemeReal.Qu alify = ADSClient.ReadA ny(hrQualify(i) ,
GetType(String) , New Integer() {80})
stTelemeReal.Ty peB = ADSClient.ReadA ny(hrTypeB(i),
GetType(String) , New Integer() {80})
.rArray(i) = stTelemeReal
Next i
</code>

The problem i encounter resides in the statement:
bArray(i) = stTelemeBool

and (equally so):

rArray(i) = stTelemeReal

ALL the array members get filled with the last read data values from the
unmanaged memory, this instead of filling each array member with the
corresponding unamanaged memory data.

I am at a loss... can anybody help me out??
Nov 21 '05 #1
4 4034
Couple of questions here
when i = 0 and you check the value of hbID is it the same when i=ubound(i)
if that is the case then your hb object is not getting declared or populated
correct
if they are not the same then take a look at
since your not rediming your arrays then that could be reason.. All though
never seen that to be a problem but
redim array(ubound(.c ountreal))
before you start looping to fill your array

you do not show us how hb is getting filled
If the other component is filling hb.. When you get hb back make sure it is
more then array of 1

"DaHool" <Da****@discuss ions.microsoft. com> wrote in message
news:F0******** *************** ***********@mic rosoft.com...
Hi there !!!

I browsed around the Internet in search for a solution of a little
difficult
problem i have in VB.NET....

However, i cannot find a suitable anwser anywhere, so i thought i'll give
it
a try here...

Okay, here's the deal:
I am trying to read from unmanaged memory with a class type struct, this
works fine as i can read the data from the unmanaged memory. The trouble
starts when i try to fill an array of structs within a struct, all the
array
members get filled with the last read values from the unmanaged memory:

The structs i build for managing the unmanaged memory;
<code>
Imports System.Runtime. InteropServices

'Beckhoff TwinCat Struct

'TYPE ST_TeleMeBool :
'STRUCT
' ID:STRING;
' Value:BOOL;
' Qualify:STRING;
' TypeB:STRING;
'END_STRUCT
'END_TYPE

<StructLayout(L ayoutKind.Seque ntial, pack:=1)> _
Public Class ST_TeleMeBool
'-- enum unmanagedtype ByValStr = a fixed size string of 80 chars
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=80)> _
Public ID As String = ""
<MarshalAs(Unma nagedType.I1)> _
Public Value As Boolean
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=80)> _
Public Qualify As String = ""
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=80)> _
Public TypeB As String = ""

End Class
'Beckhoff TwinCat Struct

'TYPE ST_TelemeReal :
'STRUCT
' ID:STRING;
' Value:REAL;
' Qualify:STRING;
' TypeB:STRING;
'END_STRUCT
'END_TYPE

<StructLayout(L ayoutKind.Seque ntial, pack:=1)> _
Public Class ST_TeleMeReal

<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=80)> _
Public ID As String = ""
'-- enum unmanagedtype r4 = a 4 byte Real (aka Single / system.float)
<MarshalAs(Unma nagedType.R4)> _
Public Value As Single
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=80)> _
Public Qualify As String = ""
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=80)> _
Public TypeB As String = ""

End Class
'Beckhoff TwinCat Struct

'TYPE ST_TelemeOut :
'STRUCT
' CountBool:INT;
' CountReal:INT;
' bArray: ARRAY[0..999] OF ST_TelemeBool;
' rArray: ARRAY[0..999] OF ST_TelemeReal;
'END_STRUCT
'END_TYPE

<StructLayout(L ayoutKind.Seque ntial, pack:=1)> _
Public Class ST_TelemeOut

'-- enum unmanagedtype I2 = a 2 byte signed integer (aka short /
system.int16)
<MarshalAs(Unma nagedType.I2)> _
Public CountBool As Integer
<MarshalAs(Unma nagedType.I2)> _
Public CountReal As Integer
'-- enum unmanagedtype ByValArray = an array of which the maximum
elementsize MUST be specified (sizeconst)
<MarshalAs(Unma nagedType.ByVal Array, SizeConst:=999) > _
Public bArray(1000) As ST_TeleMeBool
<MarshalAs(Unma nagedType.ByVal Array, SizeConst:=999) > _
Public rArray(1000) As ST_TeleMeReal

End Class
</code>
Now the code to read the unmanaged code and fill my struct:

<code>

Public stTelemeOut As ST_TelemeOut = New ST_TelemeOut '--
declares a VB side St_TelemeOut struct to fill from PLC TelemeOut struct

Public stTelemeBool As ST_TeleMeBool = New ST_TeleMeBool '-- vb
sided instance of a single .bArray() struct element within St_TelemeOut,
may
be useful

Public stTelemeReal As ST_TeleMeReal = New ST_TeleMeReal

For i = 0 To .CountBool - 1
stTelemeBool.ID = ADSClient.ReadA ny(hbID(i),
GetType(String) , New Integer() {80}) '--
WATCH SYNTAX TO ASK STRINGS!!!!
stTelemeBool.Va lue = ADSClient.ReadA ny(hbValue(i),
GetType(Boolean ))
stTelemeBool.Qu alify = ADSClient.ReadA ny(hbQualify(i) ,
GetType(String) , New Integer() {80})
stTelemeBool.Ty peB = ADSClient.ReadA ny(hbTypeB(i),
GetType(String) , New Integer() {80})
.bArray(i) = stTelemeBool
Next i
For i = 0 To .CountReal - 1
stTelemeReal.ID = ADSClient.ReadA ny(hrID(i),
GetType(String) , New Integer() {80})
stTelemeReal.Va lue = ADSClient.ReadA ny(hrValue(i),
GetType(Single) )
stTelemeReal.Qu alify = ADSClient.ReadA ny(hrQualify(i) ,
GetType(String) , New Integer() {80})
stTelemeReal.Ty peB = ADSClient.ReadA ny(hrTypeB(i),
GetType(String) , New Integer() {80})
.rArray(i) = stTelemeReal
Next i
</code>

The problem i encounter resides in the statement:
bArray(i) = stTelemeBool

and (equally so):

rArray(i) = stTelemeReal

ALL the array members get filled with the last read data values from the
unmanaged memory, this instead of filling each array member with the
corresponding unamanaged memory data.

I am at a loss... can anybody help me out??

Nov 21 '05 #2
> starts when i try to fill an array of structs within a struct, all
the array
members get filled with the last read values from the unmanaged memory:

The problem is that you reuse the same instance of the object every
time through the loop. Your rArray was filled with references to the
*SAME* object. You need to create a NEW object each time inside the
for loops, like this:
Public stTelemeBool As ST_TeleMeBool
Public stTelemeReal As ST_TeleMeReal

For i = 0 To .CountBool - 1 stTelemeBool = New ST_TelemeOut 'CREATE A NEW OBJECT
HERE

'Code to set stTelemeBool properties
.bArray(i) = stTelemeBool
Next i
For i = 0 To .CountReal - 1 stTelemeReal = New ST_TeleMeReal 'CREATE A NEW OBJECT
HERE

'Code to set stTelemeReal properties
.rArray(i) = stTelemeReal
Next i
</code>


Once you create the objects inside the for loops you should be ok

Nov 21 '05 #3
that is not the problem if you look at the code below he
makes stTelemeReal = the array at the end of the loop
So does not matter if he resues the stTelemeReal
over and over in his loops

"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
starts when i try to fill an array of structs within a struct, all

the array
members get filled with the last read values from the unmanaged

memory:

The problem is that you reuse the same instance of the object every
time through the loop. Your rArray was filled with references to the
*SAME* object. You need to create a NEW object each time inside the
for loops, like this:
Public stTelemeBool As ST_TeleMeBool
Public stTelemeReal As ST_TeleMeReal

For i = 0 To .CountBool - 1

stTelemeBool = New ST_TelemeOut 'CREATE A NEW OBJECT
HERE

'Code to set stTelemeBool properties
.bArray(i) = stTelemeBool
Next i
For i = 0 To .CountReal - 1

stTelemeReal = New ST_TeleMeReal 'CREATE A NEW OBJECT
HERE

'Code to set stTelemeReal properties
.rArray(i) = stTelemeReal
Next i
</code>


Once you create the objects inside the for loops you should be ok

Nov 21 '05 #4
It does matter, when he assigns the object reference to the array in
this line
.rArray(i) = stTelemeReal


He now has two refereneces *to the same object*. At the end of the
loop, *all* the array elements point to the same object. And since
there is only one object, they all reflect the values of the most
recent changes.

Had he defined his "structs" as Structures instead of classes it may be
different, since he would then be working with value types and not
reference types.

Chris

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
10410
by: Roland Dreier | last post by:
I'm used to initializing struct fields by name using the designator syntax that is a part of ISO C99. For example: struct foo { int x; int y; int z; }; struct foo a = {
4
2434
by: quek | last post by:
Im having a problem passing a struct ptr. Maybe someone could help me. Heres what Visual C gives me as error: error C2664: 'adpcm_coder_u' : cannot convert parameter 4 from 'struct main::$S1 *' to 'struct adpcm_state *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1
2092
by: Russell Mangel | last post by:
Sorry about the Cross-Post, I posted my question in the wrong group. Hello, What is the simplest way to create a dynamic collection (during run-time), using basic C (Struct data types). Since I am doing C++/CLI interop I wish to avoid using vector class. I am using Visual Studio 2005 C++/CLI, and I am writing an Un-Managed class library. The project type will be a static library (possibly a dll). I have
3
3214
by: Sudheer Gupta | last post by:
Hi, I am having trouble using C struct in python. Hope anyone can help me out ... Say, I have my C struct as typedef struct call { struct call *next;
11
1962
by: nephish | last post by:
hello there, all. i have a difficult app that connects to a server to get information for our database here. this server is our access point to some equipment in the field that we monitor. the messages come in over a socket connection. And according to their (very limited) documentation, are set up in a particular order. like this
1
2271
by: stromhau | last post by:
Hi, I have made a few classes in c++. They somehow cooperate doing some 3d stuff. Basically it is a moving camera acting as a flight, i have placed a lot of objects around the scene together with a b-spline surfcae. The problem class is the polygon class. this class read 3d files generates faces, edges and so on. here are two of the building blocks(structs) struct FACE{
1
2151
by: yucikala | last post by:
Hello, I'm a "expert of beginner" in C#. I have a dll - in C. And in this dll is this struct: typedef struct msg_s { /* please make duplicates of strings before next call to emi_read() ! */ int op_type; /* of "op_t" type: operation type; submit (>0), response (<0) */
2
14920
by: yalbizu | last post by:
#include <iostream> #include <string> #include <fstream> #include <iomanip> using namespace std; const int NO_OF_STUDENTS=20; struct studentType { string studentFName; string studentLName;
4
1854
by: Lew Pitcher | last post by:
(having trouble getting my reply through - hopefully, third time is a charm) On November 11, 2008 19:53, in comp.lang.c, BIll Cunningham (nospam@nspam.invalid) wrote: Why an int? Because K&R chose to use an int. Why not a double? Because K&R chose to use an int. Note the phrase "syntactically analogous". It means that the /syntax/ of
0
9856
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10913
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10299
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7834
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5684
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.