Using native library (NDK) from a C# project

edited June 2013 in Native

I am wondering whether it is possible to import the native library (the NDK one) and use it from within a C# project (using COM interop) ?

The C# SDK and the native SDK are similar but not 100% identical. Allowing to use the same library in both scenarios can help us reduce the amount of code needed...


  • Hi liortal53.

    With COM in general using our C++ lib from within C# code should work, but may be quite some work for writing the managed C++ wrapper DLL, marshal the types, etc.:

    However on Android you would have to use Mono instead of .NET and I don't know, how good their COM-interop support is by now (do they have one at all? The latest official info that I can find, is that it's a missing feature, but that info has been for Mono 1.x).

    You could also instead of COM just use Monos "normal" interop functionality: ... _Libraries
    That won't work out of the box, either. You would have to write a C wrapper interface first, that provides an API with which Monos native import functionality (which is only able to import native global functions, so for example no classes or objects) could deal with. Also in this case you will have to marshal the types

    I guess that that wouldn't result in less work than just using both libs.

    A third option may be to write your complete network code as a native dll with a thin interface through which your C# game code could interact with it.

    About marshaling:
    I would approach that task by using the Serializer and Deserializer interface of our C++ client libs to read and write Photons complex data types from/into byte-arrays, which would be transferred between the C# and the C++ layer. Our C# libs also offer a interface for serialization/deserialization and the serialized data is in the same format, that the Photon clients use, to communicate with the Photon Server/Cloud and which is understood by the Photon clients of all platforms (with limitations for some languages like Flash, JS or Lua, but not for C# or C++), so this way it should be easy to convert for example a C# Dictionary, Hashtable, String or Array into a C++ one and vice versa.

    Would you like to explain the scenario, for which you intend to use this?
  • Which C# framework do you use to develop for Android and which other platforms do you want to cover?
  • The third option sounds closest to what i envisioned.

    Our game has an "editor" like experience on PC (.NET based) with the actual game running on Android devices.

    I'm thinking whether we can have the same native library in both scenarios (with a thin layer on top perhaps on the PC side), this can save some work for doubling the code on both platform...
  • In that case on PC you would use our Windows C++ SDK, not the AndroidNDK one, but as long as you use the same version for both platforms, the API will be identical between these two, so that you could reuse your networking code.
  • Great, thanks :)
Sign In or Register to comment.