Search This Blog

Friday, April 5, 2013

C# Code To Execute a Command line Dos Exe and display the result


Some times we may need to execute a another command line executable file and display its result as string.
You can use the following code snippet for that.


public static string ReadOutputFromCmdExection(string strFileName, string strParameter)
{
    string StrResult = "";
    ProcessStartInfo start = new ProcessStartInfo();
    start.FileName = strFileName; // Specify Executable Name.
    start.UseShellExecute = false;
    start.Arguments = strParameter;
    start.RedirectStandardOutput = true;

    using (Process process = Process.Start(start))
    {
        //
        // Read in all the text from the process with the StreamReader.
        //
        using (StreamReader reader = process.StandardOutput)
        {
            StrResult = reader.ReadToEnd();
        }
    }
    return StrResult;
}
Usage:
 string strTxt = ReadOutputFromCmdExection(@"c:\windows\system32\ipconfig.exe","" );

Console.WriteLine(strTxt);





This helps to get the out put as string, so that we can parse it and use as we require.

Thursday, April 4, 2013

C# Code Excel Get Column Name Using Column Number

We use row and column number to access the values in Ms Excel cells.

In program we use  row and col as number. but When we open Excel Column is represented as alphabets as A, B, C......AA, AB, AC....



Following is the code to convert column number into Column name.


 

private string GetExcelColumnName(int columnNumber)
{
    int dividend = columnNumber;
    string columnName = String.Empty;
    int modulo;

    while (dividend > 0)
    {
    modulo = (dividend - 1) % 26;
    columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
    dividend = (int)((dividend - modulo) / 26);
    }

    return columnName;
}

Thursday, March 21, 2013

How to Print the Key and Value of PHP Array?

$data = array(
    "1"   => "One",   
    "A" => "Apple",
    "B"    => "Ball",
    "C"  => "Cat"
);

foreach ($data as $key => $value)
{
  echo $key."=".$value;
  echo "<br/>";
}

Output:


1=One
A=Apple
B=Ball
c=Cat

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;
}