Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3177
  • Last Modified:

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

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
Fippy_Darkpaw
Asked:
Fippy_Darkpaw
1 Solution
 
williamcampbellCommented:
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
 
softbeeCommented:
Also, take note of the packing of the original
0
 
grayeCommented:
Here is an example of the casting of a IntPtr to a Structure:
TempNet = (NETRESOURCE) Marshal.PtrToStructure(iPtr, typeof(NETRESOURCE));
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Tackle projects and never again get stuck behind a technical roadblock.
Join Now