570 likes | 767 Views
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. C C++ Visual Basic C#. Java Ada Assembler Java script, VB script.
E N D
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. Introduction to COM by Mark Bartosik August 2001
C C++ Visual Basic C# Java Ada Assembler Java script, VB script A quick surveyHands up if you can program in • What about threading? • Who thinks they can write multithreaded code? 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? 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. 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) 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. 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); 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++. 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); push 7 push 6 call _func0 add esp, 8 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); }; push 7 push 6 lea eax, DWORD PTR _obj$[ebp] push eax call ?func1@object_t@@QAGHHH@Z 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); }; push 7 push 6 lea eax, DWORD PTR _obj$[ebp] push eax call ?func1@object_t@@QAGHHH@Z add esp, 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); }; push 7 mov edx, 6 lea ecx, DWORD PTR _obj$[ebp] call ?func1@object_t@@QAIHHH@Z 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 Introduction to COM by Mark Bartosik August 2001
Enter COM • COM is language neutral • COM specifies a tight binary contract Introduction to COM by Mark Bartosik August 2001
Here’s a small part of the contract Introduction to COM by Mark Bartosik August 2001
Here’s the assembler You do not need to understand the assembler push 7 push 6 mov eax, DWORD PTR _COMobj$[ebp] mov edx, DWORD PTR [eax] mov ecx, DWORD PTR _COMobj$[ebp] call DWORD PTR [edx+12] • If you know assembler, you will see that this is not efficient. But it is well defined. 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) 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. Introduction to COM by Mark Bartosik August 2001
Accessing data • Global variables are bad. • What is the first rule of object orientation? • Encapsulate Introduction to COM by Mark Bartosik August 2001
Encapsulation struct employee_t { std::string name; unsigned number; }; Introduction to COM by Mark Bartosik August 2001
Encapsulation • function call Introduction to COM by Mark Bartosik August 2001
Encapsulation • There is no accessible data • There are only functions 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) 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; 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 Introduction to COM by Mark Bartosik August 2001
Language bindings • VB Introduction to COM by Mark Bartosik August 2001
Language bindings • C++ (native) Introduction to COM by Mark Bartosik August 2001
Language bindings • C++ (via #import) Introduction to COM by Mark Bartosik August 2001
Language bindings • Java script Introduction to COM by Mark Bartosik August 2001
Language bindings • VB script Introduction to COM by Mark Bartosik August 2001
Language bindings • C Forget it. It’s too complicated, and impractical! Introduction to COM by Mark Bartosik August 2001
Everything is an object! Introduction to COM by Mark Bartosik August 2001
How to create an object • VB Introduction to COM by Mark Bartosik August 2001
How to create an object • C++ Introduction to COM by Mark Bartosik August 2001
How to create an object • Java script Introduction to COM by Mark Bartosik August 2001
How to create an object • VB script 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. • 2128this is about 3x1038 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. 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. Introduction to COM by Mark Bartosik August 2001
Creating a COM object VB: dim bartosik as CEmployee dim bartolotta as CEmployee C++ 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) 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. 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) 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) 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 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 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. 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). 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. 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. 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) Introduction to COM by Mark Bartosik August 2001