A new experience of reviewing a book

After being in the programming domain for more than 4 years now and blogging for a similar timeframe, I got an opportunity to review a book.

This book is from Packt publishing company. Its called “C++ Application Development with Code::Blocks”

Image

Continue reading

Advertisements

Creating a DLL

To create a new dynamic link library (DLL) project
1. From the File menu, select New and then Project….
2. On the Project types pane, under Visual C++, select Win32.
3. On the Templates pane, select Win32 Console Application.
4. Choose a name for the project, such as testDll, and type it in the Name field. Choose a name for the solution, such as testDll, and type it in the Solution Name field.
5. Click OK to start the Win32 application wizard. On the Overview page of the Win32 Application Wizard dialog box, click Next.
6. On the Application Settings page of the Win32 Application Wizard, under Application type, select DLL.
7. On the Application Settings page of the Win32 Application Wizard, under Additional options, select Empty project. 8. Click Finish to create the project.

To add a function to the dynamic link library

1. To create a source, from the Project menu, select Add New Item…. The Add New Item dialog box will be displayed. On the Categories pane, under Visual C++, select Code. On the Templates pane, select C++ File (.cpp). Choose a name for the source file, such as testDll.cpp, and click Add. A blank file will be displayed.
2. Copy the code:

#include "testDll.h"
int add(int a, int b)
{ return a+b; }
int subtract(int a, int b)
{ return a-b; }
long multiply(int a, int b)
{ return a*b; }
float divide(int a, int b)
{ if(b==0)
{ printf("Error: Divide by Zero…\n");
return -1; }
else return (float)a/b;
}

3. To create a header file, right click “Header Files” in Solution Explorer, add files. On the Templates pane, select Header File (.h). Choose a name for the header file, such as testDll.h, and click Add. A blank file will be displayed.
Copy the following code:

#include <stdio.h>
__declspec(dllexport) int add(int a, int b);
__declspec(dllexport) int subtract(int a, int b);
__declspec(dllexport) long multiply(int a, int b);
__declspec(dllexport) float divide(int a, int b);

4. Note the __declspec(dllexport) modifier in the method declarations in this code. These modifiers enable the method to be exported by the DLL so that it can be used by other applications.
Another way to export a function is: To create a module-definition (.DEF) file which informs the linker to export our function. Select New from File menu and then select text File from Files tab. Give it a name example1.def.
Here is a sample of a module-definition fie (DEF file).

LIBRARY YOURDLL
DESCRIPTION ‘This is my DLL
EXPORTS
Whatever1 @2
Whatever2 @3

The LIBRARY command must match the name of your DLL. So if your DLL is named YOURDLL.DLL, the LIBRARY command is followed by YOURDLL.

The DESCRIPTION is any description you feel is appropriate for your DLL. The actual description is preceded by a single quote (‘).

The EXPORTS lists the functions that you exported, along with an ordinal number. It doesn’t matter what ordinal numbers you use, so long as you maintain different numbers for each function, and you shouldn’t change the ordinal numbers when you create an upgraded version of your DLL — persons preferring to use ordinal numbers to call your functions will get angry.

5. To build the project into a DLL, from the Project menu, select testDll Properties…. On the left pane, under Configuration Properties, select General. On the right pane, change the Configuration Type to Dynamic Library (.dll). Click OK to save the changes.

Note: If you are building a project from the command line, use the /LD compiler option to specify that the output file should be a DLL. For more information, see /MD, /MT, /LD (Use Run-Time Library).

6. Compile the dynamic link library by selecting Build Solution from the Build menu. This creates a DLL that can be used by other programs.

To create an application that references the dynamic link library
Create a normal Win32 Console application project.

To use the functionality from the class library in the console application

1. After you create a new console application, an empty program is created for you. The name for the source file is the same as the name that you chose for the project earlier. In this example, it is named testDllRef.cpp.

#include "testDll.h"
#include
#include
#pragma comment(lib, "testDll.lib")
int main()
{
printf("3+5= %d\n",add(3,5));
printf("3-5= %d\n",subtract(3,5));
printf("3*5= %ld\n",multiply(3,5));
printf("3/5= %f\n",divide(3,5));
_getch();
}

2. To reference the header files of the dynamic link library, you must modify the include directories path. To do this, on the Property Pages dialog box, expand the Configuration Properties node, expand the C/C++ node, and then select General. Next to Additional Include Directories, type the path of the location of the testDll.h header file.

3. Linking to the dll:

#pragma comment(lib, "testDll.lib")

Does the job. If you don’t want to use this statement u can manually set Project> properties > Configuration Properties > Linker> Input:

Additional Dependencies – add the name of the .lib file, “testDll.lib” here.
4. Copy the testDll.dll & testDll.lib to the folder where your source file (testDllRef.cpp) is present.
5. Build the executable by selecting Build Solution from the Build menu.
6. Run the “testDllRef.exe” from Debug> Start without Debugging. It should work!
7. If you try to run the generated “testDllRef.exe” from the “Release” folder, you will have to copy the “testDll.dll” & ”testDll.lib” to the Release folder. Then it’ll run.
To avoid this, you can copy the .dll & .lib to “Release” folder & in the #pragma comment specify:

#pragma comment(lib, "../Release/testDll.lib")

It’ll also work.

Reference: MSDN Library

Creating a setup file in VC++

Ever imagined how the setup files are made? i was curious too. Following is the procedure to create one in Visual Studio for VC++ projects.

Creating a setup file for your application using MS Visual Studio 2008

