Monday, January 30, 2012

MongoDB in C#.net

Introduction

This article explain how to communicate with MongoDB from C#.net, providing an exmaple. I was looking for such sample example for MongoDB in C#.net, but after reasonable searching I not found some real practical tutorial showing complete example to communicate with MongoDB. Therefore I wrote this article to provide a complete sample to use MongoDB from C#.net for developers community who want to start with this new technology. So I put together that scattered information I founded on serveral sites to compile into some real practical sample application.

Background

There are some drivers available for communicating with MongoDB from C#.net, I prefer to use the official driver recommended available here. So you must have download that driver and add references for the driver's essential libraries in your
project, such as :

  1. MongoDB.Bson.dll
  2. MongoDB.Driver.dll

At the time of executing this application, make sure that the MongoDB server is running.

Example

This sample application contains one form with different buttons :
  1. Create Data, Create sample data for Departments and Employees collections (keep remember, at first time you query any particular collection, mongoDb automatically create these collections if not already created).
  2. Get Departments, this will display the sample data created for departments colleciton, in the grid placed on the form.
  3. Get Employees, this will display the sample data created for employees colleciton, in the grid placed on the form.
  4. Delete Departments, this will delete all the departments available in the departments collection.
  5. Delete Employees, this will remove all the records available in the employees collection.

Let's start with the code. First define the connection string for your mongoDB server, by default its running on 27017 port, or you have to change it accordingly if you have specified something else. For example on my PC, I have server running on localhost at port 27017.

key="connectionString" value="Server=localhost:27017"

For create sample data, we use two methods CreateDepartment() and CreateEmployee(). Let first take a look at CreateDepartment method. It takes two parameters departmentName and headOfDepartmentId, which is practically should be the real head's Id, but for sampling here just any random Id is used. The core functionality of this method is :

  1. Connect to server: MongoServer.Create(ConnectionString) accepts the connection string for your server, and returns MongoServer type object, which later use to query the desired database object.
  2. Get Database: server.GetDatabase("MyCompany") this call returns MongoDatabase type object for the database used in this example MyCompany
  3. Retrieve departments collection: myCompany.GetCollection("Departments"), this method will actually retrieve our records from departments collection. It returns MongoCollection of passed generic type, BsonDocument
    in this case. At first time, definitely there will be no any departments present in the departments collection, so this collection object is empty(has 0 records).
  4. Create new department object: BsonDocument deptartment = new BsonDocument {}, this constructor syntax creates new department with the fields as you speficed in the constructor(remember MongoDB document could have different
    fields, not necessary that all documents have same fields)
  5. Insert document in departments collection: departments.Insert(deptartment), will actually insert the document in our departments collection.
private static void CreateDepartment(string departmentName, string headOfDepartmentId)
{
MongoServer server = MongoServer.Create(ConnectionString);
MongoDatabase myCompany = server.GetDatabase("MyCompany");

MongoCollection departments = myCompany.GetCollection("Departments");
BsonDocument deptartment = new BsonDocument {
{ "DepartmentName", departmentName },
{ "HeadOfDepartmentId", headOfDepartmentId }
};

departments.Insert(deptartment);
}

Similarly CreateEmployee() method takes its parameters, connect to server, database, employee collection and insert employee in the collection. I follow the same flow to keep things clear for understanding.

private static void CreateEmployee(string firstName, string lastName,string address, string city, string departmentId)
{
MongoServer server = MongoServer.Create(ConnectionString);
MongoDatabase myCompany = server.GetDatabase("MyCompany");

MongoCollection employees = myCompany.GetCollection("Employees");
BsonDocument employee = new BsonDocument {
{ "FirstName", firstName },
{ "LastName", lastName },
{ "Address", address },
{ "City", city },
{ "DepartmentId", departmentId }
};

employees.Insert(employee);
}

Next, we want to retrieve our recrods to display in the grid. We already look at retrieve code segment, just make a little changes here. Instead of BsonDocument I use my custom Department class which loaded collection of departments with my class objects. But make sure that the fields in the department collection must be exactly mapped in your Department class. Just calling GetCollection() method not actually retrieve the records, you need to call any desired method with query selectors (not covered in this aricle), so make things clear I just use simplest method FindAll() which rerieves all records from the given collection. Loop through each item and add in our temporary list which finally binds to our grid for display purpose. And I have followed the same theme to display employee records.

