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);
        }
        
    }} 这里,传入的Point要以结构体数组传入,请问应如何写?再次感谢

解决方案 »

  1.   

    Point[] p=new Point[12];
    IntPtr   ptr     =   Marshal.UnsafeAddrOfPinnedArrayElement   (   p,0); 
      

  2.   

    sorry 没看清楚你要求。
    如果你要传递数组的话
    我能想到的办法就是unsafe了
      

  3.   

    using System;
    using System.Text;
    using System.Runtime.InteropServices;public struct Point
    {
        public Int32 x, y;
    }
    public sealed class App
    {
        static void Main()
        {
            // Demonstrate the use of public static fields of the Marshal class.
            Console.WriteLine("SystemDefaultCharSize={0}, SystemMaxDBCSCharSize={1}",
                Marshal.SystemDefaultCharSize, Marshal.SystemMaxDBCSCharSize);        // Demonstrate the use of the SizeOf method of the Marshal class.
            Console.WriteLine("Number of bytes needed by a Point object: {0}", 
                Marshal.SizeOf(typeof(Point)));
            Point p = new Point();
            Console.WriteLine("Number of bytes needed by a Point object: {0}",
                Marshal.SizeOf(p));
            
            // Demonstrate how to call GlobalAlloc and 
            // GlobalFree using the Marshal class.
            IntPtr hglobal = Marshal.AllocHGlobal(100);
            Marshal.FreeHGlobal(hglobal);        // Demonstrate how to use the Marshal class to get the Win32 error 
            // code when a Win32 method fails.
            Boolean f = CloseHandle(new IntPtr(-1));
            if (!f)
            {
                Console.WriteLine("CloseHandle call failed with an error code of: {0}", 
                    Marshal.GetLastWin32Error());
            }  
        }    // This is a platform invoke prototype. SetLastError is true, which allows 
        // the GetLastWin32Error method of the Marshal class to work correctly.    
        [DllImport("Kernel32", ExactSpelling = true, SetLastError = true)]
        static extern Boolean CloseHandle(IntPtr h);
        
    }// This code produces the following output.
    // 
    // SystemDefaultCharSize=2, SystemMaxDBCSCharSize=1
    // Number of bytes needed by a Point object: 8
    // Number of bytes needed by a Point object: 8
    // CloseHandle call failed with an error code of: 6以上是MSDN中关于Marshal类的实例,希望对你有帮助。
      

  4.   

    就是以下函数:
    int GetRegInfo( LPCSTR str_in_SERIAL_TYPE,LPCSTR str_in_SERIAL_PWLX ,LPCSTR str_in_Reg, int* i_out_recordcount ,gzyjpdf_reginfo* struct_out_reginfo )用是这样定义的:
    unsafe public static extern int GetRegInfo(string str_SERIAL_TYPE, string str_SERIAL_PWLX, string str_Reg, int* out_recordcount,reginfo[] r1);结构体如下:
    public struct reginfo
            {
                [MarshalAs(UnmanagedType.ByValTStr,SizeConst =13)]
                public string c_SERIAL_REG;         //批文编码
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 29)]
                public string c_SERIAL_TYPE;        //批文种类:1国产、2进口
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 157)]
                public string c_SERIAL_PWLX;        //批文类型代码
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)]
                public string c_REG;                //批文号
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 101)]
                public string c_YPMC;               //药品名称
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 201)]
                public string c_ENGLISH;            //英文名
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 101)]
                public string c_TRADE_NAME;         //商品名
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 156)]
                public string c_FACTORY;            //生产单位代码
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 156)]
                public string c_SPEC;               //规格代码        }程序调用如以下:
     reginfo[] reginfo1 = new reginfo[10];
     int intRetRows = 0;
     int* pRetRows = &intRetRows;
    int i111 = GetRegInfo("","","123", pRetRows, reginfo1 );
    运行成功,但reginfo1没有值,在其他语言中是可以有值的.
      

  5.   

     public static extern unsafe int GetRegInfo(string str_SERIAL_TYPE, string str_SERIAL_PWLX, string str_Reg, int* out_recordcount, ref reginfo[] r1);
      

  6.   

    返回错误信息:The runtime has encountered a fatal error. The address of the error was at 0x79e7b307, on thread 0xe7c. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.