Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to COM by Mark Bartosik August 2001 Introduction to COM Prerequisites: –A small experience in at least one programming language, ideally C,

Similar presentations


Presentation on theme: "Introduction to COM by Mark Bartosik August 2001 Introduction to COM Prerequisites: –A small experience in at least one programming language, ideally C,"— Presentation transcript:

1 Introduction to COM by Mark Bartosik August 2001 Introduction to COM Prerequisites: –A small experience in at least one programming language, ideally C, C++ or VB. –Aim: Understand the need for COM and basically what it does. We will just be scratching the surface.

2 Introduction to COM by Mark Bartosik August 2001 A quick survey Hands up if you can program in C C++ Visual Basic C# Java Ada Assembler Java script, VB script What about threading? Who thinks they can write multithreaded code?

3 Introduction to COM by Mark Bartosik August 2001 The problem You are a project manager. You have a project that is best written as: 1/3 C++ (half of which is multi-threaded) 1/3 Visual Basic 1/3 Third party code, in unknown language How do you integrate all this?

4 Introduction to COM by Mark Bartosik August 2001 COM Component Object Model COM is the “glue” or the “protocol” to glue components and objects together to form a cohesive product.

5 Introduction to COM by Mark Bartosik August 2001 Before COM There was no standard for… –Calling functions –Accessing data –Requesting services –Packaging code –Working with threads (or not)

6 Introduction to COM by Mark Bartosik August 2001 Let’s look at how to call a function int func0(int a, int b) { return 0; } int main(int, char * []) { return func0(6, 7); } This computer does not understand this. A ‘C’ compiler does. The ‘C’ compiler translates the ‘C’ to assembler or machine code. The CPU then executes the machine code.

7 Introduction to COM by Mark Bartosik August 2001 Calling a function The caller must pass to the callee the function parameters or arguments. The callee must pass the result to the caller. e.g. int subtract(int arg1, int arg2); What does this return? subtract(7, 4);

8 Introduction to COM by Mark Bartosik August 2001 A binary contract The CPU only understands raw bytes not ‘C’ or any other language. So in practice the caller and callee must have a common agreement on what the assembler looks like. The problem comes when the caller is written by Jane in VB and the callee by Jim in C++.

9 Introduction to COM by Mark Bartosik August 2001 So what does the assembler look like? You do not need to understand the assembler - the point is there is no standard. extern “C” int func0(int, int); push7 push6 call_func0 addesp, 8

10 Introduction to COM by Mark Bartosik August 2001 So what does the assembler look like? You do not need to understand the assembler - the point is there is no standard. struct object_t { int func1(int, int); }; push7 push6 leaeax, DWORD PTR _obj$[ebp] pusheax call?func1@object_t@@QAGHHH@Z

11 Introduction to COM by Mark Bartosik August 2001 So what does the assembler look like? You do not need to understand the assembler - the point is there is no standard. struct object_t { int func1(int, int); }; push7 push6 leaeax, DWORD PTR _obj$[ebp] pusheax call?func1@object_t@@QAGHHH@Z addesp, 12

12 Introduction to COM by Mark Bartosik August 2001 So what does the assembler look like? You do not need to understand the assembler - the point is there is no standard. struct object_t { int func1(int, int); }; push7 movedx, 6 leaecx, DWORD PTR _obj$[ebp] call?func1@object_t@@QAIHHH@Z

13 Introduction to COM by Mark Bartosik August 2001 Everyone has their own standard! Each language can have its own conventions. Each vendor of a language may have a different standard. Even with one vendor and one language there can be different standards between different compiler versions. Even with the same language, same vendor, and same compiler version, and same platform (OS and CPU), there can be different contracts for different functions, e.g. __stdcall, __cdecl, __fastcall In addition there are different naming conventions: _func0, ?func1@object_t@@QAIHHH@Z

14 Introduction to COM by Mark Bartosik August 2001 Enter COM COM is language neutral COM specifies a tight binary contract

15 Introduction to COM by Mark Bartosik August 2001 Here’s a small part of the contract

16 Introduction to COM by Mark Bartosik August 2001 Here’s the assembler You do not need to understand the assembler push7 push6 moveax, DWORD PTR _COMobj$[ebp] movedx, DWORD PTR [eax] movecx, DWORD PTR _COMobj$[ebp] callDWORD PTR [edx+12] If you know assembler, you will see that this is not efficient. But it is well defined.

