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 SOFTBIT
class __declspec(dllexport) Boat { int Turn(int Speed); int Turn(int one, int two); virtual int YouDoIt(int one) = 0; //virtual int Okay(int two); }; int Boat::Turn(int Speed) { Speed = Speed + 3; return Speed; } int Boat::Turn(int one, int two) { return 1; }
This directive tells linker to produce export symbols. (Microsoft specific)
// 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 SOFTBIT
C++ VTable
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
COM Object
ISomeInterface
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 SOFTBIT
Why was the OLE control 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
26 October 1999
Cancel
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
www.softbit.fi SOFTBIT
Nemo’s TOM
Nemo Technologies 26 October 1999
Components in Windows, some history and experiences.
29
www.softbit.fi User Interface
SOFTBIT Update state (after event) Drawing graphs Indication Events
one IMobilePh
Circular Buffer / Shared memory structure
High level commands
DataBuffer/ Shared memory structure
Mobile Phone
Phone State
IAdapter
IHandler
Handler (GSM)
SendMessage GetMessage
Adapter
Handlers Serial port
26 October 1999
Components in Windows, some history and experiences.
30
www.softbit.fi SOFTBIT
Component technology: • Component technology implemented: – COM servers for “handlers”(DLL com object). – C++ classes in DLLs (using MFC).
• Components technology used: – ActiveX controls for mapping.
26 October 1999
Components in Windows, some history and experiences.
31
www.softbit.fi SOFTBIT
Handlers (Com servers) • Simple message interpreters (no UI) • Encapsulates specifics of particular phone system (moving target) • Enables multi-site development (for example in the USA).
26 October 1999
Components in Windows, some history and experiences.
32
www.softbit.fi SOFTBIT
C++ Classes • Application is made up of: – One main executable module containing main window, menu, etc. – 4 or 5 DLLs with most functionality • SerialPort • Graphing • MessageRouter •… . 26 October 1999
Components in Windows, some history and experiences.
33
www.softbit.fi SOFTBIT
ActiveX controls • Is messy to use from C++, MFC helps. • But… It gets the job done. • ActiveX controls are okay for objects that do some specific functions – displaying maps – displaying tables from databases, etc.
26 October 1999
Components in Windows, some history and experiences.
34
www.softbit.fi SOFTBIT
Experience • Architecture is successful. • Simplicity of COM servers allowed many people to work on phone specific protocol stuff. • C++ DLL classes assisted: – Reducing compile dependencies – Allowing replacement if bugs were found. 26 October 1999
Components in Windows, some history and experiences.
35
www.softbit.fi SOFTBIT
Demo: A C++ DLL component application
26 October 1999
Components in Windows, some history and experiences.
36
www.softbit.fi SOFTBIT
PC-Suite for 9110 • Framework developed by PumaTech. • Contacts plug-in developed at Softbit – No source-code was ever exchanged!
• New plug-ins are possible – Own menus, icons, own helps, etc.
• NO COM required! • Supports dynamic loading, location of services, etc. 26 October 1999
Components in Windows, some history and experiences.
37
www.softbit.fi SOFTBIT
Architecture • Uses C++ in DLLs with MFC support! – Called an “Extension DLL”
• Uses RUNTIME_TYPE information so that MFC can create instances as needed. • Restriction: only works with MFC applications, and doesn’t work distributed. 26 October 1999
Components in Windows, some history and experiences.
38
www.softbit.fi
Architecture
SOFTBIT
MFC
(3)
(3)
CView (2) Component (DLL)
Main Window
Framework (EXE) (1)
(2) Callback function (3) Windows messages (4) Communicator handling
See: “Dynamic Runtime Objects: Building Applications Your Users Can Modify at Runtime”, July 1997 Microsoft Systems Journal. 26 October 1999
Components in Windows, some history and experiences.
39
www.softbit.fi SOFTBIT
Experience • Architecture is successful. • MFC Extension classes are nice: – Messages are passes through MFC message routing mechanisms, – Program in C++, – callbacks are simple.
26 October 1999
Components in Windows, some history and experiences.
40
www.softbit.fi SOFTBIT
Experiences Conclusion
26 October 1999
Components in Windows, some history and experiences.
41
www.softbit.fi SOFTBIT
C++ or COM? • Use C++ DLLs whenever possible to provide more robust applications, that are easier to develop, debug and maintain. • Use COM when you need to develop on multisites or need to involve external persons. 26 October 1999
Components in Windows, some history and experiences.
42
www.softbit.fi SOFTBIT
MFC Extension DLL’s • MFC is good. There are a lot of code examples available on internet. • Extension DLL’s make component writing easy and connects everything up nicely.
26 October 1999
Components in Windows, some history and experiences.
43
www.softbit.fi SOFTBIT
COM Experience • COM basics are easy and work well. • Should your component work in any arbitrary container? Why? • Only use as much of COM as needed. – Don’t make yourself a giant OLE in place active document server! In place active is junk. No one can run Excel inside of Word anyway. 26 October 1999
Components in Windows, some history and experiences.
44
www.softbit.fi SOFTBIT
Design basics • Reduce dependencies – Avoid “Component soups”, unless necessary. – Consider layers, or façade patterns. – Group common functionality into packages.
• Isolate pieces – Isolate moving targets, and buggy spots in code - places where you don’t know everything. These are good candidates for components. 26 October 1999
Components in Windows, some history and experiences.
45
www.softbit.fi SOFTBIT
Design = tough questions • Consider all the alternatives. • DLLs, C++ extension DLLs, Sockets for cross process instead of DCOM, etc., Java, ...
• Make your architecture stand up to scrutiny. • Be realistic: understand your customer’s budget and time constraints - give them the components they need… (not more). 26 October 1999
Components in Windows, some history and experiences.
46
www.softbit.fi SOFTBIT
Are components always better?
26 October 1999
Components in Windows, some history and experiences.
47
www.softbit.fi SOFTBIT
Thomas O’Rourke +358 400308204 Softbit Oy www.softbit.fi
26 October 1999
Components in Windows, some history and experiences.
48
www.softbit.fi SOFTBIT
Additional Notes and Automation example
26 October 1999
Components in Windows, some history and experiences.
49
www.softbit.fi SOFTBIT
COM Books • “Effective COM”, by Don Box, is my favorite. – An objective view of COM – I totally agree with his statements • Dual interfaces are JUNK • Connection points are STUPID. • etc.
26 October 1999
Components in Windows, some history and experiences.
50
www.softbit.fi SOFTBIT
Nemo’s SAM
26 October 1999
Components in Windows, some history and experiences.
51
www.softbit.fi SOFTBIT
Automation-enable your application
• User’s can control your application. • They can use it to do things you have not thought of. • MFC provides good support for doing this. • User can use it from Visual Basic, etc.
26 October 1999
Components in Windows, some history and experiences.
52
www.softbit.fi SOFTBIT
Quality Power level Number of dropped calls Average quality per dropped call
Commands Visual Basic program
Formatted data
Sam.exe (interprets measurement data. Does conversions, indexing, highly efficient)
Scenario: User wants to make a custom report in excel.
26 October 1999
Components in Windows, some history and experiences.
Measurement data
53
www.softbit.fi SOFTBIT
Exporting all Power Level values from SAM.exe into excel (Excel VB Code)
Dim SAM As AutoApplication Dim ret As Boolean Set SAM = New AutoApplication Set filecollection = SAM.Workspace.Item("files") Set datasetcollection = SAM.Workspace.Item("datasets") Set file = filecollection.Add ret = file.Load(“Filename and path”) Set dataset = datasetcollection.Add ret = dataset.SetFile(file.Id) dataset.Filter(“$EventType = MSP”) For counter = 1 To dataset.Count ActiveSheet.Cells(counter, 1).Value = dataset.GetEventParameters(counter) Next counter dataset.Close file.Close
26 October 1999
Components in Windows, some history and experiences.
54