Solved

C# IntPtr to struct, how to access struct data?

Posted on 2009-07-07
3
3,126 Views
Last Modified: 2013-12-17
I have a C# function that calls a C++ DLL function. I used the PInvoke wizard found here to generate the code:

http://blogs.msdn.com/vbteam/archive/2008/03/14/making-pinvoke-easy.aspx

The function returns an IntPtr to a Struct. Assuming the marshaling is already done correctly, how can I use the IntPtr to access the fields of the struct?
0
Comment
Question by:Fippy_Darkpaw
3 Comments
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24795655
You have to map the C++ struct 'structure' to a C# equivalent class.

struct  st  ==>     public class st
{                          {
   int x;                      int32 x;
}                          }


then cast the IntPtr


0
 
LVL 2

Expert Comment

by:softbee
ID: 24802271
Also, take note of the packing of the original
0
 
LVL 41

Accepted Solution

by:
graye earned 250 total points
ID: 24808355
Here is an example of the casting of a IntPtr to a Structure:
TempNet = (NETRESOURCE) Marshal.PtrToStructure(iPtr, typeof(NETRESOURCE));
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question