要把一个C#的struct传递给C++写的dll,如何才能在C#中获得这个struct的IntPtr指针?

解决方案 »

  1.   

    如果有结构类型的参数,你只需定义似于的结构不就好了?
    如:
     [StructLayout(LayoutKind.Sequential)]                                 
     public  struct    RECT                                                     {                                   
       public Int16  test....
    }
    应该没必要struct转为IntPtr吧.
      

  2.   

    C++跟C#不一样反正 
    c#中这样定义 
    struct b 

      IntPtr sp; 
    } 然后在 
    str_a  ss=new str_a(); 
    IntPtr sp = Marshal.AllocCoTaskMem(Marshal.SizeOf(ss)); 
    Marshal.StructureToPtr(ss, sp, false); 
      

  3.   

    dll函数如下:
    [DllImport("Coredll.dll", CharSet = CharSet.Auto, EntryPoint = "VirtualAlloc")]
    public static extern IntPtr VirtualAlloc(
    IntPtr lpAddress,
    uint dwSize,
    uint flAllocationType,
    uint flProtect);lpAddress需要传入结构体的指针,在C#中定义如下。随便问一下,IOPreg中有一个2个值的数组,C#中的数组初始化真是复杂,而且我对这个数组初始化了,struct中的其它元素也要初始化才能编译通过,有没有简单的方法。
    public struct IOPreg{
            public uint rGPACON; // 00
            public uint rGPADAT;
            public uint[] rPAD1;        public uint rGPBCON; // 10
            public uint rGPBDAT;
            public uint rGPBUP;
            public uint rPAD2;        public uint rGPCCON; // 20
            public uint rGPCDAT;
            public uint rGPCUP;
            public uint rPAD3;        public uint rGPDCON; // 30
            public uint rGPDDAT;
            public uint rGPDUP;
            public uint rPAD4;        public uint rGPECON; // 40
            public uint rGPEDAT;
            public uint rGPEUP;
            public uint rPAD5;        public uint rGPFCON; // 50
            public uint rGPFDAT;
            public uint rGPFUP;
            public uint rPAD6;        public uint rGPGCON; // 60
            public uint rGPGDAT;
            public uint rGPGUP;
            public uint rPAD7;        public uint rGPHCON; // 70
            public uint rGPHDAT;
            public uint rGPHUP;
            public uint rPAD8;        public uint rMISCCR; // 80
            public uint rDCKCON;
            public uint rEXTINT0;
            public uint rEXTINT1;
            public uint rEXTINT2; // 90
            public uint rEINTFLT0;
            public uint rEINTFLT1;
            public uint rEINTFLT2;
            public uint rEINTFLT3; // A0
            public uint rEINTMASK;
            public uint rEINTPEND;
            public uint rGSTATUS0; // AC
            public uint rGSTATUS1; // B0
            public uint rGSTATUS2; // B4
            public uint rGSTATUS3; // B8
            public uint rGSTATUS4; // BC
            public IOPreg(uint i)
            {
                  
            rGPACON=0; // 00
             rGPADAT=0;
             rPAD1 = new uint[2];         rGPBCON=0; // 10
             rGPBDAT=0;
             rGPBUP=0;
             rPAD2=0;         rGPCCON=0; // 20
             rGPCDAT=0;
             rGPCUP=0;
             rPAD3=0;         rGPDCON=0; // 30
             rGPDDAT=0;
             rGPDUP=0;
             rPAD4=0;         rGPECON=0; // 40
             rGPEDAT=0;
             rGPEUP=0;
             rPAD5=0;         rGPFCON=0; // 50
             rGPFDAT=0;
             rGPFUP=0;
             rPAD6=0;         rGPGCON=0; // 60
             rGPGDAT=0;
             rGPGUP=0;
             rPAD7=0;         rGPHCON=0; // 70
             rGPHDAT=0;
             rGPHUP=0;
             rPAD8=0;         rMISCCR=0; // 80
             rDCKCON=0;
             rEXTINT0=0;
             rEXTINT1=0;
             rEXTINT2=0; // 90
             rEINTFLT0=0;
             rEINTFLT1=0;
             rEINTFLT2=0;
             rEINTFLT3=0; // A0
             rEINTMASK=0;
             rEINTPEND=0;
             rGSTATUS0=0; // AC
             rGSTATUS1=0; // B0
             rGSTATUS2=0; // B4
             rGSTATUS3=0; // B8
             rGSTATUS4=0; // BC
          }
      

  4.   

    如果有结构类型的参数,你只需定义似于的结构不就好了? 
    如: 
    [StructLayout(LayoutKind.Sequential)]                                
    public  struct    RECT                                                    {                                  
      public Int16  test.... 

    应该没必要struct转为IntPtr
      

  5.   

     public struct A
            {
                public int X;
                public int Y;
            }        private void button1_Click(object sender, EventArgs e)
            {
                A Test =new A();
                Test.X=100;
                Test.Y=100;
                int _StructSize =System.Runtime.InteropServices.Marshal.SizeOf(Test);            IntPtr _StuctIntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(_StructSize);            System.Runtime.InteropServices.Marshal.StructureToPtr(Test, _StuctIntPtr, true);            byte[] _Temp = new byte[8];            System.Runtime.InteropServices.Marshal.Copy(_StuctIntPtr, _Temp, 0, 8);        }
    看看这样可以不
      

  6.   

    实例个struct    然后使用ref 不就可以了.关键是C# 的STRUCT 跟C++ 里面的结构一样。
      

  7.   

    以下是MSDN的示例,创建一个托管结构,使用 StructureToPtr 方法将它传输到非托管内存,然后使用 PtrToStructure 方法将它传输回托管内存中:
    using System;
    using System.Runtime.InteropServices;public struct Point
    {
        public int x;
        public int y;
    }class Example
    {
        static void Main()
        {
            // Create a point struct.
            Point p;
            p.x = 1;
            p.y = 1;        Console.WriteLine("The value of first point is " + p.x + " and " + p.y + ".");        // Initialize unmanged memory to hold the struct.
            IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(p));        try
            {
                // Copy the struct to unmanaged memory.
                Marshal.StructureToPtr(p, pnt, false);            // Create another point.
                Point anotherP;            // Set this Point to the value of the 
                // Point in unmanaged memory. 
                anotherP = (Point)Marshal.PtrToStructure(pnt, typeof(Point));            Console.WriteLine("The value of new point is " + anotherP.x + " and " + anotherP.y + ".");        }
            finally
            {
                // Free the unmanaged memory.
                Marshal.FreeHGlobal(pnt);
            }
        }
    }
      

  8.   

    个人习惯直接传结构,使用ref
      

  9.   

    5楼正解!!!有时候C++的参数中有void *, 而且调用的时候传不同的结构体指针