1. First build your application & generate the executable (.exe) file in “Release mode”.

2. While the solution file is still open in VS, create a deployment project (as shown in the fig 1):

File> New Project > Project types: select “Setup & Deployment”.

fig 1

3. On the Templates pane on right side, select “Setup Project”. Give a suitable name & location for the project.

4. Instead of “creating a new solution”, select the “Add to solution” option from the drop down list. The window will look like this:

fig 2

5. Add the output of the application project to the setup project. To do so, right-click on Setup1 in Solution Explorer, point to Add, and then click Project Output… fig 3.fig 3

i. In the Add Project Output Group dialog box, select Primary Output.

ii. Select Release Win32 from the Configuration drop-down box, and then click OK.

6. In the Detected Dependencies, Visual Studio adds the dependent merge module (.msm) files for the Visual C++ libraries to the setup project.

fig 4

7. IMP: if you have any other dll or lib files that your project depends on, add it to the Application folder by right clicking “Application folder” in the “File System” tab & Add > file.

fig 5 fig 6

8. Build the setup project to create the installer files (Setup1.msi and setup.exe). To do so, right click the Setup1 project node in Solution Explorer and select Build.

Visual Studio creates the installer files in the Release folder of your setup project.

fig 7

9. Run the installer on a second computer that does not have the Visual C++ libraries.

i. Copy Setup1.msi and setup.exe to the second computer.

ii. Run setup.exe on the second computer. Follow the steps that are provided by the setup wizard to complete the installation.

fig 8 fig 9

Go to the installation folder & run the .exe. It should produce the desired output.

Reference: MSDN Library

Starting with OpenAL

I wanted to capture audio through microphone in C/C++. After searching a lot i decided to go for OpenAL, an open source library for audio manipulation operations. I have just started with it. Here is the procedure i followed to get my first OpenAL code working. (I am using MS Visual Studio 2008 compiler on windows).

OpenAL
======
Official Site for OpenAL downloads: http://connect.creativelabs.com/openal/Downloads/Forms/AllItems.aspx
Download the following files:
1. oalinst
2. OpenAL11CoreSDK
3. ALUT > freealut-1.1.0-src (Windows Source)
ALUT is a starting point for learning OpenAL. Contains simple programs to get familiar with OpenAL.

and extract all.

Working with OpenAL
===================
Installation Steps:
1. Install OpenAL “oalinst.exe”. Its very small.
2. Install OpenAL “OpenAL11CoreSDK.exe”. Check if u got the folder named “C:\Program Files\OpenAL 1.1 SDK”
3. Open Visual Studio. Goto Tools> Options> Project & solutions> VC++ Directories.
–> Show directories for Library files.
Add “C:\Program Files\OpenAL 1.1 SDK\libs\Win32”.
Apply.

Building the “freealut” project
===============================
1. Go to “freealut-1.1.0-src\admin\VisualStudioDotNET” folder & open “ault.sln”. If it is in previous version of Visual Studio, it’ll ask to convert to VC9. Do it.
There will be 3 projects in the solution file:
i. alut
ii. hello_world
iii. playfile

2. For each of the projects, go to “Project > Properties” and make the following modification:
Configuration Properties> C/C++ > General > Additional Include directiories:
change “C:\Program Files\OpenAL 1.1 with EFX SDK\include” to “C:\Program Files\OpenAL 1.1 SDK\include” (the exact pathname where OpenAL SDK is installed).

Remember to do this for Debug and Release modes.

3. I prefer to have the generated output .exe files to be in a separate folder near the solution file.
So change the Output Directory in “Configuration properties > General” to “..\Debug” (for Debug mode) and “..\Release” (for Release mode).

Now if u build (F7) the solution, u’ll get the corresponding output .exe files in the “Debug” or “Release” folder in the directory where the .sln is present.
Double click “hello_world.exe” to HEAR “hello world”!
“playfile.exe” needs to be run from command prompt with a audio .wav file as argument. eg: C:..\Debug>playfile file1.wav

time() and GetTickCount() in C

In C language,
time() function returns a value of the current time in seconds from 1st January, 1970 UTC.
If you want the absolute time from 1st January, 1900 UTC in seconds (may be required in some network packet transmissions), then u can simply add the following unsigned int value to time():

const unsigned int from1900Till1970 = (unsigned int) 2208988800ul;
time_1900 = time() + from1900Till1970;

This absolute time values will continue without problem till 2036(The 32bit return value can go upto that period). After that we’ll see what happens. Mostly this concept of absolute time will go by then.

GetTickCount() function returns the time since your system booted up, in milliseconds. Hence it is not an absolute value. It returns a 16bit unsigned int value. There is a possibility of Rollback. On a Windows machine, the Rollback will occur if the system is ON continuously for approx. 49.7 days. Rare possibility but it exists.

Books for Software developers

Recently going through Intel’s website i reached here:
http://edc.intel.com/Training/Reading-List/Software.aspx

Contains the names of recommended (by Intel) books on the following topics with their amazon.com links:

    Software threading for many core architectures
    Digital home technologies
    Graphics and gaming technologies
    Software development
    High performance computing

Unix Multiprocess Programming and IPC

Good material on
1. Unix Multi-Process Programming and Inter-Process Communications (IPC):
http://users.actcom.co.il/~choo/lupg/tutorials/multi-process/multi-process.html

2. Threads Programming(IEEE POSIX 1003.1c):
http://www.llnl.gov/computing/tutorials/pthreads/