From:http://www.codeproject.com/Tips/441838/Designing-the-Interface-of-DLLs-in-C-Cplusplus-P
IntroductionThistipassumesthatyouarefamiliarwithDLLusageandrelatedtermslikestatic/dynamiclinkinIfyouhavenoideahowtouseDLLs,thenreadthistipafterlearningtheDLLrelatedbasicshere(cross-platformwikipediapag,here(windows)orhere(通达信股票 数据接口,unix:linux,bsd,osx,et).
MyadviceistoneveruseDLLsinyourprojectifyoudon’thavetobecausetheyaretroublIfyoustillhavetofallbackonputtingsomeC/C++codetoDLLs(oryouhavetocreateaplug-ininterfacandit’suptoyoutodesigntheDLLinterface(thatisusedonlybyyourC/C++cod,thenreadalonThereisaminimalisticzippedprojectfileattachedwithaVisualC++2010solutionanda通达信股票 数据接口,unixbuildscripttodemonstrateplugin(DLL/sharedlibrary)loadingonWindowsand通达信股票 数据接口,unix(linux/bsd/osx)platforms.
HowShouldaDLLInterfaceLookLikeInMyOpinionFirst,declareaninterface(aclassthathasonlypurevirtualmethods)inthepublicheaderfileofyourDLIt’squiteimportanttouseonlypurevirtualmethods!Afterthis,puttheonlyexportedfunctiontoyourDLLthatreturnsapointertothisinterfacAfterthis,intheprivatesourcefilesofyourDLL,youcanderiveaclassfromthisinterfaceandimplementitsmethods.NowtheusersoftheDLLjustcalltheinterfacegettermethodoftheDLLandthencontinueusingitinaniceC++ishwaybycallingthemethodsoftheretrievedinterfacepointer.
WhyIsThisGood?Itsimplylooksnicer.AsaresultofthethinlinkinginterfacebetweenyourDLLandthemodulethatusesit-justoneexportedDLLfunction-it’sveryeasytomakethiscross-platformbecausetheuglyifdefsandplatformdependentcodeisminimaThethininterfacemakesiteasytoimplementplatformindependentdelayloadinIt’sveryeasytomergethisDLLcodeintoyourapplicationlateraspartofarefactorizationifyoudecidetodoso-it’sabreezLet’ssayyouarewritingaplug-ininterfaceforyourprogram-it’sveryeasytoputinternalplug-instoyourexecutablebyimplementingtheDLLinterfaceinsideyourprograItbecomesveryeasytoswitchbetweenanbuiltinfunctionalityandonethatisimplementedinaDLL,foryourexecutablebothkindsarevisiblejustasasimpleinterfacepointer.
VersioningPlug-inDLLs(MyStylTherearealotofwaystodetecttheversionofplug-ins.Forexample,bycodingtheversionintotheversioninforesourceoftheDLL,codingtheversiontothefilename,gettingtheversionbycallingaversiongetterexportedfunctionoftheDLL…Iuseatotallydifferentapproachthathasseveraladvantagesoverthepreviouslylistedmethods.
Let’ssayIhaveamediaplayerprogramandacodecplug-inforitwithaspecificinterfacAftersometime,IreleaseanewversionofthemediaplayerthathasamodifiedinterfacebutIdecidetoreleasetheplug-inforolderversionsofthemediaplayeraswelIdothisbywritingasingleDLLthatsupportsboththeoldandthenewversionoftheplugininterfacThepublicinterfaceheaderofmyplug-inDLLwillcontainthedeclarationofboththeoldandthenewversionoftheinterfaceanditcontainstwoexportedfunctions:oneofthemreturnstheold,andtheotherreturnsthenewinterfacepointer.Theexportedmethodshavedifferentnamesofcourse:GetMediaPlayerInterface()andGetMediaPlayerInterface2().TheDLLimplementsbothinterfacesandinsideitcanusealotofsharedcodebetweenthetwoimplementationsandIhavetomaintainonlyoneprojectforthat.
IhaveapieceofcodethatcandetectifaDLLexportssomefunctionswithoutactuallyloadingtheDLThispieceofcodeisusefulformanyreasons:IfyouhavetoloadaDLLtofindoutwhetheritisyourpluginornot,thenduringyourLoadLibrary()calltheDllMain()ofthelibrarymaybeexecutedinsideyourprocessevenifitisnotyourplugiAnotheradvantageisfasterexecutionthatmaycountalotifyourapphaslotsofplugins.HereisanothertipthatcontainsthecodethatdetectswhetheraDLLexportsyourdesiredfunction(s)withoutloadingitwithLoadLibrary():Checkingforexportedsymbols/functionsinaDLLwithoutloadingit.
AdvicestoDesigntheInterfaceofDLLsWritteninCImyselfneveruseCinuserapplicationsandI’mgenerallyagainsttheuseofCwhendevelopmentisn’tverylow/system-levelandareasonablygoodC++compilerisavailableonthespecifiedplatforIfyouhavethesourcesofaDLLthatiswritteninC,thenyoucandothefollowing:
IfyouwanttokeeptheCinterface,thendothefollowing:DeclareastructinthepublicheaderofyourDLThemembersofthisstructwillbefunctionpointers.ThisstructwillbetheequivalentoftheC++interfaceclass.SomewhereinyourDLL,definea(statiinstanceofthisstructandfillitwithpointerstoyourfunctionimplementations.ExportasinglefunctionfromtheDLLthatreturnsaconstpointertothepreviouslydefined(statistructinstancAlternativelyyoucancreateaC++“wrapper”interfacefromwhichyoucalltheoriginalCfunctions.
=========================================================我的建议:1除法不得不,否则尽量不要使用DLL
DLL中类的成员函数最好声明为purevirtualmethods
文章为作者独立观点,不代表股票交易接口观点