public static List GetDepartments()
{
List lst = new List();

MongoServer server = MongoServer.Create(ConnectionString);
MongoDatabase myCompany = server.GetDatabase("MyCompany");

MongoCollection departments = myCompany.GetCollection("Departments");
foreach (Department department in departments.FindAll())
{
lst.Add(department);
}

return lst;
}

The last thing we see here is the deletion of records. As we all know, deletion is the simplest job in most of the cases, and same here. After getting the MongoCollection object, you have to simply call its method Drop(), and it will simply delete all the records from that collection. You can also use query selectors in different methods to remove records selectively but that will be out of the scope of this article.

public static void DeleteDepartments()
{
MongoServer server = MongoServer.Create(ConnectionString);
MongoDatabase myCompany = server.GetDatabase("MyCompany");

MongoCollection departments = myCompany.GetCollection("Departments");
departments.Drop();
}

Once you get started with MongoDB you will enjoy exploring its dimensions. I found very intesresting capabilities of MongoDB. I appreciate your feedback/comments or any improvements you want to suggest in this regard, to help in making the article much better and helpful for others.


Note: Source code for sample application could be found at my CodeProject post.

Getting started with MongoDB

Introduction

Some time ago, I heard about MongoDb and I started searching on search engines to get know its scope. I found lot of supporting material in scattered form on various websites and books. Than I thought to compile atleast some basic getting started sort of tutorial for MongoDB. So that the developers who are new to MongoDB or unfamilir with, can get to know what is it and how to initiate development with this new technology.
I must mentioned here that this article is much inspired by Karl Seguin's book on MongoDB. His blog can be found here.
This article demonstrates the introduction of MongoDB, no-sql, the document-oriented database. The developers who are unfamiliar with no-sql database, will wonder how it works. As a document-oriented database, MongoDB is a more generalized NoSQL solution. It should be viewed as an alternative to relational databases. Like relational databases, it too can benefit from being paired with some of the more specialized NoSQL solutions. Take it as simple as just any database, which stores data in some particular structure. Let's first setup your environment and enjoy the MongoDB power and start thinking to use it in your projects where you find appropriate.

Setting up the MongoDB

It's easy to set up and running with MongoDB.
  1. Go to the official download page and get the binaries of your choice.
  2. Extract the archive (any where you want) and navigate to the bin subfolder. Note that mongod is the server process and mongo is the client shell
    - these are the two executables we'll be spending most of our time with.
  3. Create a new text file in the bin subfolder named mongodb.config
  4. Add a single line to your mongodb.config: dbpath=PATH_TO_WHERE_YOU_WANT_TO_STORE_YOUR_DATABASE_FILES. For example, on Windows you might do dbpath=c:\mongodb\data
  5. Make sure the dbpath you specified exists
  6. Launch mongod with the --config /path/to/your/mongodb.config parameter. As an example, if you extracted the downloaded file to c:\mongodb\ and you created c:\mongodb\data\ then within c:\mongodb\bin\mongodb.config you would specify dbpath=c:\mongodb\data\. You could then launch mongod from a command prompt via c:\mongodb\bin\mongod --config c:\mongodb\bin\mongodb.config.
Hopefully you now have MonogDB up and running. If you get an error, read the output carefully - the server is quite good at explaining what happens wrong. You can now launch mongo which will connect a shell to your running server. Try entering db.version() to make sure everything's working as it should. Hopefully you'll see the version number you installed. Go ahead and enter db.help(), you'll get a list of commands that you can execute against the db object.

Some basic concepts

Let's start our journey by getting to know the basic mechanics of working with MongoDB. Obviously this is core to understanding MongoDB, but it will also help to give the idea about some higherlevel questions about where MongoDB fits.

To get started, there are six basic concepts we need to understand.
  1. MongoDB has the same concept of a `database' with which you are likely already familiar. Within a MongoDB instance you can have zero or more databases, each acting as high-level containers for everything else. You could be think of it as simple database object in Ms SQL Sergver just for understanding the idea more clear.
  2. A database can have zero or more `collections'. A collection shares the same concept as a traditional `table', that you can think of the two as the same thing.
  3. Collections are made up of zero or more `documents'. A document can be thought of as a `row'.
  4. A document is made up of one or more `fields', which you can guess, are like `columns'.
  5. `Indexes' in MongoDB function much like their RDBMS counterparts.
  6. `Cursors' are different than the other five concepts. When you ask MongoDB for data, it returns a cursor, which you can do your processing, such as counting or skipping ahead, without actually pulling down data.
