Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-07-07
3
Medium Priority
?
3,160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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:…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

664 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