17 Introduction to COM by Mark Bartosik August 2001 Before COM There was no standard for… –Calling functions  –Accessing data –Requesting services –Version control –Packaging code –Working with threads (or not)

18 Introduction to COM by Mark Bartosik August 2001 Accessing data You thought that calling functions was problematic! Accessing data is far worse! There are many more ways to represent data than there are ways to call functions. Jim must not be allowed to access Jane’s data. Jane’s data should be private to Jane.

19 Introduction to COM by Mark Bartosik August 2001 Accessing data Global variables are bad. What is the first rule of object orientation? –Encapsulate

20 Introduction to COM by Mark Bartosik August 2001 Encapsulation struct employee_t { std::string name; unsigned number; };

21 Introduction to COM by Mark Bartosik August 2001 Encapsulation function call

22 Introduction to COM by Mark Bartosik August 2001 Encapsulation There is no accessible data There are only functions

23 Introduction to COM by Mark Bartosik August 2001 Before COM There was no standard for… –Calling functions  –Accessing data  –Requesting services –Version control –Packaging code –Working with threads (or not)

24 Introduction to COM by Mark Bartosik August 2001 Creating an instance of a class (without COM) VB: dim bartosik as CEmployee dim bartolotta as CEmployee C++ (without COM) CEmployee bartosik;

25 Introduction to COM by Mark Bartosik August 2001 How can VB create a C++ object? What is a CEmployee is VB may not be the same as a CEmployee in C++. What is needed is a language neutral means to say a “new instance of CEmployee”. Under the covers (sometimes) CoCreateInstance

26 Introduction to COM by Mark Bartosik August 2001 Language bindings VB

27 Introduction to COM by Mark Bartosik August 2001 Language bindings C++ (native)

28 Introduction to COM by Mark Bartosik August 2001 Language bindings C++ (via #import)

29 Introduction to COM by Mark Bartosik August 2001 Language bindings Java script

30 Introduction to COM by Mark Bartosik August 2001 Language bindings VB script

31 Introduction to COM by Mark Bartosik August 2001 Language bindings C Forget it. It’s too complicated, and impractical!

32 Introduction to COM by Mark Bartosik August 2001 Everything is an object!

33 Introduction to COM by Mark Bartosik August 2001 How to create an object VB

34 Introduction to COM by Mark Bartosik August 2001 How to create an object C++

35 Introduction to COM by Mark Bartosik August 2001 How to create an object Java script

36 Introduction to COM by Mark Bartosik August 2001 How to create an object VB script

37 Introduction to COM by Mark Bartosik August 2001 What are these long numbers ? The long numbers are called GUIDs. Globally Unique Identifiers, also called UUIDs (universally unique identifiers). They are pseudo random 128 bit numbers. 2 128 this is about 3x10 38 or 300,000,000,000,000,000,000,000,000,000,000,000,000 That’s more than there are particles on this planet, so they can be considered totally unique.

38 Introduction to COM by Mark Bartosik August 2001 What’s a class If we want to model a company we might have a class “CEmployee”, from which we create multiple instances of employees. The class models the concept of an employee and the objects of that class represent individual employees.

39 Introduction to COM by Mark Bartosik August 2001 Creating a COM object VB: dim bartosik as CEmployee dim bartolotta as CEmployee C++

40 Introduction to COM by Mark Bartosik August 2001 Before COM There was no standard for… –Calling functions  –Accessing data  –Requesting services  - this is really creating objects –Version control –Packaging code –Working with threads (or not)

41 Introduction to COM by Mark Bartosik August 2001 Version control Now our company evolves and we want to change how an instance of “CEmployee” behaves. What do we do: A) Change every component in the system that uses a CEmployee? B) Restrict the changes to only compatible changes? C) Create a CEmployee2 class? D) Extend CEmployee to support the old and new interfaces.

42 Introduction to COM by Mark Bartosik August 2001 Answer With COM you can do any of the above, sort of. VB limits us to options A, B, & C. C++ an do any of the above. When a client creates an instance of a class using CoCreateInstance it uses a GUID to specify to Windows that it means the CEmployee class. Once it has a CEmployee class it asks the object for a specific interface (old or new), it uses a GUID to specify which interface (the old GUID or the new GUID). QueryInterface (if you know C++ think of dynamic_cast)

