tags:

views:

48

answers:

2

Hello,

I have not many experience in such language as C# so I would be pleased if you guys could help me. I wrote this method in C++ using MPIR library:

mpz_class SchnorrProtocol::getX(const mpz_class& r) const
{
    mpz_class x;
    mpz_powm(x.get_mpz_t(), this->params_.getBeta().get_mpz_t(), r.get_mpz_t(), this->params_.getP().get_mpz_t());
    return x;
}

and now I want to import it to C#:

  #region Filter & P/Invoke
#if DEBUG
        private const string DLL = "schnorrd.DLL";
#else
       private const string DLL = "schnorr.DLL";
#endif

        [DllImport(DLL)]
  "method definition"
        ......  SchnorrProtocol::getX(......);

my problem, I don't know how to do it. Could u please help me?

+1  A: 

You have to use structlayout attribute to define mpz_class, i.e.,

  [StructLayout(LayoutKind.Explicit, Size=16, CharSet=CharSet.Ansi)]
   public class mpz_class 
   {
      // your class definition
   }

  [StructLayout(LayoutKind.Explicit, Size=16, CharSet=CharSet.Ansi)]
   public class SchnorrProtocol 
   {
           // your class definition.
   }

And here's how you marshal a method inside a C++ class

[ DllImport( DLL, 
    EntryPoint="?getX@SchnorrProtocol@@QAEHH@Z", 
    CallingConvention=CallingConvention.ThisCall )]
    public static extern int TestThisCalling( SchnorrProtocol prot );
Ngu Soon Hui
the problem is:mpz_class is C++ class which is avalable with:#include <mpirxx.h>so the standard operators and various standard functions are overloaded to allow arithmetic with this class.I don't know how to write a definition of such class.:(
Tatiana
A: 

the problem is: mpz_class is C++ class which is avalable with: #include so the standard operators and various standard functions are overloaded to allow arithmetic with this class. I don't know how to write a definition of such class.:( –

Tatiana