批处理之家's Archiver

rat 发表于 2008-12-23 09:46

VBS调用API

[url=http://hi.baidu.com/myvbscript/blog/item/58a7c11390fa92045aaf5399.html]http://hi.baidu.com/myvbscript/b ... fa92045aaf5399.html[/url]



首先要注册一个老外([url=http://cwashington.netreach.net/]http://cwashington.netreach.net[/url])写的dynwrap.dll组件,然后就可以调用了。
[url=http://pan.baidu.com/share/link?shareid=3769086219&uk=1124163200]http://pan.baidu.com/share/link?shareid=3769086219&uk=1124163200[/url]

先看一下它的说明:

This is the dynacall.dll that I described as "bringing back the Declare
statement" in VBScript. Basically it will allow you to call functions
in other dlls...like any of the win32 api functions. This will be my
only distribution.

Many thanks to Ton Plooy and Jeff Stong who wrote the code and published
it in WDJ. And more thanks to William Epp for getting it to work with my
sample script ccupd.vbs using the GETPROFILESTRING function. However I
have another function in the sample that still doesn't work
GETPROFILESECTION. This is a work in progress but I think it is a handy
little feature. Hopefully by this distributions it will get where it
needs to be. Clarence and Ian have offered to post it on their sites so
further updates can be had at either WSH site. (anyone else call this
thing woosh? Hopefully it will perform this way).

Basically you declare functions and other DLLS like this:

'   Create the wrapper object for dynamic DLL function calling
Dim UserWrap
Set UserWrap = CreateObject("DynamicWrapper")
'   GetProcAddress for GetPrivateProfileStringA()
UserWrap.Register "kernel32.DLL", "GetPrivateProfileString", "i=ssssls",
"f=s", "r=l"


The input parameters are:

i=describes the number and data type of the functions parameters

f=type of call _stdcall or _cdecl. So it can work with both MS C++ and
Borland C++. Default to _stdcall. If that doesn't work use _cdecl. If
that doesn't work good luck!

r=return data type.


Data types are:

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

William Epp added anr 'r' for VT_BYREF (pass by reference)but is for
strings only. This made the GETPROFILESTRING function to work. But it
didn't work for the GETPROFILESECTION. If anyone gets it to work please
let me know.


"i=llll", "f=s", "R=l"


Attachments:

stong.zip - original download from WDJ
DynaWrap.zip - the modified code, the DLL with modifications.
feature.htm - the feature article for this code by Jeff Stong. Only
thing I could find on WDJ. I couldn't find Ton's article.

再举几个简单的例子:

VB中对CopyFile这个api做的说明如下:

CopyFile


VB声明
Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
说明
复制文件。与vb的filecopy命令相似
返回值
Long,非零表示成功,零表示失败。会设置GetLastError
参数表
参数 类型及说明
lpExistingFileName String,源文件名
lpNewFileName String,目标文件名
bFailIfExists Long,如果设为TRUE(非零),那么一旦目标文件已经存在,则函数调用会失败。否则目标文件被改写


所以我们的vbs的代码就是:

Set Wrap = CreateObject("DynamicWrapper")
Wrap.Register "KERNEL32.DLL", "CopyFileA","i=ssl","R=l"
a=Wrap.CopyFileA ("c:\cctv.txt","c:\22.txt",1)
If a=1 Then MsgBox "copy 成功"



————————————————————————
[color=red]ps:貌似没法使用regsvr32 /u来反注册[/color]

wxcute 发表于 2008-12-23 10:45

此DLL好像可以直接使用,直接删除,无需注册。

rat 发表于 2008-12-23 11:26

回复 2楼 的帖子

你以前注册过吧?

心愿怕人 发表于 2009-2-8 15:56

有点看不懂!不过感谢分享!

ywq111 发表于 2011-2-13 14:36

刚去官网找了,发现官网关闭了,还好论坛有dll文件,多谢了。

ywq111 发表于 2011-2-13 15:11

Register ,无效的调用过程或参数。不知道为什么出现这个错误

创建warp对象的部分不成功,在我的电脑上,xp sp3

gleox 发表于 2013-1-22 17:23

关于那个f标识,可以是哪些值了?
帮助里这么解释[quote]f=type of call _stdcall or _cdecl.[/quote]
我在很多地方都有看到“f=s”这样的字样,难道它的值就是取底线后的首个字母。还有没学过C++,不懂_stdcall 和_cdecl的区别,有懂得仁兄解释下了。

这个地址[url]http://www.script-coding.com/dynwrapx_eng.html[/url]是我在俄语站点上找到的帮助,和楼主发的DynamicWrapper不同,dll命名多了一个X(DynamicWrapperX)。
看介绍说这个dll是那个老外用GoAsm汇编语言重新编写的。DynWrapX文件尺寸仅有13Kb,与DynWrap相比小了很多。
同时这个dll用汇编编写,相信速度也要比DynWrap快很多。同时,那个站上也有DynWrap的帮助和下载。下面我将相关地址列出:

[b]帮助HTML:[/b]
DynamicWrapper [url]http://www.script-coding.com/dynwrap.html[/url]
DynamicWrapperX [url]http://www.script-coding.com/dynwrapx_eng.html[/url]


[b]dll下载地址:[/b]
DynamicWrapper_98.zip [url]http://www.script-coding.com/dynwrap95.zip[/url]
DynamicWrapper_NT.zip [url]http://www.script-coding.com/dynwrapNT.zip[/url]
DynamicWrapperX.zip [url]http://www.script-coding.com/dynwrapx1_00_eng.zip[/url]

--------------------------------------------------------------------------------------

[u]补充,不知道为什么我回帖的超链接老是加不上,我把地址补充在后面。[/u]

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.