How to Make and Use a DLL using command line and cl compiler.

Posted on Updated on

Setting up cl.exe
You wouldn’t want to everytime go inside the Visual Studio folder to use the “cl” compiler. It will be much easier if you could use “cl” command from anywhere in the command prompt. vcvars32.bat is a batch file which when executed, sets the environment variable for that command prompt session, so that you can directly use the “cl” command from anywhere. So, goto C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin> and run “vcvars32.bat”.

Why does it work ?
Before running vcvars32.bat, if you echo %INCLUDE%. You’ll get a blank. But after running it, you’ll get all sorts of path names. They basically tell the system where to look for standard include files and at the same time sets the path for cl.exe. Good to start now!

Creating DLL
To create a DLL, you’ll need two files. Copy the two files sampleDLL.cpp and sampleDLL.h from here:
Replace #include “stdafx.h” with #include.
Change the contents of HelloWorld() function to – cout<<”Hi There!”;
Run this command where your files are present:

cl.exe /LD sampleDLL.cpp

Wallah! Your DLL is created!

Create an App to use your DLL
From the same link above, copy sampleApp.cpp and replace #include “stdafx.h” with #include.
Run the command:

> cl.exe sampleApp.cpp sampleDLL.lib
> sampleApp.exe
> Hi There!

That’s it ! You have successfully created and used a DLL!

The DLL contains the executable code of the library you are trying to use. It just has to be found by the executable that’s running (it’s often in the same folder as the .exe).

The header file (.h) file contains all of the API information. The compiler uses this to make sure that you’re calling functions in the DLL appropriately. Any routine you use in a DLL needs to be (typically) declared in the .h file, so the compiler understands HOW to use the routine, and can make sure you’re calling things appropriately.

The .lib (import library) file is used by the linker to wire everything together. It provides the instructions on where, inside the DLL, things are contained.

Why Virtual destructor is necessary for an Abstract Class

Posted on

One important design paradigm of class design is that if a class has one or more virtual functions, then that class should also have a virtual destructor
If a class has a base class with a virtual destructor, its destructor (whether user- or implicitly- declared) is virtual.
It’s even more important for an interface. Any user of your class will probably hold a pointer to the interface, not a pointer to the concrete implementation. When they come to delete it, if the destructor is non-virtual, they will call the interface’s destructor (or the compiler-provided default, if you didn’t specify one), not the derived class’s destructor. Instant memory leak.

1.Is your class intended to be used as a base class?
•No: Declare public non-virtual destructor to avoid v-pointer on each object of the class.
•Yes: Read next question.

2.Is your base class abstract? (i.e. any virtual pure methods?)
•No: Try to make your base class abstract by redesigning your class hierarchy
•Yes: Read next question.

3.Do you want to allow polymorphic deletion through a base pointer?
•No: Declare protected virtual destructor to prevent the unwanted usage.
•Yes: Declare public virtual destructor (no overhead in this case).

It is important to note that there is no way in C++ to mark a class as final (i.e. non subclassable), so in the case that you decide to declare your destructor non-virtual and public, remember to explicitly warn your fellow programmers against deriving from your class

Understanding Bias and Variance

Posted on

Have a look at following site for great info on Bias and Variance, and how to use them to fix our classifiers.

How to initialize VHD image in Windows

Posted on Updated on

One of the common errors when mounting a newly created VHD in windows is :

“The disk image isn’t initialized, contains partitions that aren’t recognizable, or contains volumes that haven’t been assigned drive letters. Please use the disk management snap-in to make sure that the disk, partitions, and volumes are in a usable state”

Suppose I have a vhd named abc.vhdx. To mount it, proceed as follows:

1. Open diskmgmt.msc from Run.

2. In the graphical view option if you can see a new unknown disk, it means you have already tried once to attach/mount the vhd either by double clicking on its icon or through disk management itself. Anyways if you do not see any unknown disk, go to Action->Attach VHD. Browse for your VHD file. Then your VHD would appear as an unknown disk in the bottom.

3. Now right click on the icon for your disk and click on “Initialize Disk”.

4. Now on the right side you’ll see a shaded area saying “Unallocated”. Right Click there and create a simple volume. It is simple , just click on the default options as they appear.

Now your VHD is initialized. Double Click on your VHD file, and it will be mounted as a new drive !


Hello world!

Posted on

Welcome to! This is your very first post. Click the Edit link to modify or delete it, or start a new post. If you like, use this post to tell readers why you started this blog and what you plan to do with it.

Happy blogging!