Tuesday, October 18, 2005

DLL Fundas

Excerpts from various articles related to DLLs and Registry.

DLLs Vs EXEs

Even though DLLs and applications are both executable program modules, they differ in several ways. To the end-user, the most obvious difference is that DLLs are not programs that can be directly executed. From the system's point of view, there are two fundamental differences between applications and DLLs:

• An application can have multiple instances of itself running in the system simultaneously, whereas a DLL can have only one instance.

• An application can own things such as a stack, global memory, file handles, and a message queue, but a DLL cannot.

DLL BASICS

• Each DLL has a set of classes.

• Each class has its own set-up functions and sub-routines.

• Public functions can be accessed outside of the DLL.

• Private function cannot be accessed outside of the DLL.

Use the following command to register a DLL

regsvr32 path\DLLName.dll

A message will tell you if it was successfully registered. If the registration did not work, make certain that you typed in the path and file name correctly.

To unregister

regsvr32 /u path\DLLName.dll

When a program installs a library (DLL or OCX) file the program's install routine will typically "register" the file with the system. This process tells the system the libraries in the file are available for more than one program to use. (Some DLL or OCX files are self-registering.)

COM class or Component Object Class (coclass)

(from the Platform SDK: COM / COM Class Objects and CLSIDs)

"A COM server is implemented as a COM class. A COM class is an implementation of a group of interfaces in code executed whenever you interact with a given object.

There is an important distinction between a C++ class and a COM class. In C++, a class is a type. A COM class is simply a definition of the object, and carries no type, although a C++ programmer might implement it using a C++ class. COM is designed to allow a class to be used by different applications, including applications written without knowledge of that particular class's existence. Therefore, class code for a given type of object exists either in a dynamic linked library (DLL) or in another application (EXE)."

COM object

A COM object is an instance of a COM Class at the runtime.

COM class object or Class Factory

(from the Platform SDK: COM / COM Class Objects and CLSIDs)

"The basic way to create an instance of a class (COM Class) is through a COM class object. This is simply an intermediate object that supports functions common to creating new instances (COM Object) of a given class (COM Class)."

COM component

COM component refers to a binary module, such as a DLL or an Executable. After registering, a component will expose one or more COM Class Object (or Object Factory).

Type Library

Type libraries contain the specification (metadata) for one or more COM elements, including classes, interfaces, enumerations, and more. These files are stored in a standard binary format. A type library can be a stand-alone file with the .tlb filename extension, or it can be stored as a resource in an executable file, which can have a .ocx, .dll, or .exe file name extension.

COM IDs

In COM world, you have to identify different pieces such as coclasses, interfaces, type libraries, applications, etc. These pieces must be unique in the world.

COM uses the Globally Unique IDentifier (GUID) to define these different IDs.

In an Interface Definition Language (IDL) file, you have to use the attribute uuid that stands for Universally Unique IDentifier. A UUID and a GUID are equivalent.

So, what is a GUID? A GUID is a 128-bit number, usually represented in hexadecimal, which is guaranteed "to be unique across space and time". For example, the following number is a GUID:

{60B4140E-B0A7-4540-B744-7E1A944E8C78}

COM borrows this identify system to the Distributed Computing Environment (DCE) naming scheme. The DCE RPC system uses UUIDs.

The main COM IDs are:

* LIBID: the Type Library ID, based on GUID.

* APPID: the Application ID, based on GUID.

* CLSID: the COM Class ID, based on GUID.

* IID: the Interface ID, based on GUID.

* PROGID: the Program ID, based on a text string.

Class registration and file extension informations are stored under the HKEY_LOCAL_MACHINE\Software\Classes key.

Hope this information helps you stay away from DLL Hell!

Some Resources

COM Objects and Registry in a NutShell

1 comment:

Jupiter said...

these fundas refreshed things learnt during college days.

good to read these again!