P/Invoke tries to make your life easier by automatically converting (“marshalling”) data types from managed code to native code and the other way around.
P/Invoke Tutorial: Pinning (Part 4)
Sometimes a C/C++ function needs to store data you pass to it for later reference. If such data is a managed object (like a
class) you need to make sure that the garbage collector doesn’t delete it while it’s still used/stored in the native code.
That’s what pinning is for. It prevents the garbage collector from deleting and moving the object.
Bug of the Day: Help Viewer is missing some files
Nice error message, but could you please tell me which content files are missing/corrupted or how to fix this problem?
To fix this problem, I’ve attached the initial contents of the Help Viewer folder to this post.
- HelpViewer20-VS2012.zip (Help Viewer 2.0; Visual Studio 2012)
HelpViewer21-VS2013.zip (Help Viewer 2.1; Visual Studio 2013)
Just extract to zip file to the local help store path. The path can be found in the registry (value name
- Visual Studio 2012:
Visual Studio 2013:
If you can’t find the registry keys here, try
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Help\... (i.e. without the
P/Invoke Tutorial: Passing strings (Part 2)
In the previous tutorial we passed a single string to a native C/C++ function by using P/Invoke.
This function was defined like this:
// C++ void print_line(const char* str);
// C# [DllImport("NativeLib.dll")] private static extern void print_line(string str);
However, there exists a hidden pitfall here:
What happens when the user passes a non-ASCII character to this function?
Native libraries in MonoDroid library projects
Currently, MonoDroid (Mono for Android) doesn’t support native libraries (or any other Android resource type) in library projects (bug report). Fortunately, they support normal .NET assembly resources. This led me to write a workaround for this missing feature.
The Class ∞
Here’s the class that implements to loader.
- 2012-07-18: Updates documentation and added some logging
2012-07-11: Initial revision
How to use ∞
To use this class, you first need to create a directory structure for the native libraries, like this:
Project +- NativeLibs +- armeabi-v7a +- armeabi +- other ABIs you want to support
Now place the native libraries in the apropriate directories, for example:
Project +- NativeLibs +- armeabi-v7a | +- libsqlite.so +- armeabi +- libsqlite.so
Then select “Embedded Resource” as Build Action in the file’s properties (Hotkey: F4).
Now use one of the
Load() methods to load the library. For example, if you have a type called
MyType in the project’s default namespace (specified in the project settings), you can use this call to load a library called
Note: If you renamed the directory “NativeLibs” from above, you can specify it as third parameter for