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

Accessing Java Classes from Mono for Android via JNI

Mono for Android let’s you write Android applications in a .NET language (for example, C#). It comes with wrappers for almost the entire Android API so building “standard” apps is easy enough. However, sometimes you may need to work with third party Java classes that aren’t part of Android itself. These classes usually come as a .jar file. Fortunately, Mono for Android provides a way to access those Java classes. Unfortunately, this way is a “little” bit tricky and documentation on this is (currently) quite limited.

This article will show some basic example of how to call a method of a Java class from Mono for Android. We will use Visual Studio 2010 in this article, as it is (in my opinion) currently superior to Mono Develop.

Update: Mono for Android v4.2 and higher provides an automatic way for doing the things described in this article. This is a lot easier than the manual way and thus the preferred way.

Read more →

Calculate return value for sort by int

Most programming languages (such as C#, Java, …) allow you to sort lists. Most of them also allow you to specify a sorting function so that you can customize the sort order. These functions usually take parameters a and b and define the return value as follows:

Return value if
less than zero a is less than b
equals zero a is equal to b
greater than zero a is greater than b

Usually you would do something like this (code in C#):

int Compare(int a, int b) {
  if (a < b) {
    return -1;
  }
  else if (a > b) {
    return 1;
  }
  else {
    return 0;
  }
}

However, when comparing int values, there’s a much quicker way to do this:

int Compare(int a, int b) {
  return a - b;
}

That’s it.

Note: Care should be taken if a and/or b can come close to int.MaxValue or int.MinValue. In this case the results may not be what one wants (like if a = int.MinValue and b = 1 then the result will be int.MaxValue which is wrong obviously).