473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Structure containing intptr

(Type your message here)

--------------------------------
From: Tien Pham

I am having problems with intptr. Or maybe i am not using it properly. What it is that i am having problem with is the marshaling a structure to an ptr. For instance, i have a structure declared as followed:

<StructLayout(LayoutKind.Sequential, Pack:=1)> Public Structure AG_Mem_Msg
Public dle As Byte
Public stx As Byte
Public addr As Short
Public status As Byte
Public command As Byte
Public subcode As Byte
Public param_cnt As Byte
Public byte_cnt_1 As Byte
Public mem_type As Byte
Public page_num As Byte
Public ofs_addr As Short
Public byte_cnt_2 As Byte
Public data_ptr As IntPtr
End Structure

The dataptr in this structure is the buffer that i will store my data for tranmitting out the comm port. It varies from 2 byte to a max of 243 bytes. I can assign all of the other values simply by

AG_Mem_Struct.dle = &H10
AG_Mem_Struct.stx = &H2
AG_Mem_Struct.addr = ((COMM.DRIVER_CFG.RTU_ADDR << 8) + (COMM.DRIVER_CFG.RTU_ADDR >> 8))
AG_Mem_Struct.status = 0
AG_Mem_Struct.command = &HB
AG_Mem_Struct.subcode = &H1
AG_Mem_Struct.param_cnt = &H1
AG_Mem_Struct.byte_cnt_1 = &H5
AG_Mem_Struct.mem_type = CByte(Me.cbx_MemType.SelectedIndex)
AG_Mem_Struct.page_num = CByte(Me.cbx_PageNum.SelectedIndex)
AG_Mem_Struct.ofs_addr = uiHex
AG_Mem_Struct.byte_cnt_2 = System.Convert.ToByte(Me.txt_AddCnt.Text, 16)

The question to this post is how can i store from 1....n number of bytes my data_ptr and copy it to the byte array?

I tried doing this but is failed on me.

I did not know how to index into a intptr so i had to put my data in an array the copy it to my pointer...
Dim array(1) As Byte
array(0) = &H10
Marshal.Copy(array, 0, AG_Mem_Struct.data_ptr, 2)

TMP_Q_DEF.TX_COUNT = 18 ' this is including the crc values
TMP_Q_DEF.RX_COUNT = &HFF

' i tried to copy the data to a temp array and then copy it to my buffer...
Dim TempPtr As IntPtr = Marshal.AllocHGlobal(TMP_Q_DEF.TX_COUNT)
Marshal.StructureToPtr(AG_Mem_Struct, TempPtr, True)
Marshal.Copy(TempPtr, TMP_Q_DEF.TX_PTR, 0, TMP_Q_DEF.TX_COUNT)
Marshal.FreeHGlobal(TempPtr)

' i got an error. Also, don't you need to allocate memory for the dataptr?

Thank you!
Tien

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>tpm7co272kOXe2ekQP66vA==</Id>
Jul 21 '05 #1
0 1534

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

Similar topics

8
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
9
by: Hugo Amselschlag | last post by:
Hi there, I've implemented a local system hook to suppress certain windows beeing displayed by the axWebbrowser control. Now I need some more information before I can decide, whether to suppress...
0
by: Tien Pham via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Tien Pham I am having problems with intptr. Or maybe i am not using it properly. What it is that i am having problem with is...
6
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
3
by: michelqa | last post by:
Hi, I already post a similar question last week without success. Ok I want to get the current text selection in a RICHEDIT control.. This can be easily done in C++ with EM_EXGETSEL message. I...
11
by: michelqa | last post by:
Hello, I can retrieve column text from a ListView in another process but I cant figure out how to access to structure elements (LVCOLUMN) <code> //Handle variable is a valid ListView handle ...
0
by: RobbieJosefson | last post by:
I want to marshal a structure containing a variable length array which will be populated within a C++ dll. the structure is the following Public Structure myStructure{ Public int size;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.