Search This Blog

Thursday, January 3, 2013

How to Intialize the Array elements value to NULL?

Usually we initialize the dynamically allocated array values to NULL using the memset.

We can do it while we allocate memory itself, by adding parenthesis ()  after the size.

Sample Code snippet:
 
int* nIntializedArray=new int[3]();

Example:


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

int _tmain(int argc, _TCHAR* argv[])
{
    int i=0;
    cout<<"Uninitialazied Arrary of int* \r\n\r\n";

    int* nUnIntializedArrayptr=new int[3];
    for( i=0; i<3; i++)
    {
        cout<<nUnIntializedArrayptr[i]<<"\r\n";
        
    }
    delete[] nUnIntializedArrayptr;
    cout<<"\r\n\r\n";


    cout<<"Initialazied Arrary of int* \r\n\r\n";
    int* nIntializedArray=new int[3]();
    for( i=0; i<3; i++)
    {
        cout<<nIntializedArray[i]<<"\r\n";
        
    }
    delete[] nIntializedArray;


    cout<<"UnInitialazied char* \r\n\r\n";
    char* sztxt=new char[3];
    cout<<sztxt<<"\r\n";    
    delete[] sztxt;

    cout<<"Initialazied char* \r\n\r\n";
    char* szIntialTxt=new char[3]();
    cout<<szIntialTxt<<"\r\n";    
    delete[] szIntialTxt;


    getch();
    return 0;
}
 
 

How to find the size of a dynamically allocated array?

Usually when we pass a pointer to an array to another function, we pass its size also as the parameter.
Which is not required, if the array is dynamically allocated.

We can find the size of a dynamically allocated array using _msize() function.


    TCHAR *szTxt=new TCHAR[256];
    int nArraySize=0;
    nArraySize=    _msize(szTxt)/sizeof(TCHAR);


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

void GetIntPtrArray(int* ptrInt)
{
    //To find size of Array
    int nSize=_msize(ptrInt)/sizeof(int);
    cout<<"\r\nThe size of the Integer Array is "<<nSize;
}

void GetCharPtrArray(char* ptrchr)
{
    //To find size of Array
    int nSize=_msize(ptrchr)/sizeof(char);
    cout<<"\r\nThe size of the Char Array is "<<nSize;
}
int _tmain(int argc, _TCHAR* argv[])
{
    int *nArr=new int[10];
    GetIntPtrArray(nArr);

    char *szTxt=new char[256];
    GetCharPtrArray(szTxt);

    getch();
    return 0;
}
 
 

How to update the icon changes when installing?

BMP files can be opened using MS Paint or Windows photo viewer, or with some other programs.
But Icon of that file, is the default program that associated with that extension. you can also change the default program to open files.

Even you can create your own file extension documents for your application. To update the icon of the files and to refresh the windows after default program changed or your application installation can be done using the following code.


SHChangeNotify(SHCNE_ASSOCCHANGED,0,NULL,NULL);

How to check whether internet connection is available?

Some times we need to check the internet connection before doing updates or online license check.

How to find  it? Use the following code.



#include "intshcut.h"
#pragma comment(lib,"url.lib")

if( !InetIsOffline( 0 ))
{
    // Internet Connection is available  
}

Thursday, September 20, 2012

Virtual destructor

One of the common C++ interview question is what is virtual destructor? Or what is the use of virtual destructor?
You are assigning a derived class object to a base class pointer and deleting the base class pointer.
If the base class destructor is not declared as virtual, then derived class destructor won’t get called.
If you are deleting some variables allocated heap memory in derived class destructor, then it will cause memory leak.



// CPPSamples.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
class Base
{
    public:
    Base()
    {
        cout<<"\n Base class Constructor\r\n";
    }
    ~Base()
    {
        cout<<"\n Base class Destructor\r\n";
    }

};
class Derived:Base
{
public:
    Derived()
    {
        cout<<"\n Derived class Constructor\r\n";
    }
    ~Derived()
    {
        cout<<"\n Derived class Destructor\r\n";
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Base *ptr=(Base*)new Derived();
    delete ptr;
    getch();
    return 0;
}
 

 


 
If the base class Destructor is virtual then,
 
// CPPSamples.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
class Base
{
    public:
    Base()
    {
        cout<<"\n Base class Constructor\r\n";
    }
    virtual ~Base()
    {
        cout<<"\n Base class Destructor\r\n";
    }

};
class Derived:Base
{
public:
    Derived()
    {
        cout<<"\n Derived class Constructor\r\n";
    }
    ~Derived()
    {
        cout<<"\n Derived class Destructor\r\n";
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Base *ptr=(Base*)new Derived();
    delete ptr;
    getch();
    return 0;
}
 
 

Monday, September 10, 2012

Rollback is not working in PhpMyAdmin MySQL

Once I had problem, in rollback my changes in  MySQL.
I tried disabling autocomit. But problem was not resolved.

/* disable autocommit */
$mysqli->autocommit(FALSE);

$mysqli->rollback();
mysql_query("ROLLBACK");


Then finally, I found the actual problem.
By default in PhpMyadmin table type is MyISAM. Rollback will not work in MyISAM.



Then I changed the table in to InnoDB, It worked!!!

Friday, September 7, 2012

Chat Client Cheater


Your boss and your girlfriend can able to monitor you, using the chat client installed in your computer. If you didn’t press any key or move your mouse for long time then chat client status will automatically goes to “Away”. Using that, they can find, when you are coming to or leaving the office and how much time you are spending in cafeteria.

In most of companies, they have chat clients for internal communication.  Mostly they use Microsoft Lync or some other XMPP Chat client similar to G-Talk.  We can cheat them, by sending fake mouse movement.
I have created an application, which sends the fake mouse events. So I always switch off my monitor alone and go to cafeteria or leave office early. But if anyone can, switch on the monitor and use my computer. My company policy doesn’t allow me to leave my desk without locking my computer.  So if someone touches (Mouse move or Key Press) my computer it will automatically get locked.  You can also set a secret key to deactivate this application and not to get locked when you came back.


