www.softbit.fi SOFTBIT Components in Windows, some history and experiences. Thomas O’Rourke Softbit Oy / Managing Director 26 October 1999 Components in Windows, some history and experiences. 1
www.softbit.fi SOFTBIT Presentation 1. Introduction to Windows components and some historical background. 2. Some real examples from Softbit, and our experiences. 26 October 1999 Components in Windows, some history and experiences. 2
www.softbit.fi SOFTBIT Softbit Oy • An Elektrobit company, currently 20 persons. • Subcontract software development for communications technology companies. • Example customers, Nokia, Nemo, etc. • Specialize in C++ development and component technologies. 26 October 1999 Components in Windows, some history and experiences. 3
www.softbit.fi SOFTBIT Thomas O’Rourke ~1978-80 Played Advent. Sold first program for $2.00 1987-88 Exchange student, Univ of Oulu 1990 B.Sc. Computer Science (Minnesota) 1990-92 Elektrobit Oy (2 years) 1992 D.I. EE, Univ of Oulu. 1992-97 Microsoft Ltd. (Seattle, USA) 1997-… Softbit Oy 26 October 1999 Components in Windows, some history and experiences. 4
www.softbit.fi SOFTBIT Definition of Components • Binary code reuse: – Changes to component do not require clients to recompile. – Upgradable at runtime. • Modern component technologies: – Polymorphism, – Location and discovery services, – Distribution, etc. 26 October 1999 Components in Windows, some history and experiences. 5
www.softbit.fi SOFTBIT Windows component history • Windows applications may use (or provide) many types of components, some big some small - I discuss these. – DLLs. – DLLs and C++. – COM objects and technologies: • COM fundamentals, • Automation, • OCX and ActiveX controls. 26 October 1999 Components in Windows, some history and experiences. 6
www.softbit.fi SOFTBIT DLLs C++ Name Mangling 26 October 1999 Components in Windows, some history and experiences. 7
www.softbit.fi SOFTBIT DLL - Dynamic Link Library • DLLs are the foundation of Windows and are still today. • Are components. • Take some time to load, but after first call they are as fast as functions. • All custom controls (buttons, trackbars) are in DLLs 26 October 1999 Components in Windows, some history and experiences. 8
www.softbit.fi SOFTBIT Principles Explicit linking: hInstance = LoadLibrary(“MyDll.dll”);// map DLL to addr pFunc = GetProcAddress(“MyDll.dll” “MyFunction”); (*pFunc)(parm1, parm2); //call func FreeLibrary(hInstance); // unmap dll ImplicitLinking MyFunction(Parm1, Parm2) // remember to include MyDll.lib to link time! // Loading and unloading is UNCONTROLLABLE! -->Good Book: Advanced Windows by Jeffery Richter. ( MsPress) 26 October 1999 Components in Windows, some history and experiences. 9
www.softbit.fi SOFTBIT C++ and DLLs • Can a C++ class “live in a DLL”, and can I use it in an application? • Yes. • How is this possible? • Name mangling. 26 October 1999 Components in Windows, some history and experiences. 10
www.softbit.fi class __declspec(dllexport) Boat This directive tells { SOFTBIT int Turn(int Speed); linker to produce int Turn(int one, int two); virtual int YouDoIt(int one) = 0; export symbols. //virtual int Okay(int two); }; (Microsoft specific) int Boat::Turn(int Speed) { Speed = Speed + 3; return Speed; } int Boat::Turn(int one, int two) { return 1; } // Taken from BOAT.LIB or BOAT.MAP // 0001:00000000 ??0Boat@@QAE@XZ 10001000 f Boat.obj //0001:00000020 ??0Boat@@QAE@ABV0@@Z 10001020 f Boat.obj //0001:00000040 ??4Boat@@QAEAAV0@ABV0@@Z 10001040 f Boat.obj //0001:00000050 ?Turn@Boat@@AAEHH@Z 10001050 f Boat.obj //0001:00000069 ?Turn@Boat@@AAEHHH@Z 10001069 f Boat.obj // ... // And if we take the comment away from the virtual we get the following: //Boat.obj : error LNK2001: unresolved external symbol // "private: virtual int __thiscall Boat::Okay(int)" (?Okay@Boat@@EAEHH@Z) 26 October 1999 Components in Windows, some history and experiences. 11
www.softbit.fi SOFTBIT Problems with DLLs • If shared by applications they must be put in common directory. • Name conflicts. • “C” style interface does not support instances, or grouping, etc. 26 October 1999 Components in Windows, some history and experiences. 12
www.softbit.fi SOFTBIT COM Automation OLE Controls (ActiveX Controls) Good Book: Understanding ActiveX and OLE by David Chappell. (MsPress) 26 October 1999 Components in Windows, some history and experiences. 13
www.softbit.fi SOFTBIT Why was COM invented? • As a mechanism for OLE2. – Put Excel charts into Word, (linking and embedding). – Document centric computing, structured storage, etc. (Excel and word data in same document). • DDE was a painful thing. • DLL’s were not enough! 26 October 1999 Components in Windows, some history and experiences. 14
www.softbit.fi SOFTBIT COM • Big argument about mechanism for OLE2. • C++ was the new cool technology… • Simple solution: Use C++ Virtual function table format as basis for COM. “Bless Vtables as a binary interoperability standard.” 26 October 1999 Components in Windows, some history and experiences. 15
www.softbit.fi C++ VTable SOFTBIT Class MyCar { virtual int AddRef () = 0; virtual int Release() = 0; virtual int QueryInterface(big_int ID, void **pInterface) = 0; virtual int TurnLeft(int Speed) = 0; virtual int TurnRight(int Speed) = 0; virtual int StartAccelerating(int Vel) = 0; virtual int StopAccelerating(int Vel) = 0; virtual int PressBrakes(int Kilograms, int Time) = 0; }; // THIS IS A REAL COM INTERFACE!! 26 October 1999 Components in Windows, some history and experiences. 16
www.softbit.fi SOFTBIT COM Objects Vtable: TurnLeft() TurnRight() Calling a COM interface ISomeInterface COM Object IAmSecondInterface UNDERSTAND: Everything happens through interfaces. 26 October 1999 Components in Windows, some history and experiences. 17
www.softbit.fi SOFTBIT Loading a COM Object COM system searches registry to determine which DLL houses your COM object. pFactory = GetClassFactory(GUID_CDMA_SYSTEM) pFactory->CreateInstance(GUID_PHONE_CONTROL, &pInterface); pInterface->MakePhoneCall(); pInterface->Release(); 26 October 1999 Components in Windows, some history and experiences. 18
www.softbit.fi SOFTBIT Problems with Vtables • Very easy to use in C++ ;) • Painful to use in C :| • Impossible to use from Visual Basic :( – They are not dynamic. – VB Couldn’t handle types… Everything could be cast to everything else. – Vtables are not dynamic (can’t change at runtime). 26 October 1999 Components in Windows, some history and experiences. 19
www.softbit.fi SOFTBIT Automation • Automation is built on top of COM. • Automation is based upon getting “Names and Ids” for functions, I.e. switch statements! • Automation defines its own object creation, own types, enumerators, even more than COM. 26 October 1999 Components in Windows, some history and experiences. 20
www.softbit.fi SOFTBIT Automation in C++ • A COM object which supports Automation has some text and beep function char sTextOutFunc[128]; char sBeepFunc[128]; sTextOutFunc = GetNameofFunction(1) sBeepFunc = GetNameofFunction(2) CallFunc(sTextOutFunc, “Hello World”); CallFunc(sBeepFunc, “World”); 26 October 1999 Components in Windows, some history and experiences. 21
www.softbit.fi Why was the OLE control SOFTBIT architecture invented? • As a COM-based replacement for VBx’s. (Visual Basic controls). • OLE Controls were to be the VB controls on steroids! • OCX’s could store data, had edit mode for design-time, broadcast capabilities, etc. 26 October 1999 Components in Windows, some history and experiences. 22
www.softbit.fi SOFTBIT Microsoft discovers internet • Needed a plug-in to browser technology, what to do? • Rename OCX to ActiveX • Drop requirements for all those “silly” interfaces - like persistence and editing, etc. 26 October 1999 Components in Windows, some history and experiences. 23
www.softbit.fi SOFTBIT Some common mistakes with COM and ActiveX Controls 26 October 1999 Components in Windows, some history and experiences. 24
www.softbit.fi SOFTBIT COM Mistakes • Theory: if you want to make real component system every component must be in a separate DLL. • Reality: Loading DLLs is not very fast. Try loading 500 DLLs. • Experience: How you package your components is important! 26 October 1999 Components in Windows, some history and experiences. 25
www.softbit.fi SOFTBIT OCX button example • Theory: Every little UI element should be an ActiveX control. • Reality: ActiveX controls were expensive to create (not so bad nowadays). 26 October 1999 Components in Windows, some history and experiences. 26
www.softbit.fi SOFTBIT Wrapping a standard button Container Application (IExplorer) reflector FIRE_BUTTON_EVENT WM_COMMAND, BTN_CLICK WM_COMMAND wnd OK Cancel 26 October 1999 Components in Windows, some history and experiences. 27
www.softbit.fi SOFTBIT Examples of Windows Components from Softbit and our Experiences. 26 October 1999 Components in Windows, some history and experiences. 28
Recommend
More recommend