打印本文 打印本文 关闭窗口 关闭窗口
Visual Basic调用Windows API函数的应用举例
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1747  更新时间:2009/4/23 18:58:02  文章录入:mintao  责任编辑:mintao

Visual Basic调用Windows API函数的应用举例

【 摘 要】
       本 文 介 绍 了 利 用Visual Basic 调 用 Windows API 函 数 的 方 法, 并 通 过 举 例 介 绍 部 分

       API 函 数 的 功 能 。 例 如:VB 程 序 私 有 初 始 化 参 数 的 存 取, 及 当 前 系 统 信 息 的 检 测。

       【 关 键 字】
       Windows Microsoft( 微 软) 公 司 的 视 窗 系 统。
       Visual Basic(VB) Microsoft( 微 软) 公 司 的 可 视 化 编 程 工 具。
       API(Application Program Interface) 应 用 程 序 接 口。
       初 始 化 文 件( 例 如:DEMO.INI)

       【 正 文 】

一、 利 用VB 调 用API 函 数 的 方 法。

       (1) 用Declare 语 句 申 明 所 要 调 用 的API 函 数, 若 该 函 数 务 返 回 值, 可 申 明 为Sub 过 程; 若 有 返 回 值, 则 可 申 明 为Function 函 数。

       注: 所 有 的API 函 数 的 申 明 都 在 .\VB\WINAPI\WIN31API.HLP 中, 只 需 用Copy 和 Paste 的 方 法 即 可 放 到 相 应 的 地 方。

       (2) 一 旦 申 明 了 某 一 个API 函 数 后, 就 可 以 象 调 用VB 的 函 数 一 样。 但 需 注 意, 如 果 参 数 传 递 不 对, 可 能 会 导 致 死 机。

二、 VB 程 序 私 有 初 始 化 参 数 的 存 取。

       Windows 软 件 的 初 始 化 参 数 的 获 取 与 保 存 是 通 过 读 取 扩 展 名 为 .INI 的 文 本 文 件 来 实 现 的, 即 程 序 运 行 前 先 从 指 定 的INI 文 件 中 搜 索 到 所 需 的 参 数, 并 反 映 到 程 序 的 运 行 环 境 中; 当 程 序 退 出 时 又 将 当 前 的 环 境 参 数 保 存 到 指 定 的INI 文 件 中。Windows 提 供 的API 函 数 中 的GetPrivateProfileString 和WritePrivateProfileString 就 有 这 些 功 能。

       说 明:
       (1) GetPrivateProfileString 声 明:Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String,ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

       功 能: 获 取INI 文 件 中 与 指 定 关 键 字 对 应 的 参 数( 字 符 串 性) 并 将 此 参 数 拷 贝 到lpReturnedString 中。

       参 数
       类 型
       说 明
       lpApplicationName
       String
       待 寻 找 的 关 键 字 所 在 的 段
       lpKeyName
       String
       与 参 数 相 对 应 的 关 键 字 名
       lpDefault
       String
       指 定 的 关 键 字 不 存 在 时 返 回 的 缺 省 值
       lpReturnedString
       String
       预 先 分 配 好 的 长 度 至 少 为nSize 字 节 的 字 符 串 缓 冲 区
       nSized
       Integer
       将 要 装 入lpReturnedString 缓 冲 区 的 最 大 字 符 数
       lpFileName
       String
       初 始 化 文 件 的 名 字
       返 回 值
       Integer
       拷 贝 到lpReturnedString 缓 冲 区 的 字 符 数

       (2)WritePrivateProfileString
       声 明:Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

       功 能: 在 初 始 化 文 件 的 指 定 关 键 字 项 内 设 置 参 数( 字 符 串)。
       返 回 值: 如 果 设 置 成 功, 返 回TRUE; 否 则 返 回FALSE。

三、 VB 检 测 当 前 系 统 信 息

       所 需 的API 函 数 有GetWindowsDirectory,GetWinFlags,GetVersion,GetSystemDirectory, 等, 具 体 的 使 用 方 法 可 见 举 例。

四、 举 例

       DEMO.PRJ

       ( 一) 建 立 项 目 文 件DEMO.PRJ; 建 立 模 块 文 件DEMO.BAS. 并 将 下 列 声 明 插 入 到DEMO.BAS 中。

       Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

       Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

       Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
       Declare Function GetKeyboardType Lib "user32" (ByVal nTypeFlag As Long) As Long
       Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
       Declare Function GetVersion Lib "kernel32" () As Long
       Declare Function GetWinflags Lib "kernel32" () As Long

       注:

  1. GetWindowsDirectory: 该 函 数 获 取Windows 目 录 的 路 径。
  2. GetSystemDirectory: 该 函 数 获 取Windows 系 统 子 目 录 的 路 径。
  3. GetVersion: 该 函 数 返 回 当 前Windows 版 本 号 和DOS 版 本 号。 返 回 值 的 低 位 字 节 说 明Windows 主 版 本 号, 返 回 值 的 低 位 字 的 高 位 字 节 说 明Windows 副 版 本 号, 高 位 字 的 低 位 字 节 说 明DOS 副 版 本 号, 高 位 字 的 高 位 字 节 说 明DOS 主 版 本 号。

       4、 GetWinflags: 该 函 数 返 回Windows 运 行 系 统 上 的 内 存 配 置。

       返 回 标 志 值
       含 意
       WF_80X87
       Intel 数 字 协 处 理 器
       WF_CPU386
       80386 CPU
       WF_CPU486
       80486 CPU
       WF_ENHANCED
       Windows 系 统 运 行 在386 增 强 模 式
       WF_PMODE
       Windows 系 统 运 行 在 保 护 模 式
       WF_STANDARD
       Windows 系 统 运 行 在 标 准 模 式
       WF_WLO
       运 行 在OS/2 下

       5、GetKeyboardType(): 该 函 数 得 到 系 统 键 盘 类 型,nTypeFlag=0 时 返 回 键 盘 类 型。
       返 回 值
       含 意
       1
       IBM PC/XT 或 兼 容 键 盘
       2
       Olivetti "ICO" 键 盘(102 个 键)
       3
       IBM 或 兼 容 键 盘(84 个 键)
       4
       IBM 增 强 型 或 相 似 键 盘(101 或102 个 键)
       5
       Nokia 1050 或 相 似 键 盘
       6
       Nokia 9140 或 相 似 键 盘
       7
       日 本 键 盘

       6、 为 了 调 用 这 些API 函 数 更 加 方 便, 可 以 编 写 一 些 包 含 函 数。 这 些 用 户 自 定 义 函 数 的 功 能 也 需 要 在 模 块 中。


Function SysDir()
Temp = Space$(255)
StringLen = GetSystemDirectory(Temp, 255)
SysDir = Left$(Temp, StringLen)
End Function
___________________________________________________________________________
Function WinDir() As String
Temp = Space$(255)
StringLen = GetWindowsDirectory(Temp, 255)
WinDir = Left$(Temp, StringLen)
End Function
___________________________________________________________________________
Function KeyType()
KbType = GetKeyboardType(0)
Select Case KbType
       Case 1
       KeyType="IBM PC\XT,or compatible(83key)"
       Case 2
       KeyType="Olivetti ''''ICO''''(102key)"
       Case 3
       KeyType="IBM AT,or similar(84key)"
       Case 4
       KeyType = "IBM Enhance (101 or 102 keys)"
       Case 5
       KeyType = "Nokia 1050 or similar"
       Case 6
       KeyType = "Nokia 1050 or

[1] [2]  下一页

打印本文 打印本文 关闭窗口 关闭窗口