Thursday, 23 February 2017

A Simple Mouse Logger in Windows using c++

The mouse logger is an important tool to track computer mouse input activities. Here the simplest example of mouse logger is given. 

A callback function (procedure) is defined for mouse to track its activities. This procedure is called every time by SetWindowsHookEx function when a mouse input is applied.


Source Code:
//_____________________________________________________________
#include <Windows.h>
#include <stdio.h>
HHOOK hMouseHook;
LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
       MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
       if (pMouseStruct != NULL)
       {
              if (wParam == WM_LBUTTONDOWN) //When mouse left button is clicked.
              {
                     printf("Mouse isclicked.\n");
                     MessageBox(NULL, "mouse is clicked!!!", "key pressed", MB_ICONINFORMATION);
              }
       }
       return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
/*Set the hook and set it to use the callback function above. WH_MOUSE_LL means it will set a low level mouse hook. More information about it at MSDN. The last 2 parameters are NULL, 0 because the callback function is in the same thread and window as the function that sets and releases the hook. If you create a hack you will not need the callback function in another place then your own code file anyway.Read more about it at MSDN. */
void SetHook()
{
       hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, NULL, 0);
}
void ReleaseHook()
{
       //If you need to release thismouse hook, call this function.
       UnhookWindowsHookEx(hMouseHook);
}
int main()
{
       SetHook();
       MSG message;
       while (GetMessage(&message, NULL, 0, 0))
       {
              ;
       }
       return 0;
}
//______________________________________________________________


Output:


Thursday, 16 February 2017

Adding Two Integer Numbers Without Using + Operator in C/C++

Using the concept of boolean algebra we can get sum of two integers without using the plus operator or any other mathematical operator like -,x,/ etc. 

We know that the outputs of half adder is (Sum = A XOR B , Carry = A AND B ) and in the full adder the carry is added to the sum and also in the parallel adder the output carry is shifting in the left side. In C/C++ XOR, AND and Left Shift operations are defined using ^ , &, << operators respectively.

Source code:
#include<stdio.h>
#include<conio.h>
int FullAdder(int x, int y)
{
       int S = (x) ^ (y);          //Output Sum Of Half Adder
       int C = (x)&  (y);        //Output Carry Of Half Adder
       if (y == 0)
       {
              return x;
       }
       else if (x == 0)
       {
              return y;
       }
       else
       {
              return FullAdder(S, C << 1);
       }
}
int main()
{
       printf("Sum of 57863, 78437 = %d\n", FullAdder(57863, 78437));
       printf("Sum of 34567, 23453 = %d\n", FullAdder(34567, 23453));
       printf("Sum of 57863, 78437, 34654 = %d\n", FullAdder(FullAdder(57863, 78437), 34654));
       printf("Sum of 57, 784,4354,64657,344546 = %d\n", FullAdder(FullAdder(FullAdder(57, 784), FullAdder(4354, 64657)), 344546));
       _getch();
       return 0;
}
Output of the code: