Search This Blog

Wednesday, February 20, 2013

How to Automate Button OR Check box click?

We can automate command button click or check box of another running  application using the following sample code.

To automate, first we need the window handle of the application that we need to automate. We can get the handle by using FindWindow() API.


struct stUserInterFace
{
HWND handle;
LPCTSTR lpCaption;
};



BOOL CALLBACK GetAllButtons(HWND handle,LPARAM lpParam)
{
    stUserInterFace *ptrStUI=NULL;
    ptrStUI=(stUserInterFace*)lpParam;
    HWND hwndBtn=ptrStUI->handle;

    TCHAR    szClassName[128];
    GetClassName(handle,(LPSTR)&szClassName,sizeof(szClassName));

    if (_tcscmp(szClassName,_T("ThunderRT6CommandButton")) == 0 ||_tcscmp(szClassName,_T("Button")) == 0 )
    {
        TCHAR szCaption[128];
        GetWindowText(handle,(LPSTR)&szCaption,sizeof(szCaption));

        if (_tcscmp(szCaption,ptrStUI->lpCaption) == 0)
        {
            ptrStUI->handle = handle;
            return FALSE; // no more child windows
        }
    }
return TRUE;
}


BOOL ClickButton(HWND hWnd,LPCTSTR lpButtonCaption)
{
    BOOL bRet=FALSE;
    HWND    hwndBtn=NULL;

    stUserInterFace *ptrUI=new stUserInterFace;
    ptrUI->lpCaption=lpButtonCaption;
    
    EnumChildWindows(hWnd,GetAllButtons,(LPARAM)ptrUI);
    if (ptrUI->handle != NULL) 
    {
        PostMessage(ptrUI->handle,BM_CLICK,0,0);    
        bRet=TRUE;
    }
    delete ptrUI;
    return bRet;
}
BOOL CheckBox(HWND hWnd,LPCTSTR lpButtonCaption)
{
    BOOL bRet=FALSE;
    HWND    hwndBtn=NULL;

    stUserInterFace *ptrUI=new stUserInterFace;
    ptrUI->lpCaption=lpButtonCaption;
    
    EnumChildWindows(hWnd,GetAllButtons,(LPARAM)ptrUI);
    if (ptrUI->handle != NULL) 
    {
        PostMessage(ptrUI->handle,BM_SETCHECK,1,0);    
        bRet=TRUE;
    }
    delete ptrUI;
    return bRet;
}
 
Usage: 
        ShellExecute(NULL,NULL,"Test.EXE",NULL,NULL,SW_SHOW);
 Sleep(2000);
 HWND hwnd=::FindWindow(NULL,"Test");

 if(hwnd!=NULL)
 {
  ClickButton(hwnd,"TestButton");
 }

Tuesday, February 19, 2013

Why a constructor cannot be declared as virtual?

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor.

We can't override a constructor. When any method is declared as virtual it should be overridden in its derived class. Since constructor is used for initializing the variables and if declare a constructor as virtual it can’t be overridden.

Chat Client cheater Software

In my previous post Chat client cheater, I posted a sample code to send fake mouse movements and make your chat instant messenger to show you always in online.

Now I created it as a software,  You can download and use it directly for free.

for more details kindly visit,
http://www.smartpricedeal.com/products/chatclientcheater.html

Singleton Class in C++


Singleton pattern is a design pattern that restricts the instantiation of a class to one object.

Application:
  • This pattern can be used in "Database class" to that insert and retrieve record's in threads.
  • Can be used in Logger class.
How to create?
  • Define the constructor as private.
  • Declare copy constructor and Assignment operator as private to avoid creating copy of object
Sample 1( Allocating memory dynamically with new operator):

 
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;


class Singleton
{
private:
    static Singleton *m_ptrInstance;
    Singleton(){};
    Singleton(Singleton const&);             
    void operator=(Singleton const&);

public:
    static Singleton* GetInstance()
    {
        if(m_ptrInstance==NULL)
        {
            m_ptrInstance=new Singleton();
        }
        return m_ptrInstance;
    }
    void Display()
    {
        cout<<"This is singleton class"; 
    }
};
Singleton* Singleton::m_ptrInstance=NULL;


int _tmain(int argc, _TCHAR* argv[])
{
    //How to call    
    Singleton::GetInstance()->Display();
    getch();
    return 0;
}
 
Sample 2( Without allocating memory dynamically with new operator):

#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;


class Singleton
{
private:
    Singleton(){}
    Singleton(Singleton const&);             
    void operator=(Singleton const&);

public:
    static Singleton& GetInstance()
    {
        static Singleton _instance;
        return _instance;
    }
    void Display()
    {
        cout<<"This is singleton class"; 
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    //How to call    
    Singleton::GetInstance().Display();
    getch();
    return 0;
}