Windows脚本编程核心技术精解Chapter03.pdf
《Windows脚本编程核心技术精解Chapter03.pdf》由会员分享,可在线阅读,更多相关《Windows脚本编程核心技术精解Chapter03.pdf(32页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 3Kidnapping ObjectsIn This Chapter?Find out more about objects?Connect to objects and list all available objects on your system?Analyze the inner structure of objects?Read secret TypeLibrary information to retrieve the commands stored inobjectsWhy should you deal with objects?Well,because VB
2、Script is merelysoftware glue.You can write little scripts that do useful things,but to really accomplish powerful automation tasks,VBScript is definitely notenough.In this chapter,all you need to do is sit back and enjoy the ride.This tour will show you how to get all the information you need to un
3、derstand scriptdevelopmentand Ill show you how to find out the names of all the hiddenmethods stored inside objects.Eventually,all of this information will fall into place without much brainwork.You want action fast?Then fast forward to Part II.You already knoweverything you need to execute scripts.
4、Part II is loaded with everydayexamples you can try out and use as a foundation for your own experiments.Do come back later,though,to find out all about hidden COM objects andhow to access their methods.What Objects Really AreVBScript only delivers the basic infrastructure.You can define decisions,f
5、unctions,and procedures,but VBScript has no command to access any ofthe interesting parts of your computer.These include reading values from theWindows Registry,for example,or checking for available disk space.The realwork has to be done by someone different.Fortunately,VBScript contains one very po
6、werful commandCreateObject.This command can access COM components installed on your system and“borrow”all the commands stored inside of this component.This is anincredible source of power.4684-8 ch03.f.qc 3/3/00 9:31 AM Page 85There is a lot of confusion about the technical terms.COM objects storefu
7、nctionality in a language-independent format.Some books refer to COMobjects as ActiveX objects;others use the old term OLE2.Just think of COMobjects as black boxes that contain useful stuff.You dont need to know howthe COM object achieves its results.As you will see,almost any modernsoftware stores
8、its functionality in a number of COM objects.These modulescan be accessed by anyone.Basically,through the use of COM objects,your scripts can do just abouteverything a regular program can do.There arent limits anymore.And evenbetter,you dont need to waste your time programming all of these functions
9、yourself.In fact,you dont even have to know much about programming.Chances are there are already predefined ActiveX objects on your system,bethey database access,Internet FTP transfer,or scanner control.Just pick theright ActiveX component and have your script boss it around.Thats the funpart about
10、scriptingyou get incredible power,yet you dont have to inventall the programs and functions yourself.Instead,borrow whatever functionalityyou need from whats already installed on your system.Recycle your software,and use its power to suit your needs.How Can I Talk To COM Objects?How do you identify
11、a COM object?COM objects come in two flavorseitherOCXor DLLfiles.To get a taste of how many potentially useful resources arewaiting for you,do a quick search:Open your Start menu,choose Find,anddo a search on files and folders.In the filename box,type in*.DLL*.OCX.Make sure you choose your hard driv
12、e as the search target.Hundreds of fileswill be listed,leaving you wondering which secrets await you inside.OCXfiles are ActiveX controls.They were originally designed to be hosted byWeb pages.DLLfiles are dynamic link libraries.They help to split programs(and Windows itself)into modules.Each DLLfil
13、e stores some functionality,and whenever more is needed,Microsoft and other companies add more DLLs.Both file types may or may not be suitable for scripting.In order for scripts toaccess the functions inside,the file needs to offer a special interface calledIDispatch.In addition,the file needs to be
14、 registered.You will learn moreabout this later in this chapter.COM objects work pretty much like VBScript command extensions.Plugthem in,and you get an extra set of commands.Plug in as many objects asyou need simultaneously.Its really that easy.Connecting to an ObjectYou want proof?Take a look at t
15、he following example.VBScript has no filesystem commands whatsoever,so it would be pretty useless if you wanted todesign your own disk utility.Thats why Microsoft added a special object to86Part I:Scripting KickstartII4684-8 ch03.f.qc 3/3/00 9:31 AM Page 86the scripting host called Scripting.FileSys
16、temObject.Plug it in,and youget all the file system commands you could possibly ask for:3-1.VBS get access to the ActiveX object:set fs=CreateObject(“Scripting.FileSystemObject”)call a method to make the object do something:set drive=fs.GetDrive(“C:”)MsgBox“Available Space on C:”&FormatNumber(drive.
17、AvailableSpace/10242)&“MB”This script determines the free space on drive C:(see Figure 3-1).To be able to do that,the script needs to borrow some functions from Scripting.FileSystemObject.So,the first thing the script does is call for help.It usesCreateObjectto get a reference to the object it wants
18、 to help outScripting.FileSystemObject.Figure 3-1:Find out available space on a drive with the help ofScripting.FileSystemObject.Your scripts will call for help all of the time.Their cries will be answered by many different objects throughout this book,so its a good idea to fullyunderstand this mech
19、anism.CreateObjectreturns a reference to the object.Because this reference is not a regular variable,like a number or a text string,you need to use the key word Setto assign it to a variable.The variable thenacts like the object,and the script above can use the variable fsto accessmethods and proper
20、ties of the new object.Setis a very important key word.VBScript doesnt need it,but you do.UseSetwhenever you want to store an object reference in a variable.Whenever you want to access the inside of the object,you just use yournewly created reference.The script wants to get a hold of a drive,so it c
21、allsGetDrive.GetDriveis part of the Scripting.FileSystemObject;its not a VBScript command.So,in order to call this function,you need to use yourreference stored in fs.This is why the script calls fs.GetDriveinstead ofGetDrive.Your script now has successfully called its first foreign function.GetDriv
22、ereturns another object called IDrive.This object represents the drive with all its technical details.You will see much more about thesefunctions in Chapter 8.Because GetDrivereturns an object,your scriptagain uses Setto store the object reference in a variable.Chapter 3:Kidnapping Objects87II4684-8
23、 ch03.f.qc 3/3/00 9:31 AM Page 87To find out the available space on the drive,the script asks for the driveproperty AvailableSpace.Because this information is stored in the IDriveobject,the script again uses the correct object reference.It asks for drive.AvailableSpace.Either AvailableSpacealone or
24、fs.AvailableSpacewould fail because this property only exists in the IDriveobjectandnowhere else.If youre wondering how to find out which object contains which information,its not black magic;its just a matter of information and documentation.Youwill see in a moment how easy it is to find out which
25、objects are returned andwhat information is contained in them.The FormatNumbercommand just serves cosmetic purposes.It formats theraw number,which is divided by 10242 to get megabytes instead of bytes.Whats all this fuss about“methods,”“properties,”and“functions?”Actually,a method is just a new term
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Windows 脚本 编程 核心技术 Chapter03
限制150内