Introduction to COM
-
Upload
amir-clark -
Category
Documents
-
view
42 -
download
1
description
Transcript of Introduction to COM
![Page 1: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/1.jpg)
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.
![Page 2: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/2.jpg)
Introduction to COM by Mark Bartosik August 2001
A quick surveyHands 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?
![Page 3: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/3.jpg)
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?
![Page 4: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/4.jpg)
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.
![Page 5: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/5.jpg)
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)
![Page 6: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/6.jpg)
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.
![Page 7: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/7.jpg)
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);
![Page 8: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/8.jpg)
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++.
![Page 9: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/9.jpg)
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
![Page 10: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/10.jpg)
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
![Page 11: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/11.jpg)
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
![Page 12: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/12.jpg)
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
![Page 13: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/13.jpg)
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
![Page 14: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/14.jpg)
Introduction to COM by Mark Bartosik August 2001
Enter COM
• COM is language neutral
• COM specifies a tight binary contract
![Page 15: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/15.jpg)
Introduction to COM by Mark Bartosik August 2001
Here’s a small part of the contract
func1
func4
func3
func2
v-tableinterfacepointer
![Page 16: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/16.jpg)
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.
![Page 17: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/17.jpg)
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)
![Page 18: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/18.jpg)
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.
![Page 19: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/19.jpg)
Introduction to COM by Mark Bartosik August 2001
Accessing data
• Global variables are bad.
• What is the first rule of object orientation?– Encapsulate
![Page 20: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/20.jpg)
Introduction to COM by Mark Bartosik August 2001
Encapsulation
struct employee_t
{
std::string name;
unsigned number;
};
![Page 21: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/21.jpg)
Introduction to COM by Mark Bartosik August 2001
Encapsulation
• function call
![Page 22: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/22.jpg)
Introduction to COM by Mark Bartosik August 2001
Encapsulation
• There is no accessible data
• There are only functions
![Page 23: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/23.jpg)
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)
![Page 24: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/24.jpg)
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;
![Page 25: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/25.jpg)
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
![Page 26: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/26.jpg)
Introduction to COM by Mark Bartosik August 2001
Language bindings
• VB
![Page 27: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/27.jpg)
Introduction to COM by Mark Bartosik August 2001
Language bindings
• C++ (native)
![Page 28: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/28.jpg)
Introduction to COM by Mark Bartosik August 2001
Language bindings
• C++ (via #import)
![Page 29: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/29.jpg)
Introduction to COM by Mark Bartosik August 2001
Language bindings
• Java script
![Page 30: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/30.jpg)
Introduction to COM by Mark Bartosik August 2001
Language bindings
• VB script
![Page 31: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/31.jpg)
Introduction to COM by Mark Bartosik August 2001
Language bindings
• C
Forget it. It’s too complicated, and impractical!
![Page 32: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/32.jpg)
Introduction to COM by Mark Bartosik August 2001
Everything is an object!
![Page 33: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/33.jpg)
Introduction to COM by Mark Bartosik August 2001
How to create an object
• VB
![Page 34: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/34.jpg)
Introduction to COM by Mark Bartosik August 2001
How to create an object
• C++
![Page 35: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/35.jpg)
Introduction to COM by Mark Bartosik August 2001
How to create an object
• Java script
![Page 36: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/36.jpg)
Introduction to COM by Mark Bartosik August 2001
How to create an object
• VB script
![Page 37: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/37.jpg)
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.
• 2128 this 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.
![Page 38: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/38.jpg)
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.
![Page 39: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/39.jpg)
Introduction to COM by Mark Bartosik August 2001
Creating a COM object
VB:
dim bartosik as CEmployee
dim bartolotta as CEmployee
C++
![Page 40: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/40.jpg)
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)
![Page 41: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/41.jpg)
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.
![Page 42: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/42.jpg)
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)
![Page 43: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/43.jpg)
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)
![Page 44: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/44.jpg)
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
![Page 45: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/45.jpg)
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
![Page 46: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/46.jpg)
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.
![Page 47: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/47.jpg)
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).
![Page 48: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/48.jpg)
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.
![Page 49: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/49.jpg)
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.
![Page 50: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/50.jpg)
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)
![Page 51: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/51.jpg)
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.
![Page 52: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/52.jpg)
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.
![Page 53: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/53.jpg)
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)
![Page 54: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/54.jpg)
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).
![Page 55: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/55.jpg)
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.
![Page 56: Introduction to COM](https://reader036.fdocuments.in/reader036/viewer/2022081506/568133f7550346895d9aeacf/html5/thumbnails/56.jpg)
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!