Properly Incrementing an IntPtr

Just as native pointer types are moved around with pointer arithmetic in native code, it can also be useful to move IntPtr types around in managed code.  Say for instance there is an IntPtr available which points to a native array of Dog instances.  To access the values of that array individually requires pointer arithmetic…


Dereference a double IntPtr

A common PInvoke question is how to deal with a double pointer.  More specifically, how can one dereference an IntPtr to another pointer without using unsafe code?  Dereferencing a double pointer is done the same way a dereference to any other structure is done: Marshal.PtrToStructure.  PtrToStructure is used to transform a native pointer, in the…


Is IntPtr(long) truncating?

The short answer is: No, not when it matters A colleague and I were discussing a particular scenario around IntPtr,PInvoke and 64 bit correctness.  Eventually our discussion lead us to the IntPtr constructor which takes a long.  To my surprise the code for the constructor is the following. public unsafe IntPtr(long value) { this.m_value =…


PInvoke and bool (or should I say BOOL)?

Boolean value types are a constant source of problems for people attempting to generate PInvoke signatures.  It’s yet another case of managed and native types differing on size.  There are two common boolean types in native code: bool and BOOL.  As with most PInvoke issue the main issue is to understand the size bool: 1…


When is a LONG not a long?

Answer: When PInvoke is involved. I ran across a common error today on stackoverflow regarding P/Invoke that is worth blogging about.  The question regarded the translation of a native API with a parameter of type LONG.  The user mistakenly used the .Net long type as the parameter.  The error is that a C++ LONG is…


PInvoke Interop Assistant on Channel 9

Beth Massi dropped by my office a couple of months ago and we did a channel 9 video on the PInvoke Interop Assistant.  Mainly an overview of the product and a bit of a tutorial.  Check out the video here.


PInvoke Interop Assistant now on CodePlex

I’m happy to announce that the PInvoke Interop Assistant tool is now available on CodePlex.  This includes the binaries, source code and the dev authored unit tests.  It is hosted on the more general CLR Interop Tools page. I will be actively maintaining this tool in the future and (hopefully) adding more features.  There…


Making PInvoke Easy

I very excited to announce we recently released a tool I’ve been working on to MSDN that will greatly help with using PInvoke in managed code.  The tool is called the “PInvoke Interop Assistant” and is included as part of a MSDN article on marshalling data for PInvoke and Reverse PInvoke scenarios.  Here is a…


Follow up: PInvoke with 32 and 64 bit

Recently after I made my original post titled “PInvoke with 32 and 64 bit”, a developer emailed me.  They pointed out that there is an internal structure that dynamically resizes based on the architecture; IntPtr.  IntPtr will be size of the native chip pointer value.  So for values like size_t that map to the pointer…


Marshaling Nested Data Structures Part 4

This is part 4 of a series.  You can find part one here.  Please refer to that article for all of the Native definitions of the structures that I use here. In the previous article we were left with a solution where using the code was very clean but the actualy implementation had extra allocation and…