In summary, MongoDB is made up of databases which contain collections. A collection is made up of documents. Each document is made up of fields. Collections can be indexed, which improves lookup and sorting performance. Finally, when we get data from MongoDB we do so through a cursor which is delayed to execute until necessary, might be called as lazy loading While these concepts are similar to their relational database counterparts, but they are not identical. The core difference comes from the fact that relational databases define columns at the table level whereas a document-oriented database defines its fields at the document level. Each document within a collection can have its own unique set of fields. As such, a collection is a container in comparison to a table, while a document has a lot more information than a row.

Let's start play with MongoDB

First we'll use the global use method to switch databases, go ahead and enter use mycompany. It doesn't matter that the database doesn't really exist yet. The first collection that we create will also create the actual mycompany database. Now that you are inside a database, you can start
issuing database commands, like db.getCollectionNames(). If you do so, you should get an empty array ([ ]). Since collections are schema-less, we don't explicitly need to create them. We can simply insert a document into a new collection. To do so, use the insert command, supplying it with the document to insert:
db.departments.insert({name: 'Human Resource', city: 'karachi', 
head: 'Muhammad Ibrahim'})
The above line is executing insert against the departments collection, passing it a single argument. Internally MongoDB uses a binary serialized JSON format. Externally, this means that we use JSON a lot, as is the case with our parameters. If we execute db.getCollectionNames() now, we'll actually see two collections: departments and system.indexes. system.indexes is created once per database and contains the information on our databases index. You can now use the find command against departments to return a list of documents:
db.departments.find()
Notice that, in addition to the data you specified, there's an _id field. Every document must have a unique _id field. You can either generate one yourself or let MongoDB generate an ObjectId for you. Most of the time you'll probably want to let MongoDB generate it for you. By default, the _id field is indexed - which explains why the system.indexes collection was created. You can look at system.indexes:
db.system.indexes.find()
What you're seeing is the name of the index, the database and collection it was created against and the fields included in the index.
Now, back to our discussion about schema-less collections. Insert a totally different document into departments, such as:
db.departments.insert({name: 'Development', country: 'Pakistan',
departmentManager: 'Saeed Anwar', annualBudget: 5000000})
And, again use find to list the documents. Hopefully now you are starting to understand why the more traditional terminology wasn't a good fit.
There's one practical aspect of MongoDB you need to have a good grasp of before moving to more advanced topics: query selectors. A MongoDB query selector is like the where clause of an SQL statement. As such, you use it when finding, counting, updating and removing documents from collections. A selector is a JSON object , the simplest of which is {} which matches all documents (null works too). If we want all departments in Karachi city, we could use {city:'Karachi'}.
Before delving too deeply into selectors, let's set up some data to play with. Let insert some data in Employees collection, remember although its not already exists but when you go to insert in that collection, MondoDB will create that collection in current database :
db.employees.insert({name: 'Amir Sohail', dob: new Date(1973,2,13,7,47), 
hobbies: ['cricket','reading'], city: 'Karachi', gender: 'm'});
db.employees.insert({name: 'Inzama-ul-Haq', dob: new Date(1977,2,13,7,47),
hobbies: ['cricket','browsing'], city: 'Lahore', gender: 'm'});
db.employees.insert({name: 'Muhammad Yousuf', dob: new Date(1978, 0, 24, 13,
0), hobbies: ['football','chatting'], city: 'Karachi', gender: 'm'});
db.employees.insert({name: 'Muhammad Younis', dob: new Date(1982, 0, 24, 13,
0), hobbies: ['watching movies'], city: 'Peshawar', gender: 'm',
department:'Human Resource'});
db.employees.insert({name: 'Shahid Afridi', dob: new Date(1983, 0, 24, 13, 0),
hobbies: ['basketball','chatting'], city: 'Karachi', gender: 'm',
department:'Development'});
db.employees.insert({name: 'Moin Khan', dob: new Date(1978, 0, 24, 13, 0),
hobbies: ['cricket','chatting', 'browsing'], city: 'Islamabad', gender: 'm'});
db.employees.insert({name: 'Afra Kareem', dob: new Date(1993, 0, 24, 13, 0),
hobbies: ['reading','browsing'], city: 'Karachi', gender: 'f',
department:'Development'});
db.employees.insert({name: 'Asma Khan', dob: new Date(1985, 0, 24, 13, 0),
hobbies: ['reading','watching movies'], city: 'Lahore', gender: 'f',
department:'Human Resource'});
db.employees.insert({name: 'Nazia Malik', dob: new Date(1984, 0, 24, 13, 0),
hobbies: ['reading'], city: 'Karachi', gender: 'f', department:'Development'});
db.employees.insert({firstName: 'Waqar', lastName: 'Younis',
dob: new Date(1978, 0, 24, 13, 0), hobbies: ['cricket','chatting',
'basketball', 'browsing'], city: 'Karachi', gender: 'm'});
db.employees.insert({name: 'Waseem Akram', dob: new Date(1975, 0, 24, 13, 0),
hobbies: ['cricket','chatting'], city: 'Rawalpindi', gender: 'm'});
db.employees.insert({name: 'Shoaib Akhtar', dob: new Date(1980, 0, 24, 13, 0),
hobbies: ['football'], city: 'Rawalpindi', gender: 'm'});
db.employees.insert({name: 'Muhammad Amir', dob: new Date(1978, 0, 24, 13, 0),
hobbies: ['bowling'], city: 'Karachi', gender: 'm'});
db.employees.insert({name: 'Saeed Ajmal', dob: new Date(1983, 0, 24, 13, 0),
hobbies: ['spin bowling'], city: 'Karachi', gender: 'm'});
db.employees.insert({name: 'Abdur Rehman', dob: new Date(1982, 0, 24, 13, 0),
hobbies: ['bowling'], city: 'Lahore', gender: 'm'});
db.employees.insert({name: 'Muhammad Mushtaq', dob: new Date(1972, 0, 24,
13, 0), hobbies: ['cricket','chatting'], city: 'Lahore', gender: 'm'});
db.employees.insert({firstName: 'Saqlain', lastName: 'Mushtaq',
dob: new Date(1978, 0, 24, 13, 0), hobbies: ['football','chatting'],
city: 'Karachi', gender: 'm', department:'Development'});
Now that we have data, we can master selectors. {field: value} is used to find any documents where field is equal to value. {field1: value1, field2: value2} is how we do an and statement. The special $lt, $lte, $gt, $gte and $ne are used for less than, less than or equal, greater than, greater than or equal and not equal operations. For example, to get all male employees that have city Karachi, we could do:
db.employees.find({gender: 'm', city: 'Karachi'})
The $exists operator is used for matching the presence or absence of a field, for example:
db.employees.find({firstName: {$exists: false}})
Should return a single document. If we want to OR rather than AND we use the $or operator and assign it to an array of values we want or'd:
db.employees.find({gender: 'f', $or: [{hobbies: 'reading'}, 
{hobbies: 'browsing'}, {city: 'Karachi'}]})
The above will return all female employees which either have hobbies reading or browsing or city is Karachi.
There's something pretty neat going on in our last example. You might have already noticed, but the loves field is an array. MongoDB supports arrays as first class objects. This is an incredibly handy feature. Once you start using it, you wonder how you ever lived without it. What's more interesting is how easy selecting based on an array value is: {hobbies: 'cricket'} will return any document where cricket is a value of hobbies. There are more available operators than what we've seen so far. The most flexible being $where which lets us supply JavaScript to execute on the server. These are all described in the Advanced Queries section of the MongoDB website. What we've covered so far though is the basics you'll need to get started. It's also what you'll end up using most of the time.
We've seen how these selectors can be used with the find command. They can also be
used with the remove command which we've briefly looked at, the count command, which we haven't looked at but you can probably figure out.
The ObjectId which MongoDB generated for our _id field can be selected like so:
db.employees.find({_id: ObjectId("TheObjectId")})
We have remove() command for deletion purpose. To delete all records simply you could call it on the required collection.
db.employees.remove()
Or alternatively you could place the desired query selectors to delete only the selective documents.

