Tuesday, March 4, 2014

Windows Heap, Stack and Leaks - A story with pictures

Well the title was just to attract your attention, anyways this is just for me to remember certain things related to windows stack and heap corruption issues.

  1. C++ Resource Leaks
  2. C++ Stack Corruption
  3. C++ Stack Overflow
  4. C++ Heap Corruption
And in case the links are dead, I have converted them into full page images, refer them if and only if the links are dead or changed.

Enjoy !

Friday, September 20, 2013

Generate Lib From DLL

To avoid installing and fighting against MSYS and Cygwin, you can just extract exported symbols from libvlc.dll to generate a .lib (libvlc.lib) and link your program against it.

Open Visual Studio Command Prompt
It can be found within the Visual Studio Tools menu entry:
Start / Program Files / Microsoft Visual Studio / Visual Studio Tools / Visual Studio Command Prompt.

Extract Symbols
Within the command prompt type:
dumpbin /exports "C:\Program Files\VideoLAN\VLC\libvlc.dll" > "C:\Program Files\VideoLAN\VLC\libvlc.def"
Edit the libvlc.def file and modify it to get something like this:
Generate the .lib
Still within the command prompt type:
lib /def:"C:\Program Files\VideoLAN\VLC\libvlc.def" /out:"C:\Program Files\VideoLAN\VLC\libvlc.lib" /machine:x86
Of course, you'll need to adapt the path according to your configuration.

Generate Lib From DLL

Saturday, September 7, 2013

Generating DTMF tones using C++

What are DTMF tones ?

DTMF tones are the tones used in telephones for tone dialing. The DTMF tones are sums of two sine wave tones at following frequencies:
                 1209 Hz 1336 Hz 1477 Hz 1633 Hz
                          ABC     DEF
   697 Hz          1       2       3       A
                  GHI     JKL     MNO
   770 Hz          4       5       6       B
                  PRS     TUV     WXY
   852 Hz          7       8       9       C
   941 Hz          *       0       #       D

How to generate DTMF tone samples
Generating sine wave samples is easy using the following formula:
  • n is the sample number (starting from 0)
  • f is the frequency you wan to generate
  • samplerate is the rate you are playing the samples through your sound card
Generating DTMF tones using this method is quite easy by just summing two of those sine waves.For example, for calculating samples for 8 kHz sample rate at 8 bit (unsigned) data, use the following function:
sample(n) = 128 + 63*sin(n*2*pi*f1/8000) + 63*sin(n*2*pi*f2/8000)
Where f1 and f2 are the frequencies of the sine waves in DTMF tone.

C++ Code (For 8 KHz Sampling Rate)
#include <windows.h>
#include  <math.h>

#define M_PI       3.14159265358979323846

class DTMF 
 DTMF(char digit, int iMilliSeconds = 100, WORD wSampleRate = 8000) 
  m_iPacketLength = iMilliSeconds * 8000/1000;
  m_pTone = new BYTE[m_iPacketLength];
  if(m_pTone == NULL){
  int lowtone_frequency = 0;
  int hightone_frequency = 0;
   case '1': case '2': case '3': case 'A': lowtone_frequency =  697; break;
   case '4': case '5': case '6': case 'B': lowtone_frequency =  770; break;
   case '7': case '8': case '9': case 'C': lowtone_frequency =  852; break;
   case '*': case '0': case '#': case 'D': lowtone_frequency =  941; break;
   case '1': case '4': case '7': case '*': hightone_frequency =  1209; break;
   case '2': case '5': case '8': case '0': hightone_frequency =  1336; break;
   case '3': case '6': case '9': case '#': hightone_frequency =  1477; break;
   case 'A': case 'B': case 'C': case 'D': hightone_frequency =  1633; break;
  double pi_prod_1 = (2.0 * M_PI * lowtone_frequency)/wSampleRate;
  double pi_prod_2 = (2.0 * M_PI * hightone_frequency)/wSampleRate;
  for(int i=0; i<m_iPacketLength; i++)
   m_pTone[i] = 128 + BYTE(63*sin(i*pi_prod_1) + 63*sin(i*pi_prod_2));
 ~DTMF() {
  if(m_pTone != NULL){
   delete[] m_pTone;
   m_pTone = NULL;
 PBYTE GetData() const {
  return m_pTone;
 int GetLength() const {
  return m_iPacketLength;
 PBYTE m_pTone;
 int m_iPacketLength;

  1. DTMF Wikipedia 

Friday, August 9, 2013

Firefox: Get results from your favorite Google country domain

Firefox's default search bar gets its configuration options (what search engine to use, what parameters to pass, etc.) from XML files located in 'searchplugins' directory in Firefox’s default directory. If you are on a Windows system this most probably be 'C:\Program Files\Mozilla Firefox\browser\searchplugins'. There's an XML file for Google called 'google.xml'.To modify it follow the following steps (I am doing it for google.co.in, you can replace it as per your requirement): 
  1. The XML tags in this file are self-explanatory. First copy google.xml and make a new one, let's call it 'google_india.xml' (you should have admin rights on your Windows OS).
  2. Edit 'ShortName' tag content and rename it to something else, for example, Google India.
  3. Then there are two 'Url' tags. Leave the first 'Url' tag as it is get search suggestions (though never seen it work as I am behind a firewall). In the second tag change template attribute to template="https://www.google.co.in/search" (or whatever you find useful).
  4. Change https://www.google.com/ to https://www.google.co.in within '<SearchForm>' tag. This specifies the page to display if you click on the magnifying glass icon, at the left hand corner of the search bar, without any search terms.
  5. Now restart your Firefox browser and make the newly added Google search engine as default.

Wednesday, June 19, 2013

Windows API: The relationship between Process, Handle and Windows

HAVE: Process ID, NEED: Process handle

HAVE: Process handle, NEED: Process ID

HAVE: Window handle, NEED: Process ID

HAVE: Window handle, NEED: Process handle
Solution: Use 3) and then 1)

HAVE: Process ID, NEED: Window handle
SolutionEnumWindows(), then in the callback function do 3) and check if it matches your process ID.

HAVE: Process handle, NEED: Window handle
Solution: 2) and then 5)