473,395 Members | 1,972 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,395 software developers and data experts.

How to Marshal an IntPtr containing one more IntPtr

Hi All,
I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out.

I have an structure something like:

public struct Detail
{
public int age;

public Detail(int _age)
{
age = _age;
}
}

public struct Name
{
public IntPtr names;
}


While trying to Marshal the above code I am marshalling the Object of Detail struct using IntPtr Names: i.e,

Name abc = new Name();

Detail obj = new Detail(2);
int sizeDetailobj = Marshal.SizeOf(typeof(Detail));

abc.names= Marshal.AllocHGlobal(abc.names* Marshal.SizeOf(typeof(Detail)));
Marshal.StructureToPtr(obj, (IntPtr)(abc.names.ToInt32() + sizeDetailobj), false);



Furthurmore I need to pass the object of struct Name which contains the IntPtr to struct Detail along with some other fields.

as,

int size = Marshal.SizeOf(typeof(Name));
IntPtr data = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(abc, data, true);


Here marshalling happens well without any problem,

For sending the message over Network i am using BEEP Protocol, which converts data into byte[]

as byte[] dataBytes = new byte[size];
Marshal.Copy(data, dataBytes, 0, size);


And at the receiving end while unmarshalling,
I am unmarshalling As;

IntPtr proxyCtx ; //which gets the databyte value

Name namebuf = (Name)Marshal.PtrToStructure(proxyCtx, typeof(Name)); // At this step i got the desired result as IntPtr abc.names

but the IntPtr received itself contains an IntPtr to struct Detail, while unmarshalling;

Detail obj1 = new Detail();
int structSize = Marshal.SizeOf(typeof(Detail));
obj1 = (Detail)Marshal.PtrToStructure(namebuf.names, typeof(Detail));


At this point i was suppose to get the value of age in obj1.age field but instead i am getting some garbage values.

Plz Suggest what i am suppose to do to achieve required value.

Thanks in Advance,
Eric Haas.
Sep 18 '08 #1
0 2086

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: William Stacey | last post by:
The following code works, but I can't figure out why. I take a struct with two members, a single byte and byte. I then marshal the whole struct to a byte. I create a new struct (without init'ing...
2
by: Stefan | last post by:
I have the following question The Marshal class contains a function to allocate a block of memory 1. Marshal.AllocHGlobal ( int cb 2. Marshal.AllocHGlobal ( IntPtr cb The first version I can...
1
by: dhornyak | last post by:
I have been banging my head against the wall for a while now, and can't seem to id the problem. I've been through a ton of posts and the code doesn't seem any different. Can anybody see it? When...
13
by: Just Me | last post by:
The following almost works. The problem is Marshal.PtrToStringAuto seems to terminate at the first null so I don't get the full string. Any suggestions on how to fix this? Or how to improve the...
14
by: Vertilka | last post by:
I need to read binary data file written by C++ program, using my C# application. How do i marshal the bytes i read with my C# code to .NET types. The data is numbers (integers float doubles...
6
by: william.thorpe.b | last post by:
I have recently switched from VS2003 to VS2005 and at the same time from V1 to V2 of the .NET Compact Framework. The target is a Windows CE 5.0 device and an ARMV4I processor. ...
1
by: nicewenyan | last post by:
I want to pass a managed c# byte (8 bit) array into a unmanaged c++ function: extern "C" void AddData(unsigned int* data); I use P/Invoke on managed side to do the marshaling as following: ...
4
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
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
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...
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.