Points of Interest

We did get MongoDB up and running, looked briefly at the insert and remove commands. We also introduced find and saw what MongoDB selectors were all about. We've had a good start and laid a solid foundation for things to come. Believe it or not, you actually know most of what there is to know about MongoDB - it really is meant to be quick to learn and easy to use. Insert different documents, possibly in new collections, and get familiar with different selectors. Use find, count and remove. After a few tries on your own, things that might have seemed awkward at first will hopefully fall into place.
Hopefully I am planning to write another article to use MongoDB with C#.Net environment. I appreciate your feedback/comments or any improvements you want to suggest in this regard, to help in making the article much better and helpful for others.

Monday, January 2, 2012

What is the difference between Class and Structure?

Following are the differences between Class and Structure :
  • Structures are value types and classes are reference types. So structures are stored on stack and classes uses heap.
  • Garbage collector terminated objects created from classes. Structures are not destroyed using Garbage collector.
  • Structures do not require constructors while classes require. Usually classes have default parameter less constructors.
  • Since structures not allow inheritance, therefore structures members cannot be declared as protected.

What are similarities between Class and structure?

 Following are the similarities between classes and structures:

  •  Classes and structures both can have methods, properties, fields, constants, enumerations.
  •  Both can have parameterless constructors and parameterized constructors.
  •  Both can contains delegates and events.
  •  Both can implement interface.