43 Introduction to COM by Mark Bartosik August 2001 Before COM There was no standard for… –Calling functions  –Accessing data  –Requesting services  - this is really creating objects –Version control  –Packaging code –Working with threads (or not)

44 Introduction to COM by Mark Bartosik August 2001 Sharing objects You’ve got two pieces of code (maybe in different languages) that both want to access the employee object. How do they ensure that they don’t leak employee objects. AddRef Release

45 Introduction to COM by Mark Bartosik August 2001 Native support Visual Basic, C#, integrate seamlessly with COM, it is hard to tell the difference between a COM object and a native language object. C++ can be extended through classes to integrate with COM, or it can have native compiler support. In VC5 and VC6 it is moderate compiler support, in VC7 (.NET) it has extensive compiler support. VB script and Microsoft Java script have limited support. C has no support

46 Introduction to COM by Mark Bartosik August 2001 Packaging code This is simple. Either a.dll or.exe is created. The dll must implement a fixed number of functions: DllCanUnloadNow <- beyond this talk DllGetClassObject <- how to create an object DllRegisterServer <- how to install DllUnregisterServer <- how to uninstall The.exe must call specified functions in startup to install, uninstall or expose object creation.

47 Introduction to COM by Mark Bartosik August 2001 What’s ActiveX Buzz-word ActiveX DLL (a COM.dll provides services) ActiveX server (a COM.exe provide services) ActiveX control (a.ocx same as COM.dll, but provides a GUI, like a custom push button).

48 Introduction to COM by Mark Bartosik August 2001 Installing a package Regsvr32 your.dll Regsvr32 your.ocx Regsvr32 /u your.dll your.exe -install These install data into the machine’s registry. A GUID identifying each class that the DLL implements.

49 Introduction to COM by Mark Bartosik August 2001 The chain of events To create an object: dim bartosik as CEmployee VB translates CEmployee to {0222A94E-1707-4293-975A-7A788DEE911C} VB calls CoCreateInstance using the GUID Windows looks in the registry to find which DLL implements the class represented by the GUID. Windows loads that dll (or exe) and calls a function in it to request a new instance of that class. CoCreateInstance returns a pointer to the new employee object to VB. VB allows that pointer to be accessed via the variable ( bartosik ). VB allows the functions of the employee to be called, using the COM standards.

50 Introduction to COM by Mark Bartosik August 2001 Before COM There was no standard for… –Calling functions  –Accessing data  –Requesting services  - this is really creating objects –Version control  –Packaging code  –Working with threads (or not)

51 Introduction to COM by Mark Bartosik August 2001 Threads this is really complex There are two styles of writing code: –Single threaded –Multithreaded In general single threaded code, is only happy being called by single threaded code (i.e. in a single thread). Multithreaded code can be called by either. So single threaded code needed to be protected or isolated from access by multiple threads.

52 Introduction to COM by Mark Bartosik August 2001 Apartments Apartments isolate different threading models. Apartments control how these two types of code interact. Anything else to too complex for today.

53 Introduction to COM by Mark Bartosik August 2001 Before COM There was no standard for… –Calling functions  –Accessing data  –Requesting services  - this is really creating objects –Version control  –Packaging code  –Working with threads (or not) 

54 Introduction to COM by Mark Bartosik August 2001 Summary COM allows us to Mix code from different languages, compilers, vendors, threading models, etc. Solves many version control issues. Encourages encapsulation (protects data through use of interfaces).

55 Introduction to COM by Mark Bartosik August 2001 Don’t believe the hype Not everything is an object! COM can be very inefficient (so can VB). Can be thousands of times slower than raw C++. COM solves some problems and causes other problems. There is no silver bullet - be a little skeptical. Be skeptical. There is no good do it all solution. C, C++, Java, C#, VB, Ada all have strengths and weaknesses. So does COM, but what COM does is give you more flexibility in choosing the right language for the task by providing some glue.

56 Introduction to COM by Mark Bartosik August 2001 Where next?.NET ?.NET is the successor to COM, but integrates tightly with COM. Hype? 1 year ago, the industry leaders were saying that COM+ was the way forward, now they say COM is dead long live.NET. The industry leads need to sell more courses, software, and books, and hype is a good selling technique. Books: Effective COM, Don Box -- but this is not for beginners!


Download ppt "Introduction to COM by Mark Bartosik August 2001 Introduction to COM Prerequisites: –A small experience in at least one programming language, ideally C,"

Similar presentations


Ads by Google