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

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 …

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

The big move

People who visit here more often probably noticed that my blog has been down a couple of times the last few months. The problem was that after giving up on hosting it on an (expensive!) windows server I tried unsuccessfully to host my blogengine.net blog on Ubuntu with Mono. I could probably get this to work but it takes too much time and effort. So I decided to move to a hosted blog on Blogger. This way I can spend more time blogging and less time on blog administration.If you’re on a stylish white blogger page right now you’re already on the new blog. Have fun and expect most of my old content to appear there over time. If you’re on my old coffee themed blog you’ll probably need to move to mendeltsiebenga.nl. Or you can wait. As soon as I have all my content moved over I’ll point mendeltsiebenga.com to the new blog too.