VBS调用系统API

如Beep的API声明为

Public Declare Function Beep Lib “kernel32″ Alias “Beep” (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long

则使用方法如下

'创建对象
Set Wrap = CreateObject("DynamicWrapper")
'注册API
Wrap.Register "KERNEL32.DLL", "Beep", "i=ll", "f=s", "r=l"
'调用API
Wrap.Beep(500, 100)

其中注册API中, “i=ll”是API的参数类型, “f=s”是调用方式, “r=l”是返回类型
这些可以参考下面的说明。

REM i: (Argument Type)
REM 'a', sizeof(IDispatch*), VT_DISPATCH}	// a IDispatch*
REM 'c', sizeof(unsigned char), VT_I4}		// c signed char
REM 'd', sizeof(double), VT_R8}			// d 8 byte real
REM 'f', sizeof(float), VT_R4}			// f 4 byte real
REM 'k', sizeof(IUnknown*), VT_UNKNOWN}		// k IUnknown*
REM 'h', sizeof(long), VT_I4}			// h HANDLE
REM 'l', sizeof(long), VT_I4}			// l long
REM 'p', sizeof(void*), VT_PTR}			// p pointer
REM 's', sizeof(BSTR), VT_LPSTR}		// s string
REM 't', sizeof(short), VT_I2}			// t short
REM 'u', sizeof(UINT), VT_UINT}			// u unsigned int
REM 'w', sizeof(BSTR), VT_LPWSTR}		// w wide string

REM f: (Call Method)
REM 'm' - DC_MICROSOFT 0x0000, Default
REM 'b' - DC_BORLAND 0x0001, Borland compat
REM 's' - DC_CALL_STD 0x0020, __stdcall
REM 'c' - DC_CALL_CDECL 0x0010, __cdecl
REM '4' - DC_RETVAL_MATH4 0x0100, Return value in ST
REM '8' - DC_RETVAL_MATH8 0x0200, Return value in ST

REM r: (Return Type)
REM Same as i

示例:

'用机箱内的蜂鸣器播放音乐
Sub BeepMusic()
	Set Wrap = CreateObject("DynamicWrapper")
	Wrap.Register "KERNEL32.DLL", "Beep", "i=ll", "f=s", "r=l"
	res = Wrap.Beep(500, 100)
	res = Wrap.Beep(550, 100)
	res = Wrap.Beep(600, 100)
	res = Wrap.Beep(650, 100)
	res = Wrap.Beep(700, 700)
	WScript.Sleep 200
	res = Wrap.Beep(700, 100)
	res = Wrap.Beep(650, 100)
	res = Wrap.Beep(600, 100)
	res = Wrap.Beep(550, 100)
	res = Wrap.Beep(500, 700)
End Sub
BeepMusic
'取前景窗体标题
Sub GetForeWindowCaption()
	Const WM_GETTEXT = &HD
	Set Wrap = CreateObject("DynamicWrapper")
	Wrap.Register "USER32.DLL", "GetForegroundWindow", "f=s", "r=l"
	Wrap.Register "USER32.DLL", "SendMessage", "i=lllr", "f=s", "r=l"
	Title = Space(100)
	res = Wrap.SendMessage(Wrap.GetForegroundWindow(), WM_GETTEXT , 100, Title)
	GetForeWindowCaption = Title
End Sub
MsgBox GetForeWindowCaption
'发送键盘消息,显示桌面
Sub ShowDesktop()
	Const VK_LWIN = &H5B
	Const VK_D = &H44
	Public Const KEYEVENTF_KEYUP = &H2
	Set Wrap = CreateObject("DynamicWrapper")
	Wrap.Register "USER32.DLL", "keybd_event", "i=ccll", "f=s"
	Wrap.keybd_event VK_LWIN, 0, 0, 0
	Wrap.keybd_event VK_D, 0, 0, 0
	Wrap.keybd_event VK_D, 0, KEYEVENTF_KEYUP, 0
	Wrap.keybd_event VK_LWIN, 0, KEYEVENTF_KEYUP, 0
End Sub
ShowDesktop
posted @ 2014-08-12 12:13  狂师  阅读(6622)  评论(0编辑  收藏  举报