Skip to main content

PInvoke, managed and unmanaged datatypes

I'm doing a lot of PInvoking lately. One of the hard parts is mapping the unmanaged datatypes found in c++ include files to managed datatypes.

I found a nice article on codeproject with a list of the most common mappings. I'll repeat the list here so I can find it easilly, maybe other people can benefit from it too. I'll keep the list up-to-date with my own mappings and some remarks.

Unmanaged Managed Remarks
BOOL, BOOLEAN Boolean, Int32 A 32 bit field set to 0 for FALSE and 1 for TRUE
BSTR String  
BYTE SByte Signed byte, the original article has BYTE and UBYTE switched around.
CHAR Char CHAR is single byte ANSI character, Char is a dual byte Unicode character. Byte may be a better mapping.
DOUBLE Double  
DWORD UInt32 The original article also said Int32, but DWORD is always unsigned
FLOAT Single  
GUID Guid GUID is actually a c struct consisting of different integer values. The framework marshalls this to a Guid value.
HANDLE IntPtr 32 bits on x86, 64 bits on 64 bit platforms.
Also used for other handles like HFONT and HMENU
HRESULT Int32 The original artilcle also said UInt32 but HRESULT is defined as signed
INT Int32  
LANGID Int16 or UInt16  
LCID Int32 or UInt32  
LONG Int32  
LPARAM IntPtr, UIntPtr or Object Value can represent different things depending on context. 32 or 64 bits depending on platform.
LPCSTR, LPCTSTR, LPCWSTR String  
LPSTR, LPTSTR, LPWSTR String String is immutable so use StringBuilder instead of String if the string can change in unmanaged code
LPVOID IntPtr, UIntPtr or Object  
LRESULT IntPtr Value can represent different things depending on context. 32 or 64 bits depending on platform.
SAFEARRAY .NET array type  
SHORT Int16  
TCHAR Char  
UCHAR Byte  
UINT UInt32  
ULONG UInt32  
UUID Guid Not sure if this works. GUID and UUID datatype are about the same so it should.
VARIANT Object  
VARIANT_BOOL Boolean  
WCHAR Char 16 bit Unicode character
WORD UInt16 The original article also said Int32 but WORD is unsigned only.
WPARAM IntPtr, UIntPtr or Object Value can represent different things depending on context. 32 or 64 bits depending on platform.

Most of the unmanaged datatypes are defined in windef.h. Microsoft has a page on MSDN here with descriptions.

Comments

Popular posts from this blog

Using xUnit.Net with .Net 4.0

I’ve been using xUnit.Net for a while now. It’s just a tiny bit cleaner and slightly less abrasive than other .Net unit testing frameworks. Leaving out unnecessary stuff like [TestFixture] and shortening Assert.AreEqual to the equally clear but shorter Assert.Equal don’t seem like big improvements but when you type them several times a day tiny improvements start to add up. I also like the use of the [Fact] attribute instead of [Test]. It shifts the focus from testing to defining behavior. So how do we get all this goodness working with the Visual Studio 2010 beta?

Running a Git repository on Ubuntu using Gitosis

20I’ve been using Git for a couple of small projects that I’ve been hosting on github.com but version control for my bigger ‘secret’ projects still runs on a windows machine with visual svn server.Now that I’m starting to use Mono for a couple of projects so I’m playing with linux more. Last week I decided to try to try out gitosis on an ubuntu server. I found out it’s pretty easy to use when you know your way around git but for a noob like me some things weren’t immediately clear. Eventually I solved most problems I ran into, so I decided to write up the steps I took to install gitosis on ubuntu 10.04

Android development resource links

I've been playing with the Android SDK and I have a growing list of bookmarks to Android dev resources for my own use. I thought the best place to keep them would be here on my blog. That way other people can benefit too. I'll keep updating this list so feel free to add suggestions in the comments.