473,406 Members | 2,371 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,406 software developers and data experts.

Get IntPtr to Current Object?

I have a class that has an Explicit StructLayout. In the constructor
below, I am copying data from a byte array onto this class so as to
initialize all the explicit attributes of the class. What I don't
like is the overhead of allocating memory for a temporary variable
(pData) when I want to marshal the data in the rawData array directly
into the current object. Would there be a way to get an IntPtr
directly to the current object?

public EntityState(byte[] rawData)
{
IntPtr pData = Marshal.AllocHGlobal(rawData.Length);
Marshal.Copy(rawData, 0, pData, 144);
Marshal.PtrToStructure(pData, this);
Marshal.FreeHGlobal(pData);
}
Jul 11 '08 #1
2 7902
On Jul 11, 9:17 am, "O.B." <funkj...@bellsouth.netwrote:
I have a class that has an Explicit StructLayout. In the constructor
below, I am copying data from a byte array onto this class so as to
initialize all the explicit attributes of the class. What I don't
like is the overhead of allocating memory for a temporary variable
(pData) when I want to marshal the data in the rawData array directly
into the current object. Would there be a way to get an IntPtr
directly to the current object?

public EntityState(byte[] rawData)
{
IntPtr pData = Marshal.AllocHGlobal(rawData.Length);
Marshal.Copy(rawData, 0, pData, 144);
Marshal.PtrToStructure(pData, this);
Marshal.FreeHGlobal(pData);

}
Actually, I think this will work. Any gotchas that I should be aware
of?

public EntityState(byte[] rawData)
{
unsafe
{
fixed (byte* pData = rawData)
{
Marshal.PtrToStructure((IntPtr)pData, this);
}
}
}
Jul 11 '08 #2
On Jul 11, 6:17*pm, "O.B." <funkj...@bellsouth.netwrote:
I have a class that has an Explicit StructLayout. *In the constructor
below, I am copying data from a byte array onto this class so as to
initialize all the explicit attributes of the class. *What I don't
like is the overhead of allocating memory for a temporary variable
(pData) when I want to marshal the data in the rawData array directly
into the current object. *Would there be a way to get an IntPtr
directly to the current object?
You can't get a pointer to an object, but you can just get a plain
pointer to the first data member (using operator unary & as usual),
which, if you did the layout correctly, should be equivalent. Just
don't forget to pin it using "fixed". A pointer can then be cast to
IntPtr.
Jul 11 '08 #3

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

Similar topics

2
by: Nuno Esculcas | last post by:
Hello, I come from C++ and i now have to work with C#, and someone tell me that bye bye pointers but i think this is not true, i must convert a DIB image in something that i can use in C# (like...
3
by: Ron Vecchi | last post by:
I am creating my own MainMenu control similar to VS.Net. (yes I know theirs some out their). I have predominatly been a web developer for about 5 years and am playing with Windows forms. So this...
13
by: Christian Westerlund | last post by:
Hi! I'm trying to use P/Invoke and a Method which takes an IntPtr where I am supposed to put an address to a method which the native method will use to communicate back to me. How do I convert a...
5
by: Sakharam Phapale | last post by:
Hi All, I am using Microsoft Speech DLL "sapi.dll". I want to use following method of "SpeechLib.SpStream" object (following is a signature which I copied from Quick Info feature of .NET code...
3
by: Richards | last post by:
The .NET documentation has the following statement about "The IntPtr type can be used ...... as a common means of referring to data between languages that do and do not support pointers." Can...
0
by: Jørn Jensen | last post by:
Hey! I have an issue with creating bitmap from an IntPtr. The IntPtr is created from a Twain scanner, which seem to work perfectly in it's original state (the app is originally written by somebody...
8
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The...
1
by: Beorne | last post by:
I have imported a corporate image handling COM object in my C# project. To access in a fast way the memory of the image there is a method that returns a pointer to the memory (in byte) of the...
0
by: Charming12 | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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...

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.