VC6里定义的结构如下:#define S_MIN_RSA_MODULUS_BITS 508
#define S_MAX_RSA_MODULUS_BITS 1024
#define S_MAX_RSA_MODULUS_LEN ((S_MAX_RSA_MODULUS_BITS + 7) / 8)   128
#define S_MAX_RSA_PRIME_BITS ((S_MAX_RSA_MODULUS_BITS + 1) / 2)    512
#define S_MAX_RSA_PRIME_LEN ((S_MAX_RSA_PRIME_BITS + 7) / 8)       64typedef struct {
  unsigned int bits;                            //bits
  unsigned char modulus[S_MAX_RSA_MODULUS_LEN]; //n
  unsigned char exponent[S_MAX_RSA_MODULUS_LEN]; //e
} S_RSA_PUBLIC_KEY;
typedef struct {
  unsigned int bits;                            //bits
  unsigned char modulus[S_MAX_RSA_MODULUS_LEN]; //n
  unsigned char publicExponent[S_MAX_RSA_MODULUS_LEN]; //e
  unsigned char exponent[S_MAX_RSA_MODULUS_LEN]; //d
  unsigned char prime[2][S_MAX_RSA_PRIME_LEN]; //p,q
  unsigned char primeExponent[2][S_MAX_RSA_PRIME_LEN]; //dp,dq
  unsigned char coefficient[S_MAX_RSA_PRIME_LEN]; //qinv
} S_RSA_PRIVATE_KEY;VC编译的DLL文件中要用到该结构的方法如下:
S_INT S_GenerateKeyPair(
IN S_INT nKeyID,
IN OUT S_RSA_PUBLIC_KEY *pPublicKey,
OUT S_RSA_PRIVATE_KEY *pPrivateKey
);请问我应该如何在C#中正确定义该结构,把改结构正确作为参数传递给那个方法?谢谢

解决方案 »

  1.   

    这里有很多例子:
    http://msdn2.microsoft.com/en-us/library/eshywdt7.aspx
      

  2.   

    我在C#里写的结构和调用如下:
    public struct S_RSA_PUBLIC_KEY
    {
    private uint bits;          //bits
    private char[] modulus; //n
    private char[] exponent; //e public S_RSA_PUBLIC_KEY(uint bits,char[] modulus,char[] exponent)
    {
    this.bits = bits;
    this.modulus = new char[128];
    this.modulus = modulus;
    this.exponent = new char[128];
    this.exponent = exponent; }


    public struct S_RSA_PRIVATE_KEY
    {
    private uint bits;           //bits
    private char []modulus; //n  128
    private char []publicExponent; //e  128
    private char []exponent; //d  128
    private char [,]prime; //p,q  2,64
    private char [,]primeExponent; //dp,dq  2,64
    private char []coefficient; //qinv  64
    //构造函数 public  S_RSA_PRIVATE_KEY( uint bits,char[] modulus,char []publicExponent,char []exponent,char [,]prime,char [,]primeExponent,char []coefficient)
    {
    this.bits = bits;

    this.modulus = new char[128];
    this.modulus = modulus;

    this.publicExponent = new char[128];
    this.publicExponent = publicExponent;

    this.exponent = new char[128];
    this.exponent = exponent;

    this.prime = new char[2,64];
    this.prime = prime;

    this.primeExponent = new char[2,64];
    this.primeExponent = primeExponent;

    this.coefficient = new char[64];
    this.coefficient = coefficient;
    } } 
    使用该结构作为参数的方法:
    public override int DoGenerateKeyPairEx(int nKeyID, ref S_RSA_PUBLIC_KEY pPublicKey,ref  S_RSA_PRIVATE_KEY pPrivateKey)
    {
    return S_GenerateKeyPairEx(nKeyID, ref pPublicKey, ref pPrivateKey);
    }
    调用该方法:S_RSA_PUBLIC_KEY publicKey = new S_RSA_PUBLIC_KEY();
    S_RSA_PRIVATE_KEY privateKey = new S_RSA_PRIVATE_KEY();
    pci.DoGenerateKeyPairEx(keyID,ref  publicKey, ref privateKey);
    编译通过了,可是输入keyID后,出错:“无法封送类型 S_RSA_PRIVATE_KEY 的字段prime:该类型无法作为结构字段进行封送处理”
    这是什么原因?请高手指教。
      

  3.   

    这里有一个类型对应关系说明,转换应该很容易了.Wtypes.h 中的非托管类型 非托管 C 语言类型 托管类名 说明 
    HANDLE void* System.IntPtr 32 位 
    BYTE unsigned char System.Byte 8 位 
    SHORT short System.Int16 16 位 
    WORD unsigned short System.UInt16 16 位 
    INT int System.Int32 32 位 
    UINT unsigned int System.UInt32 32 位 
    LONG long System.Int32 32 位 
    BOOL long System.Int32 32 位 
    DWORD unsigned long System.UInt32 32 位 
    ULONG unsigned long System.UInt32 32 位 
    CHAR char System.Char 用 ANSI 修饰。 
    LPSTR char* System.String 或 System.StringBuilder 用 ANSI 修饰。 
    LPCSTR Const char* System.String 或 System.StringBuilder 用 ANSI 修饰。 
    LPWSTR wchar_t* System.String 或 System.StringBuilder 用 Unicode 修饰。 
    LPCWSTR Const wchar_t* System.String 或 System.StringBuilder 用 Unicode 修饰。 
    FLOAT Float System.Single 32 位 
    DOUBLE Double System.Double 64 位 
      

  4.   

    我给的链接里面有很多例子,特别是关于struct的,你看看就明白了。
      

  5.   

    以前做过C++和C#的网络通信,也遇过这样的问题.
    如果C#的结构体和类差不多,好像不固定长度,而且C#的类型和C++的不同,所以只好把全部数据转成BYTE类型,按长度发过去了.
      

  6.   

    在VC中如果声明字符串数组,在C#里面要这样写:[MarshalAs(UnmanagedType.ByValTStr, SizeConst = ***)]
    public string strName;至于unsigned char是不是也是这样,你自己试试吧