日期:2014-05-20  浏览次数:21006 次

从C#程序中调用非受管DLLs,关于属性[StructLayout(LayoutKind.Sequential)]
大家都知道,如果我们为了从C#   中调用非受管DLLs函数,首先必须要有一个声明,在C#中使用的是DllImport关键字,例如下面的例子:
using   System.Runtime.InteropServices;   //   DllImport所在的名字空间
[StructLayout(LayoutKind.Sequential)]
      public   struct   POINT   {
            public   POINT(int   xx,   int   yy)   {   x=xx;   y=yy;   }
            public   int   x;
            public   int   y;
            public   override   string   ToString()   {
                  String   s   =   String.Format( "({0},{1}) ",   x,   y);
                  return   s;
            }
      }

      [StructLayout(LayoutKind.Sequential)]
      public   struct   SIZE   {
            public   SIZE(int   cxx,   int   cyy)   {   cx=cxx;   cy=cyy;   }
            public   int   cx;
            public   int   cy;
            public   override   string   ToString()   {
                  String   s   =   String.Format( "({0},{1}) ",   cx,   cy);
                  return   s;
            }
      }

      [StructLayout(LayoutKind.Sequential)]
      public   struct   RECT   {
            public   int   left;
            public   int   top;
            public   int   right;
            public   int   bottom;
            public   int   Width()             {   return   right   -   left;   }
            public   int   Height()           {   return   bottom   -   top;   }
            public   POINT   TopLeft()     {   return   new   POINT(left,top);   }
            public   SIZE     Size()           {   return   new   SIZE(Width(),   Height());   }
            public   override   string   ToString()   {
                  String   s   =   String.Format( "{0}x{1} ",   TopLeft(),   Size());
                  return   s;
            }
      }

public   class   Win32   {
    [DllImport( "User32.Dll ")]
    public   static   extern   int   GetWindowRect(int   hwnd,   ref   RECT   rc);