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.
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  
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.


Popular posts from this blog

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 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

Square One available on the Android market

This is just a short post to let you know that a first version of the Android app I’ve been working on for the last couple of weeks is available on the Android market. The app is called Square One and it’s a simple bassline synthesizer. It’s free so try it out and let me know what you think of it, but be prepared it’s still an early version. I hope to add more features in the next few months and maybe build something that can be used to create real music.The lower part of the screen contains the sequencer controls that can be used to program your own bass lines. On the left is a four by four grid of buttons where you can select a step in the sequence. On the right you can select the note to be played on that step. When you’re done you can press Start and the sequence starts playing. The knobs on the top can be used to control a couple of parameters from the synthesizer engine that creates the sound. You can control the cutoff frequency and resonance of the low-pass filter, attack and …

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.