首页 > 代码库 > VBS调用系统API

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")‘注册APIWrap.Register "KERNEL32.DLL", "Beep", "i=ll", "f=s", "r=l"‘调用APIWrap.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 charREM ‘d‘, sizeof(double), VT_R8}			// d 8 byte realREM ‘f‘, sizeof(float), VT_R4}			// f 4 byte realREM ‘k‘, sizeof(IUnknown*), VT_UNKNOWN}		// k IUnknown*REM ‘h‘, sizeof(long), VT_I4}			// h HANDLEREM ‘l‘, sizeof(long), VT_I4}			// l longREM ‘p‘, sizeof(void*), VT_PTR}			// p pointerREM ‘s‘, sizeof(BSTR), VT_LPSTR}		// s stringREM ‘t‘, sizeof(short), VT_I2}			// t shortREM ‘u‘, sizeof(UINT), VT_UINT}			// u unsigned intREM ‘w‘, sizeof(BSTR), VT_LPWSTR}		// w wide stringREM f: (Call Method)REM ‘m‘ - DC_MICROSOFT 0x0000, DefaultREM ‘b‘ - DC_BORLAND 0x0001, Borland compatREM ‘s‘ - DC_CALL_STD 0x0020, __stdcallREM ‘c‘ - DC_CALL_CDECL 0x0010, __cdeclREM ‘4‘ - DC_RETVAL_MATH4 0x0100, Return value in STREM ‘8‘ - DC_RETVAL_MATH8 0x0200, Return value in STREM 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 SubBeepMusic
‘取前景窗体标题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 = TitleEnd SubMsgBox 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, 0End SubShowDesktop