 Source Code in VC++ (MFC):




HHOOK g_hDesktopHook; // Also declared global
HHOOK g_hKeyboardHook; // Also declared global
BOOL g_bMouseEvent=TRUE;
CWinThread *g_MouseThread=NULL;
int g_nXPos=0;
int g_nYpos=0;

void SendMouseMoveMessage()
{
    INPUT ip;

    ip.type = INPUT_MOUSE;
    ip.mi.dx = 0;
    ip.mi.dy = 0;
    ip.mi.mouseData = 0;
    ip.mi.dwFlags = (MOUSEEVENTF_MOVE || MOUSEEVENTF_ABSOLUTE);
    ip.mi.time = 0;
    ip.mi.dwExtraInfo = GetMessageExtraInfo();
    SendInput(1, &ip, sizeof(ip));
}
UINT MouseMoveThread(LPVOID lpMsg)
{
    while(g_bMouseEvent)
    {
        SendMouseMoveMessage();
        Sleep(5000);
    }
    return 0;
}
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if(nCode < 0)
        return CallNextHookEx(g_hDesktopHook, nCode, wParam, lParam);
    KBDLLHOOKSTRUCT *msg = (KBDLLHOOKSTRUCT*)lParam;
    if(nCode == HC_ACTION)
    {

        if(wParam == WM_LBUTTONDOWN ||wParam == WM_RBUTTONDOWN ||wParam ==WM_MOUSEMOVE )
        {
            LPMOUSEHOOKSTRUCT lpMouse=NULL;
            lpMouse=(LPMOUSEHOOKSTRUCT)lParam;
            if(lpMouse)
            {
                if(g_nXPos==0 && g_nYpos==0)
                {
                    g_nXPos=lpMouse->pt.x;
                    g_nYpos=lpMouse->pt.y;
                }
                if(g_nXPos!=lpMouse->pt.x||    g_nYpos!=lpMouse->pt.y)
                {
                    g_bMouseEvent=FALSE;
                    //Lock Computer
                    LockWorkStation() ;
                    AfxGetMainWnd()->PostMessage(WM_CLOSE);
                }
            }
        }
        else if (  wParam == WM_KEYUP || wParam ==WM_KEYDOWN) 
        {
            if ( msg->vkCode =='U' ) 
            {
                //If U is pressed Remain Unlocked and Exit 
                //Else lock computer
                g_bMouseEvent=FALSE;
                AfxGetMainWnd()->PostMessage(WM_CLOSE);

            }
            else
            {
                g_bMouseEvent=FALSE;
                LockWorkStation() ;
                AfxGetMainWnd()->PostMessage(WM_CLOSE);
                
            }
        }   
    }

    return CallNextHookEx(g_hDesktopHook, nCode, wParam, lParam);
}
void CChatClietCheaterDlg::OnBnClickedStart()
{
    Sleep(2000);
    if((g_hDesktopHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, NULL, 0)) == NULL)
    {
    // hook failed...
    }
    
    if((g_hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, MouseProc, NULL, 0)) == NULL)
    {
        //  hook failed....
    }
    g_MouseThread=AfxBeginThread(MouseMoveThread,NULL);
    
}

void CChatClietCheaterDlg::OnDestroy()
{
    CDialogEx::OnDestroy();
    g_bMouseEvent=FALSE;
    if(g_hDesktopHook)
        UnhookWindowsHookEx(g_hDesktopHook);

    WaitForSingleObject(g_MouseThread->m_hThread,5000);
}
 
Update: 
 
I created it as a software application. for details please visit 
http://mykodetips.blogspot.com/2013/02/chat-client-cheater-software.html