Thursday, November 3, 2011

Web Garden - ASP.Net

When an application is hosted by multiple processes on the same server it is said to be a web garden environment.All IIS Requests process by worker process. By default each and every application pool contain single worker process. The worker process is a small Win32 shell of code that hosts the common language runtime (CLR) and runs managed code. It takes care of servicing requests for ASPX, ASMX, and ASHX resources. There is generally only one instance of this process on a given machine. All currently active ASP.NET applications run inside of it, each in a separate AppDomain.

Identical copies of the process run on all of the CPUs with affinity to the process, known as Web Garden.

Think of a web garden as a web farm, but all in the context of a single machine.

What are the Limitations?
You’d stuck with an application that assumes everything lives within the same worker process.

No InProc session state:
If you’re using session state in your application, then you need to use an out-of-process session state, such as the ASP.NET State Service or sessions stored in SQL Server. InProc session management won’t work because each worker process will be maintaining its own session state. If you use an out-of-process session state, then you can be sure that all worker processes are consulting the same single resource as the place to store and retrieve session data.

No built-in caching mechanism:
If you’re using in-proc Caching, you need to remember that each individual worker process is going to maintain its own cache. This can make an operation such as clearing the cache difficult.
A better solution would be to move to an out-of-process cache like memcached. Then you only have one service that you need to clear the cache for, and that cache clearing action will be observed by all worker processes.

Advantages:
  • Robust processing of requests. When a worker process in an application pool is tied up (for example, when a script engine stops responding), other worker processes can accept and process requests for the application pool.
  • There is also a reduced contention for resources. When a Web garden reaches a steady state, each new TCP/IP connection is assigned, according to a round robin scheme, to a worker process in the Web garden. This helps smooth out workloads and reduce contention for resources that are bound to a worker process.
  • Web gardening helps enable scalability on multiprocessor computers by distributing the work to several processes. This improves performance and eliminates cross processor lock contentions.
  • Application availability, Processes in a Web garden share the requests that arrive for that particular application pool. If one worker process fails, another worker process can continue to process requests.
  • Optimum utilization of processes running on multiple processors located in a single server
  • Binding the application to processor with the help of CPU masks (Processor affinity), applications can be swapped out and restarted on the fly.
  • Could be said as a provisional solution for faulty applications.
Disadvantages :
  • Many applications assume dedicated access to some system level resource.  when this kind of application starts, it takes exclusive access to a resource; so when a second version of the application starts, it fails to get this exclusive access.
  • Prevent the use of session state in the process. So in statefull application you have another performance penalty to serialize the state to an external store.
  • Low Performance for large volumes of data.

Scenerios Where could Use :
  • Application need to share resources and hence can cause contention of resources.
  • The application is not very CPU intensive.
  • There exists multiple instances of an application, each of which can be allocated a worker process.

Saturday, October 29, 2011

Difference between a.Equals(b) and a == b?

Simply we could say,
a==b  checks equality of values
a.Equals(b))  checks the equality of references(objects)

Value Types :
When a & b are of same data type
int a = 1;
int b = 1;
Console.WriteLine("a==b > {0}", a == b);// Returns true
Console.WriteLine("a.Equals(b) > {0}", a.Equals(b));// Returns true

When a & b are of different data types
int a = 1;
long b = 1;
Console.WriteLine("a==b > {0}", a == b);// Returns true
Console.WriteLine("a.Equals(b) > {0}", a.Equals(b));// Returns false
It shows that a.Equals(b) not only checks for content, but also their data types.

Reference Types :
Compare StringBuilder objects.

