Date post: | 02-Jan-2016 |
Category: |
Documents |
Upload: | amir-clark |
View: | 42 times |
Download: | 1 times |
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.
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?
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
func1
func4
func3
func2
v-tableinterfacepointer
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.
• 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.
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
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.
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.
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
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).
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.
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!