Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Porting C++ native code

$
0
0

I have a great deal of C++ code that I want to use in an Android app, preferably without having to re-write the whole thing in a C# Portable Class Library.

I would like explicit instructions on how to get this code to be referenceable from the Android app. I see documentation that says "use P/Invoke", and some more that says use CppSharp, but my attempts at getting this working on my own have failed because the Android app still seems to require a native library, so I must be missing something important.

I am currently using Visual Studio 2012, but am willing to upgrade to 2013 if necessary. The C++ code is just a bunch of files and is not currently actually compiled into any sort of library (dynamic or static), so if there are project settings for building the c++ library that will make the process smoother (for example, using /clr:pure or something) please mention those as well. I am also able to do some rewriting of the c++ code to make it compatible, but if the amount of rewrite becomes more than the effort of making a PCL, then we'll be going that route instead.

I have created test code that I would like to get working before I start building the whole library. If having a specific example helps, please use this class:

in TestCpp.h:

namespace TestCppLib
{
  typedef void (_stdcall *drawrect) (float x, float y, float h, float l, float w);

  class TestCpp
  {
  private:
      int length;

  public:
      TestCpp(void);
      ~TestCpp(void);

      int Hello1(void);
      const char* Hello2(const char* instr);

      int GraphicsTest(drawrect g2);
  };
}

in TestCpp.Cpp:

#include TestCpp.h
namespace TestCppLib
{
  TestCpp::TestCpp(void)
  {
      length=-1;
  }


  TestCpp::~TestCpp(void)
  {
  }

  int TestCpp::Hello1()
  {
      return length;
  }

  const char *TestCpp::Hello2(const char *instr)
  {
      length++;
      return instr;
  }

  int TestCpp::GraphicsTest(drawrect g2)
  {
      g2(2,2,5,10,1);
      return 1;
  }
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>