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

Read more →

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?

A content file required by the Help Viewer is missing or has been corrupted.

To fix this problem, I’ve attached the initial contents of the Help Viewer folder to this post.

Just extract to zip file to the local help store path. The path can be found in the registry (value name LocationPath):

  • Visual Studio 2012: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Help\v2.0\Catalogs\VisualStudio11
  • Visual Studio 2013: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Help\v2.1\Catalogs\VisualStudio12

If you can’t find the registry keys here, try HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Help\... (i.e. without the Wow6432Node).

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?

Read more →

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.

MonoDroidLibraryLoader.cs

History:

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

Selecting "Embedded Resource" as "Build Action"

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 libsqlite.so:

MonoDroidLibraryLoader.Load("sqlite", typeof(MyType));

Note: If you renamed the directory “NativeLibs” from above, you can specify it as third parameter for Load().