DllImport
DllImport作为一种属性提供第二种方法调用不带类型库的DLL中的函数。DllImport大致与使用Declare语句等效,但对如何调用函数提供更多的控制。
基本信息
- 中文名
DllImport
- 常用方法
大多数 Windows API 调用与 DllImport 一起使用
- 用途
使用 DllImport 属性调用 Windows API
- 所属学科
计算机
基本内容
可以将大多数Windows API调用与 DllImport 一起使用,只要该调用引用的是共享(有时称为“静态”)方法就可以。不能使用需要类实例的方法。与 Declare 语句不同,DllImport 调用不能使用 MarshalAs 属性。
使用 DllImport 属性调用 Windows API
通过在“文件”菜单上单击“新建”,然后单击“项目”,打开一个新的“Windows 应用程序”项目。出现“新建项目”对话框。
从 Visual Basic 项目模板的列表中选择“Windows 应用程序”。将显示新项目。
将一个名为 Button2 的按钮添加到启动窗体上。
双击 Button2 打开窗体的代码视图。
要简化对 DllImport 的访问,请向启动窗口类的代码顶部添加一条 Imports 语句:
Visual Basic 复制代码
Imports System.Runtime.InteropServices
在 End Class 语句之前为窗体声明一个空函数,并将函数命名为 MoveFile。
将 Public 和 Shared修饰符应用到函数声明中,并基于 Windows API 函数使用的参数来设置 MoveFile 的参数:
Visual Basic 复制代码
Public Shared Function MoveFile( _
ByVal src As String, _
ByVal dst As String) _
As Boolean
' Leave the body of the function empty.
End Function