VFW基础知识(一些定义性质的。从CSDN中得到的。)

 

指示该属性化方法由非托管动态链接库 (DLL) 作为静态入口点公开。

命名空间:  System.Runtime.InteropServices
程序集:  mscorlib(在 mscorlib.dll 中)

语法

C#

[ComVisibleAttribute(true)]

[AttributeUsageAttribute(AttributeTargets.Method, Inherited = false)]

public sealed class DllImportAttribute : Attribute

 

备注

可将该属性应用于方法。

DllImportAttribute 属性提供对从非托管 DLL 导出的函数进行调用所必需的信息。作为最低要求,必须提供包含入口点的 DLL 的名称。

可直接将此属性应用于 C# C++ 方法定义;但在使用 Declare 语句时,Visual Basic 编译器会忽略此属性。对于包含 BestFitMappingCallingConventionExactSpellingPreserveSigSetLastError ThrowOnUnmappableChar 字段的复杂方法定义,可直接将此属性应用于 Visual Basic 方法定义。

注意JScript 不支持此属性。可以使用 C# Visual Basic 包装类从 JScript 程序访问非托管 API 方法。

有关使用平台调用服务访问非托管 DLL 中的函数的其他信息,请参见 使用非托管 DLL 函数。

注意:

DllImportAttribute 不支持泛型类型的封送处理。

示例

下面的代码示例演示如何使用 DllImportAttribute 属性导入 Win32 MessageBox 函数。 然后,代码示例将调用导入的方法。

 

 

 

C# 

复制代码

using System;

using System.Runtime.InteropServices;

 

class Example

{

    // Use DllImport to import the Win32 MessageBox function.

    [DllImport("user32.dll", CharSet = CharSet.Auto)]

    public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

 

    static void Main()

    {

        // Call the MessageBox function using platform invoke.

        MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);

    }

}

 

继承层次结构

System..::.Object
  System..::.Attribute
    
System.Runtime.InteropServices..::.DllImportAttribute

线程安全

