《COM 教程 计算机 英文.docx》由会员分享,可在线阅读,更多相关《COM 教程 计算机 英文.docx(52页珍藏版)》请在优知文库上搜索。
1、IntroductionFormanypeople,learningCOMandDCOMistough.YouknowthatlearningCOMistherightthingtodo-youhearconstanthypeandyouknowthatmanyofMicrosoft,sproductsandprogrammertoolsarebasedonCOM,soitisobviouslysomethingthatisimportant.ButyoualsoknowthatCOMisreallyhard.YoumayhavealreadytriedtolearnCOMonce,ormay
2、beevenseveraltimes.Youmayhaveslidthroughacoupleofbooks,playedwithsomewizards,etc.Butitjustdoesn,tmakeanysense.Everythingseemsextremelycomplicatedandmuchharderthanitneedstobe.There,salsothevocabulary:marshalling,apartmentthreads”,z,singletonobjects“andsoon.Whatisthis?Thepurposeofthissetoftutorialsist
3、ohelpyoutoquicklyunderstandwhatisgoingonintheworldofDCOMsothatyoucancreateCOMclientsandserverseasily.Wedothatstartingatthebeginningandlayingthingsoutforyousimplyandintherightorder.BythetimeyoufinishthesetutorialsyouwillunderstandallofthebasicconceptsdrivingDCOMandyouwillbeabletoproceedquicklytolearn
4、therest.YouwillbeamazedathoweasyDCOMcanbeonceyougetagoodstart. TheBaSiCSOfCOM-thebestplacetostartisatthebeginning. SimDIeCoMCIientS-COMclientsareeasy SimPIeCOMSerVerS-usingtheAT1.wizardtobuildaserverTheBasicsofCOMUnderstandinghowCOMworkscanbeintimidatingatfirst.Onereasonforthisintimidationisthefactt
5、hatCOMusesitsownvocabulary.AsecondreasonisthatCOMcontainsanumberofnewconcepts.OneoftheeasiestwaystomasterthevocabularyandconceptsistocompareCOMobjectstonormalC+objectstoidentifythesimilaritiesanddifferences.YoucanalsomapunfamiliarconceptsfromCOMintothestandardC+modelthatyoualreadyunderstand.Thiswill
6、giveyouacomfortablestartingpoint,fromwhichwelllookatCOMsfundamentalconcepts.Oncewehavedonethis,theexamplepresentedinthefollowingsectionswillbeextremelyeasytounderstand.ClassesandObjectsImaginethatyouhavecreatedasimpleclassinC+calledxxx.Ithasseveralmemberfunctions,namedMethodA,MethodB,andMethodC.Each
7、memberfunctionacceptsparametersandreturnsaresult.Theclassdeclarationisshownhere:classxxxpublic:intMethodA(inta);intMethodB(floatb);floatMethodC(floatc);;Theclassdeclarationitselfdescribestheclass.Whenyouneedtousetheclass,youmustcreateaninstanceoftheobject.Instantiationsaretheactualobjects;classesare
8、justthedefinitions.Eachobjectiscreatedeitherasavariable(localorglobal)oritiscreateddynamicallyusingthenewstatement.Thenewstatementdynamicallycreatesthevariableontheheapandreturnsapointertoit.Whenyoucallmemberfunctions,youdosobydereferencingthepointer.Forexample:xxx*px;/px=newxxx;/p-MethodA(l);/delet
9、epx;/pointertoxxxclasscreateobjectonheapcallmethodfreeobjectItisimportantforyoutounderstandandrecognizethatCOMfollowsthissameobjectedorientedmodel.COMhasclasses,memberfunctionsandinstantiationsjustlikeC+objectsdo.AlthoughyounevercallnewonaCOMobject,youmuststillcreateitinmemory.YouaccessCOMobjectswit
10、hpointers,andyoumustde-allocatethemwhenyouarefinished.WhenwewriteCOMcode,wewontbeusingnewanddelete.Althoughwe,regoingtouseC+asourlanguage,wellhaveawholenewsyntax.COMisimplementedbycallstotheCOMAPI,whichprovidesfunctionsthatcreateanddestroyCOMobjects.HeresanexampleCOMprogramwritteninpsedo-COMcode.ixx
11、*pi/CoCreateInstance(,&pi)/pi-MethodA();/pi-Release();/pointertoxxxCOMinterfacecreateinterfacecallmethodfreeinterfaceInthisexample,wellcallclassixxaninterface.Thevariablepiisapointertotheinterface.ThemethodCoCreateInstancecreatesaninstanceoftypeixx.Thisinterfacepointerisusedtomakemethodcalls.Release
12、deletestheinterface.vepurposelyomittedtheparameterstoCoCreateInstance.Ididthissoasnottoobscurethebasicsimplicityoftheprogram.CoCreateInstancetakesanumberofarguments,allofwhichneedsomemoredetailedcoverage.Fornow,letstakeastepbackandlookatthebiggerissueswithCOM.HowCOMIsDifferentCOMisnotC+,andforgoodre
13、ason.COMobjectsaresomewhatmorecomplicatedthentheirC+brethren.Mostofthiscomplicationisnecessarybecauseofnetworkconsiderations.TherearefourbasicfactorsdictatingthedesignofCOM:C+objectsalwaysruninthesameprocessspace.COMobjectscanrunacrossprocessesoracrosscomputers. COMmethodscanbecalledacrossanetwork.C
14、+methodnamesmustbeuniqueinagivenprocessspace.COMobjectnamesmustbeuniquethroughouttheworld. COMserversmaybewritteninavarietyofdifferentlanguagesandonentirelydifferentoperatingsystems,whileC+objectsarealwayswritteninC+.1.etslookatwhatthesedifferencesbetweenCOMandC+meantoyouasaprogrammer.COMcanrunacros
15、sprocessesInCOM,youastheprogrammerareallowedtocreateobjectsinotherprocesses,andonanymachineonthenetwork.ThatdoesnotmeanthatyouW川alwaysdoit(inmanycasesyouwont).However,thepossibilitymeansthatyoucantcreateaCOMobjectusingthenormalC+newstatement,andcallingitsmethodswithlocalprocedurecallswontsuffice.Toc
16、reateaCOMobject,someexecutingentity(anEXEoraService)willhavetoperformremotememoryallocationandobjectcreation.Thisisaverycomplextask.Byremote,wemeaninanotherprocessoronanotherprocess.ThisproblemissolvedbycreatingaconceptcalledaCOMserver.ThisotherentityW川havetomaintaintightcommunicationwiththeclient.COMmethodscanbecalledacrossanetworkIfyouhaveaccesstoamachineo