Solved

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

Posted on 2009-07-07
3
3,116 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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