此类型的任何公共 static(在 Visual Basic中为 Shared 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

平台

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

.NET Framework .NET Compact Framework 并不是

 

extern拆分词条

extern

求助编辑百科名片

extern可以置于变量或者函数前,以表示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义。另外,extern也可用来进行链接指定。

 

 

 

 

 

 

LayoutKind 枚举

 

 

控制当导出到非托管代码时对象的布局。

 

此枚举与 StructLayoutAttribute 一起使用。

 

可将该属性应用于类或结构。

通常,公共语言运行库控制类或结构的数据字段在托管内存中的物理布局。如果类或结构需要按某种方式排列,则可以使用 StructLayoutAttribute。如果要将类传递给需要指定布局的非托管代码,则显式控制类布局是重要的。LayoutKind Sequential 用于强制将成员按其出现的顺序进行顺序布局。Explicit 控制每个数据成员的精确位置。如果使用 Explicit,则每个成员必须使用 FieldOffsetAttribute 来指示该字段在类型中的位置。

默认情况下,C#Visual Basic .默认情况下,NET C++ 编译器将 Sequential 布局值应用于结构。对于类,必须显式应用 Sequential 值。类型库导入程序 (Tlbimp.exe) 也应用此属性;它总是在导入类型库时应用 Sequential 值。

 

 

 

下面的示例演示 PtInRect 函数的托管声明,该声明检查一个点是否位于矩形内,并定义一个具有 Sequential 布局的 Point 结构和一个具有 Explicit 布局的 Rect 结构。

C# 

复制代码

enum Bool

{

   False = 0,

   True

};

[StructLayout(LayoutKind.Sequential)]

public struct Point

{

   public int x;

   public int y;

}  

 

[StructLayout(LayoutKind.Explicit)]

public struct Rect

{

   [FieldOffset(0)] public int left;

   [FieldOffset(4)] public int top;

   [FieldOffset(8)] public int right;

   [FieldOffset(12)] public int bottom;

}  

 

class LibWrapper

{

   [DllImport("user32.dll", CallingConvention=CallingConvention.StdCall)]

   public static extern Bool PtInRect(ref Rect r, Point p);

};

 

class TestApplication

{

   public static void Main()

   {

      try

      {

         Bool bPointInRect = 0;

         Rect myRect = new Rect();

         myRect.left = 10;

         myRect.right = 100;

         myRect.top = 10;

         myRect.bottom = 100;

         Point myPoint = new Point();

         myPoint.x = 50;

         myPoint.y = 50;

         bPointInRect = LibWrapper.PtInRect(ref myRect, myPoint);

         if(bPointInRect == Bool.True)

            Console.WriteLine("Point lies within the Rect");

         else

            Console.WriteLine("Point did not lies within the Rect");

      }

      catch(Exception e)

      {

         Console.WriteLine("Exception : " + e.Message);

      }

   }

}

 

 

 

 

UnmanagedType 枚举

指定如何将参数或字段封送到非托管代码。

System.Runtime.InteropServices..::.MarshalAsAttribute 中使用 UnmanagedType 枚举,以指定在与非托管代码进行交互的过程中如何对类型进行封送处理。可以使用此枚举对使用简单值类型(I1I2I4I8R4R8U2U4 U8)、.NET Framework 中未提供的非托管类型以及各种杂项类型的代码进行封送处理。

有关更多信息,请参见 与非托管代码交互操作。

示例

下面的代码段演示如何在托管源代码中声明由 COM 组件实现的非托管接口。System.Runtime.InteropServices..::.ComImportAttribute 防止 IMyStorage 接口被导回由 COM 使用。(COM 客户端应当直接使用现有 COM 组件。)在此示例中,MarshalAsAttribute 指定若干 UnmanagedType 成员,这些成员表示由原始 COM 接口使用的类型。

C# 

复制代码

using System;

using System.Runtime.InteropServices;

 

namespace MyModule

{

      // If you do not have a type library for an interface

      // you can redeclare it using ComImportAttribute.

 

      // This is how the interface would look in an idl file.

 

      //[

      //object,

      //uuid("73EB4AF8-BE9C-4b49-B3A4-24F4FF657B26"),

      //dual,    helpstring("IMyStorage Interface"),

      //pointer_default(unique)

      //]

      //interface IMyStorage : IDispatch

      //{

      //    [id(1)]

      //    HRESULT GetItem([in] BSTR bstrName, [out, retval] IDispatch ** ppItem);

      //    [id(2)]

      //    HRESULT GetItems([in] BSTR bstrLocation, [out] SAFEARRAY(VARIANT)* pItems);

      //    [id(3)]

      //    HRESULT GetItemDescriptions([in] BSTR bstrLocation, [out] SAFEARRAY(VARIANT) ** ppItems);

      //    [id(4), propget]

      //    HRESULT get_IsEmpty([out, retval] BOOL * pfEmpty);

      //};

 

      // This is the managed declaration.

 

      [ComImport]

      [Guid("73EB4AF8-BE9C-4b49-B3A4-24F4FF657B26")]

      public interface IMyStorage 

      {

           [DispId(1)]

           [return : MarshalAs( UnmanagedType.Interface )]

           Object GetItem( [In, MarshalAs( UnmanagedType.BStr )] String bstrName );

 

           [DispId(2)]

           void GetItems( [In, MarshalAs( UnmanagedType.BStr )] String bstrLocation,

                 [Out, MarshalAs( UnmanagedType.SafeArray,

                              SafeArraySubType = VarEnum.VT_VARIANT )] out Object[] Items );

 

 

           [DispId(3)]

           void GetItemDescriptions( [In] String bstrLocation,

                 [In, Out, MarshalAs( UnmanagedType.SafeArray )] ref Object[] varDescriptions );

 

           bool IsEmpty

           {

                 [DispId(4)]

                 [return : MarshalAs( UnmanagedType.VariantBool )]

                 get;

           }

      }

}

 

 

原文地址:https://www.cnblogs.com/mahaisong/p/2153661.html