在C++里面定义
typedef struct testStruct
{
LONG Height;
LONG Width;
BYTE log;
};
__declspec(dllimport) testStruct ReturnStruct(LONG a);__declspec(dllimport) testStruct ReturnStruct(LONG a)
{
testStruct test;
test.Height = a;
test.Width = 30L;
test.log = FALSE;
return test;
}打包成DLL以后,在C#里面用DLLImport调用这个ReturnStruct方法的话,要怎么写code呢?

解决方案 »

  1.   


            [DllImport("your.dll", CharSet = CharSet.Auto)]
            public static extern testStruct ReturnStruct(uint a);
      

  2.   

    直接这样写 testStruct 会没有定义的
      

  3.   

    .net 里面自己定义遍
    [System.Runtime.InteropServices.StructLayout
            (LayoutKind.Explicit)]
        public struct testStruct    {        [System.Runtime.InteropServices.FieldOffset(0)]
            public Int32 Height;
            [System.Runtime.InteropServices.FieldOffset(4)]
            public Int32 Width;
            [System.Runtime.InteropServices.FieldOffset(8)]
            public Byte log;
        }
      

  4.   

    c++中 long 是有符号的,注意下不能使用uint而是用int32
      

  5.   

    你在C#里也要相同定义一个struct呀。
    public struct testStruct
    {
      long Height;
      long Width;
      byte log;
    }
      

  6.   


    无法封送处理“parameter #1”: 无效的托管/非托管类型组合(该值类型必须与 Struct 成对出现)。
      

  7.   


            [StructLayout(LayoutKind.Sequential)]
            public class testStruct
            {
                public Int32 Height;
                public Int32 Width;
                public byte log;
            }这样定义就对了 然后类型匹配的问题就慢慢去研究了