Marshalling data for C#
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"
开发者_StackOverflow中文版...... SchnorrProtocol::getX(......);
my problem, I don't know how to do it. Could u please help me?
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 );
精彩评论