Archive for September, 2008


Do you know what is meant by HASH?

A fixed-size result obtained by applying a mathematical function (the hashing algorithm) to an arbitrary amount of data. (from MSDN)

We can create hashes by using Microsoft Crypto APIs.

I’m now explaining the procedures for making hash of a data using MS Crypto APIs.

  • Acquire access to key container

Use the API CryptAcquireContext()

  • Create an empty hash object

Use the API CryptCreateHash()

  • Hold the Data to in a byte array
  • Add the Data buffer to the Hash

Use the API CryptHashData()

  • Read the length of the Hashed Data

Use the API CryptGetHashParam()

  • For example

      DWORD dwCount = sizeof(DWORD);
      DWORD dwHashLen;
      if(!CryptGetHashParam(m_hHash, HP_HASHSIZE, (BYTE *)&dwHashLen, &dwCount, 0))
      {
            TRACE(_T(“Error during CryptGetHashParam.\n”));
            return false;
      }

Now the dwHashLen variable holding the Length

  • Get the Hashed data

BYTE *pbHash = new BYTE[dwHashLen];
if(!CryptGetHashParam(m_hHash, HP_HASHVAL, pbHash, &dwHashLen, 0))
{
            TRACE(_T(“Error during CryptGetHashParam.\n”));
            return false;
}

           After that , pbHash hold the hashed data. 🙂

Now this seems so simple right…?

Start programming using MS Crypto APIs.. 🙂

Advertisements

Use memset() sagely !!

How can we initialize data members to some default values??

So many options are available now. right?

ZeroMemory(), memset() etc are there..

Using ZeroMemory() memory management function , we can fills a block of memory with zeros. 

For example 

char szSendURL[256];

ZeroMemory(szSendURL,sizeof(szSendURL));

🙂

 

Then we move to the next one. memset()..

Using memset() we can Sets buffers to a specified character.

For example 

 

char buffer[4];

memset( buffer, ‘*’, 4 );

Similarly, you can use memset() to initialize class objects also. 😀

But what happens if the class contains some virtual functions? *-)

Let’s take another example

#include <string.h>
#include <memory.h>
#include <iostream.h>
class Bird
{
protected:
char m_Name[25];
public:
virtual void SetName() {}
char* ShowName() { return m_Name;}
};

class Flying_Bird: public Bird
{
void SetName() { strcpy(m_Name,”Dove”); }
};

void main()
{
Bird *obj = new Flying_Bird(); //Create an object
memset((void *)obj,NULL,sizeof(Flying_Bird)); //memset to 0
obj->SetName(); //Crashes here
cout<<obj->ShowName();
}

 

🙂

This results in a program crash. Because one virtual function is there. Also that contains one pointer to the virtual function table (VTABLE) also. This pointer is used to resolve virtual function calls, at run time and for dynamic type casting. The pointer is hidden, and is not accessible to programmers by normal means. When we do a memset(), the value of this pointer also gets overwritten, which, in turn, results in a crash, if a virtual function is called.

To avoid such mysterious crashes, memset() should not be used on the objects of a class with virtual functions. Instead use the default constructor or an init routine to initialize member variables.

🙂

“Life never leaves u empty, it always replace everything u lost”

for https://vctipsplusplus.wordpress.com/

BijU

Changing the color of Windows controls gives them a fancy look and feel.The magazine CDs etc give some  free wares like mp3 players,video cutters etc to us.
hope most of them noted their amazing colors.. like magenta,violet etc.. 🙂
how can we change the usual colors and shape of dialog?

🙂

Create one Dialog based application.

This can be done in MFC by using simple codes.

hope this code will definitely help you.

// CTestColorDlg dialog

class CTestColorDlg : public CDialog
{
// Construction
public:
CTestColorDlg(CWnd* pParent = NULL);    // standard constructor

// Dialog Data
//{{AFX_DATA(CTestColorDlg)
enum { IDD = IDD_TESTCOLOR_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTestColorDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
HICON m_hIcon;
CBrush m_brush;

// Generated message map functions
//{{AFX_MSG(CTestColorDlg)
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

Then, add this line in the OnInitDialog function
/////////////////////////////////////////////////////////////////////////////
// CTestColorDlg message handlers

BOOL CTestColorDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Set the icon for this dialog.  The framework does this automatically
//  when the application’s main window is not a dialog
SetIcon(m_hIcon, TRUE);            // Set big icon
SetIcon(m_hIcon, FALSE);        // Set small icon

// TODO: Add extra initialization here
m_brush.CreateSolidBrush(RGB(255, 0, 255));
CRect rect;
GetWindowRect(&rect);
int w = rect.Width();
int h = rect.Height();

CRgn rgn1;

rgn1.CreateEllipticRgn(1, 1, w, h/2 + 30);
SetWindowRgn(static_cast<HRGN>(rgn1.GetSafeHandle()), TRUE);
rgn1.Detach();

return TRUE;  // return TRUE  unless you set the focus to a control
}

Then, add the windows Message Handler WM_CTLCOLOR and add the following line.

HBRUSH CTestColorDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

// TODO: Change any attributes of the DC here

hbr = m_brush;
return hbr;

}

🙂
Just compile and go..
use one anti glare screen in front of the monitor.. 🙂
hows that..?? 🙂

“No one is perfect until you fall in love with them!”

for https://vctipsplusplus.wordpress.com/

BijU