Posts

Showing posts from June, 2011

Using more complicated PInvoke calls

In my last blog post I covered using a very basic PInvoke call to an unmanaged dll that returned an integer. For basic data types int, char, double, float, etc you don't need to do any manual marshaling. However, especially with the Win32 API you will find yourself needing to use more complicated types. Even getting a string back from an unmanaged dll takes more work, and then we have classes, structs, arrays, etc we need to deal with.

Let's look at some unmanaged dll signatures

extern "C" { struct { char* NAME, char* ADDRESS, int age } PERSON; void FillByteArray( unsigned char* byteArray, int arrayLength ); const char* GetLastErrorMessage(); void FillAStruct( PERSON* pPerson ); }

Now here we have three functions; one fills a byte array, one returns a string, and another modifies a struct we've defined. Nothing complicated on the C++ side but we need to be careful when we start using pinvoke to call these functions. Let…