StringBuilder a = new StringBuilder("testing");
StringBuilder b = new StringBuilder("testing");
Console.WriteLine("a==b > {0}", a == b);// Returns false
Console.WriteLine("a.Equals(b) > {0}", a.Equals(b));// Returns true
a==b returns false, because these are two different objects refenreces
a.Equals(b) returns true, because there content/values are equal


Lets consider a custom class Person objects.

public class Person
{
    string Name;

    public Person(string name)
    {
        Name = name;
    }
}

Person a = new Person("Person");
Person b = new Person("Person");
Console.WriteLine("a==b > {0}", a == b);// Returns false
Console.WriteLine("a.Equals(b) > {0}", a.Equals(b));// Returns false
a==b returns false, similaly like StringBuilder example, as two different objects refenreces
a.Equals(b) returns false,  Unlike StringBuilder example, because here content/values are not checking by-default, in order to check the internal contents of objects we have to put our custom logic, as in next example.


Lets add our own Equals() method override to put logic to compare internal content of objects. We are checking the Name of both objects.
So now Person class will look like :

public class Person
{
    string Name;

    public Person(string name)
    {
        Name = name;
    }

    public override bool Equals(object obj)
    {
    if (obj.GetType() != this.GetType())
    {
        return false;
    }

    Person p = obj as Person;
    return Name.Equals(p.Name);
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }
}

Person a = new Person("Person");
Person b = new Person("Person");
Console.WriteLine("a==b > {0}", a == b);// Returns false
Console.WriteLine("a.Equals(b) > {0}", a.Equals(b));// Returns true
a.Equals(b) now returns true, because now its checking Name of both objects for equality.

Moreover if you want to put cusomt checking for == operator, you even can do this by overloading ==operator.

In next example we add two overloads for ==operator and !=operator in Person class :
public static bool operator ==(Person a, Person b)
{
    // If both are null, or both are same instance, return true.
    if (System.Object.ReferenceEquals(a, b))
    {
        return true;
    }

    // If one is null, but not both, return false.
    if (((object)a == null) || ((object)b == null))
    {
          return false;
    }

    return a.Name.Equals(b.Name);
}

public static bool operator !=(Person a, Person b)
{
return !(a == b);
}

Person a = new Person("Person");
Person b = new Person("Person");
Console.WriteLine("a==b > {0}", a == b);// Returns true
Console.WriteLine("a.Equals(b) > {0}", a.Equals(b));// Returns true
now both techniques will return true, because both are checking the same data.

For further guidelines about overloading Equals () or operator overloading, please visit MSDN:
http://msdn.microsoft.com/en-us/library/ms173147%28v=vs.80%29.aspx

Sunday, October 9, 2011

Exe, Dll and Ocx files

EXE :
  • Exe has only one main entry point (it contains a startup function etc).
  • Exe is an out of process server, when the system launches new exe, a new process is created.
  • When exe is lanuched, it occupised its own memory space.
  • The exe’s entry thread is called in context of main thread of that process.
  • The exe can process requests on an independent thread of execution, notifying the client of task completion using events or asynchronous call-backs. This frees the client to respond to the user.
  • If an error occurs the client processes can continue to operate.
  • Generally slower than an Dll alternative.

DLL :
  • A Dll runs is an in process server running in the same memory space as the client process.
  • DLLs are loaded into an exe application (a Dll can't run by itlsef). If tried to run it directly , it will display an error about a missing entry point.
  • In-process component shares its client’s address space, so property and method calls don’t have to be marshaled. This results in much faster performance.
  • If an unhandled error occurs it will cause the client process to stop operating.
  • That in most cases DLLs have an export section where symbols are exported.
  • DLL can be reused and versioned. It reduces storage space as different programs/files can use the same dll.
  • DLL does not have a main entry point. Binding occurs at runtime, that’s why it is called "Dynamic Link" library.
  • The system loads a DLL into the context of an existing thread.

OCX :
  • An OCX is used where a visual interface is required for the function , and in the 3rd party controls you use.
  • Ocx's are interfaces that you can place on a form. Like a textbox or a picturebox.
  • Very often, you could think of an OCX as an extension of the VB IDE controls.
  • They need to be driven by the form's thread and can not exist without a parent form.
  • An OCX is a file that can hold one or more ActiveX controls. These files do not need to have the .ocx extension (some are .dll files) and thus should not be referred to as "OCXs".