Windows脚本可以调用MSWinsock控件获得比较强大的网络访问能力,但那个106KB的mswinsck.ocx(V6.0.89.88)不是系统自带的,这使得该控件的实用性大打折扣。而且,脚本要调用MSWinsock还必须克服一个困难。
以"MSWinsock"为关键字,使用百度搜索网页,可以获得至少3个VBS调用MSWinsock控件的例子:获取本机IP、端口扫描和发在"黑防"的《VBS+MSWinsock打造灵巧UDP后门》。但是,当你下载并注册mswinsck.ocx,然后尝试那些代码时,却会得到一个"类别未授权使用(Class is not licensed for use)"错误,错误号0x80040112。那些代码的作者似乎都以为MSWinsock和一般的控件一样,只要用regsvr32注册就能正常使用了,没有实际测试它的可移植性。而转贴文章的人,更不会关心内容是否可靠,一味地复制粘贴而已。结果,网上所有关于VBS调用MSWinsock的例子都是"空中楼阁",没有一个跑的起来。国内如此,国外也一样。所有对该控件无法使用的质疑,都被人以"需要注册"为理由打发了。当然,也有了解"真相"的人,但其给出的解决方案竟然是用VB重新写个ocx封装MSWinsock,这实在是@#$%^&!。
那么,"真相"究竟是什么?搜索"类别未授权使用"和错误号,这是个好主意,但结果不理想。我们需要逆向思考,为什么原作者不会遇到错误。答案是原作者的mswinsck.ocx不是单独注册的,而是随VB一起安装的。其中的差别在于,后者会安装VB控件的设计时许可证(design-time license)。ActiveX控件的License有两种:设计时(design-time)和运行时(run-time)。当编译调用了某个控件的exe/dll/ocx时,编译器会自动加入该控件的运行时许可证,所以"重新写个ocx封装MSWinsock"这个办法可行。使用脚本直接调用MSWinsock时没有运行时许可证,这时脚本宿主会尝试在注册表中获取设计时许可证,单独注册的MSWinsock没有相应的设计时许可证,所以就出错了。
在VS.NET安装盘里有VB6控件的授权密钥(/Extras/VB6 Controls/VB6Controls.reg),导入注册表后就能正常使用包括MSWinsock在内的VB6控件了。
附 VB6Controls.reg 内容如下:
REGEDIT
HKEY_CLASSES_ROOT/Licenses = Licensing: Copying the keys may be a violation of established copyrights.
// Masked Edit Control 6.0 license key
HKEY_CLASSES_ROOT/Licenses/BC96F860-9928-11cf-8AFA-00AA00C00905 = mmimfflflmqmlfffrlnmofhfkgrlmmfmqkqj
// Chart Control 6.0 (OLEDB) license key
HKEY_CLASSES_ROOT/Licenses/12B142A4-BD51-11d1-8C08-0000F8754DA1 = aadhgafabafajhchnbchehfambfbbachmfmb
// Common Dialog Control 6.0 license key
HKEY_CLASSES_ROOT/Licenses/4D553650-6ABE-11cf-8ADB-00AA00C00905 = gfjmrfkfifkmkfffrlmmgmhmnlulkmfmqkqj
// ADO Data Control 6.0 (OLEDB) license key
HKEY_CLASSES_ROOT/Licenses/C4145310-469C-11d1-B182-00A0C922E820 = konhqhioohihphkouimonhqhvnwiqhhhnjti
// Common Controls-3 6.0 license key
HKEY_CLASSES_ROOT/Licenses/38911DA0-E448-11D0-84A3-00DD01104159 = mcpckchcdchjcjcclidcgcgchdqdcjhcojpd
// Windows Common Controls-2 5.0 (SP2) license key
HKEY_CLASSES_ROOT/Licenses/9E799BF1-8817-11cf-958F-0020AFC28C3B = uqpqnqkjujkjjjjqwktjrjkjtkupsjnjtoun
// Windows Common Controls license key
HKEY_CLASSES_ROOT/Licenses/57CBF9E0-6AA7-11cf-8ADB-00AA00C00905 = aahakhchghkhfhaamghhbhbhkbpgfhahlfle
// Data Bound Grid Control 5.0(SP3) license key
HKEY_CLASSES_ROOT/Licenses/556C75F1-EFBC-11CF-B9F3-00A0247033C4 = xybiedobrqsprbijaegcbislrsiucfjdhisl
// Data Bound List Controls 6.0 license key
HKEY_CLASSES_ROOT/Licenses/096EFC40-6ABF-11cf-850C-08002B30345D = knsgigmnmngnmnigthmgpninrmumhgkgrlrk
// Internet Transfer Control 6.0 license key
HKEY_CLASSES_ROOT/Licenses/78E1BDD1-9941-11cf-9756-00AA00C00908 = yjrjvqkjlqqjnqkjvprqsjnjvkuknjpjtoun
// Multimedia Control 6.0 license key
HKEY_CLASSES_ROOT/Licenses/B1EFCCF0-6AC1-11cf-8ADB-00AA00C00905 = qqkjvqpqmqjjpqjjvpqqkqmqvkypoqjquoun
// Chart Control 6.0 license key
HKEY_CLASSES_ROOT/Licenses/7C35CA30-D112-11cf-8E72-00A0C90F26F8 = whmhmhohmhiorhkouimhihihwiwinhlosmsl
// Windows Common Controls-2 6.0 license key
HKEY_CLASSES_ROOT/Licenses/4F86BADF-9F77-11d1-B1B7-0000F8753F5D = iplpwpnippopupiivjrioppisjsjlpiiokuj
// Windows Common Controls 6.0 license key
HKEY_CLASSES_ROOT/Licenses/ED4B87C4-9F76-11d1-8BF7-0000F8754DA1 = knlggnmntgggrninthpgmnngrhqhnnjnslsh
// Comm Control 6.0 license key
HKEY_CLASSES_ROOT/Licenses/4250E830-6AC2-11cf-8ADB-00AA00C00905 = kjljvjjjoquqmjjjvpqqkqmqykypoqjquoun
// DataGrid Control 6.0 (OLEDB) license key
HKEY_CLASSES_ROOT/Licenses/CDE57A55-8B86-11D0-b3C6-00A0C90AEA82 = ekpkhddkjkekpdjkqemkfkldoeoefkfdjfqe
// DataList Control 6.0 (OLEDB) license key
HKEY_CLASSES_ROOT/Licenses/A133F000-CCB0-11d0-A316-00AA00688B10 = cibbcimbpihbbbbbnhdbeidiocmcbbdbgdoc
// DBWin license key
HKEY_CLASSES_ROOT/Licenses/D015B071-D2ED-11d0-A31A-00AA00688B10 = gjdcfjpcmjicjcdcoihcechjlioiccechepd
// MSDBRPT license key
HKEY_CLASSES_ROOT/Licenses/9DF1A470-BA8E-11D0-849C-00A0C90DC8A9 = cchcqjejhcgcqcfjpdfcdjkckiqikchcojpd
// FlexGrid Control 6.0 license key
HKEY_CLASSES_ROOT/Licenses/72E67120-5959-11cf-91F6-C2863C385E30 = ibcbbbebqbdbciebmcobmbhifcmciibblgmf
// MAPI Controls 6.0 license key
HKEY_CLASSES_ROOT/Licenses/899B3E80-6AC6-11cf-8ADB-00AA00C00905 = wjsjjjlqmjpjrjjjvpqqkqmqukypoqjquoun
// MSRDO 2.0 license key
HKEY_CLASSES_ROOT/Licenses/B1692F60-23B0-11D0-8E95-00A0C90F26F8 = mjjjccncgjijrcfjpdfjfcejpdkdkcgjojpd
// RemoteData Control 6.0 license key
HKEY_CLASSES_ROOT/Licenses/43478d75-78e0-11cf-8e78-00a0d100038e = imshohohphlmnhimuinmphmmuiminhlmsmsl
// Windowless Controls 6.0 license key
HKEY_CLASSES_ROOT/Licenses/80E80EF0-DBBE-11D0-BCE2-00A0C90DCA10 = qijimitpmpnpxplpvjnikpkpqoxjmpkpoivj
// PictureClip Control 6.0 license key
HKEY_CLASSES_ROOT/Licenses/6FB38640-6AC7-11cf-8ADB-00AA00C00905 = gdjkokgdldikhdddpjkkekgknesjikdkoioh
// Rich TextBox Control 6.0 license key
HKEY_CLASSES_ROOT/Licenses/DC4D7920-6AC8-11cf-8ADB-00AA00C00905 = iokouhloohrojhhhtnooiokomiwnmohosmsl
// Sheridan Tab Control license key
HKEY_CLASSES_ROOT/Licenses/190B7910-992A-11cf-8AFA-00AA00C00905 = gclclcejjcmjdcccoikjlcecoioijjcjnhng
// SysInfo Control 6.0 license key
HKEY_CLASSES_ROOT/Licenses/E32E2733-1BC5-11d0-B8C3-00A0C90DCA10 = kmhfimlflmmfpffmsgfmhmimngtghmoflhsg
// Winsock Control 6.0 license key
HKEY_CLASSES_ROOT/Licenses/2c49f800-c2dd-11cf-9ad6-0080c7e7b78d = mlrljgrlhltlngjlthrligklpkrhllglqlrk