Wednesday, July 9, 2008

Desktop Spy Using C#.Net

Now I am posting a pure software projects using c#.net. It is very simple spy ware like program.



This program very useful to watch activities in a remote computer connected in network. By installing this program in college or school computer connected in network, administrator can watch activities of students on computer. This is a simple Screen Capture program combined with some network and socket programming in .Net.

Code Description


This code consists of two part a Spy_ client and Spy_ server. Spy_ client capture the desktop and convert it to bitmap image then this image send through network. Capturing Screen is very easy in VC++ coding and lot of codes available to capture screen in VC++ . If you want to capture screen in .Net programming you have to import Some DLL, user32.dll and gdi32.dll.In VC++ you don’t need to import these DLL and directly use functions and structures in these DLL.


Spy client


For example if you want to use DeleteDC() function in gdi32.dll you have to import this DLL using DllImport attribute

[DllImport("gdi32.dll", EntryPoint = "DeleteDC")]

Public static extern IntPtr DeleteDC(IntPtr hDc);

You should use “System.Runtime.InteropServices” namespace for using DllImport.


GetDesktopImage() function capture the desktop screen.



public static Bitmap GetDesktopImage()

{


//for the size of the screen.

SIZE size;


//to handle bitmap.

IntPtr hBitmap;


// handle the desktop device context.

IntPtr hDC = GetDC(GetDesktopWindow());


// compatible device context in memory for screen device context

IntPtr hMemDC = CreateCompatibleDC(hDC);


//to get X coordinates of the screen.

size.cx = GetSystemMetrics(SM_CXSCREEN);


//to get Y coordinates of the screen.

size.cy = GetSystemMetrics(SM_CYSCREEN);


// create a compatible bitmap of the screen size and using

//the screen device context.

hBitmap = CreateCompatibleBitmap(hDC, size.cx, size.cy);


//As hBitmap is IntPtr, we cannot check it against null.

//For this purpose, IntPtr.Zero is used.

if (hBitmap != IntPtr.Zero)

{

// select the compatible bitmap in the memeory device

//context and keep the refrence to the old bitmap.

IntPtr hOld = (IntPtr)SelectObject(hMemDC, hBitmap);


//copy the Bitmap to the memory device context.

BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, SRCCOPY);


//select the old bitmap back to the memory device context.

SelectObject(hMemDC, hOld);


//delete the memory device context.

DeleteDC(hMemDC);


//release the screen device context.

ReleaseDC(GetDesktopWindow(), hDC);


//Image is created by Image bitmap handle and stored in

//local variable.

Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap);


//Release the memory to avoid memory leaks.

DeleteObject(hBitmap);


//This statement runs the garbage collector manually.

GC.Collect();


//Return the bitmap

return bmp;

}


//If hBitmap is null, retun null.

return null;


}


sender(string remote_IP, int port_number) function send these image to remote computer.


public void sender(string remote_IP, int port_number)

{

ms = new MemoryStream();

pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

byte[] arrImage = ms.GetBuffer();

myclient = new TcpClient(remote_IP, port_number);

myns = myclient.GetStream();

mysw = new BinaryWriter(myns);

mysw.Write(arrImage);

ms.Flush();

mysw.Flush();

myns.Flush();

ms.Close();

mysw.Close();

myns.Close();

myclient.Close();

}


A timer used to capture desktop in definite time of interval and send this image to server through defined port and IP address.


Spy Server


Server receive this images and a thread is used to continuously receive image from port. The received images displayed on a picture box control.


listen() function receive data and display on a picturebox.


private void listen()

{


try

{


// Open The Port

mytcpl = new TcpListener(6000);


mytcpl.Start(); // Start Listening on That Port

mysocket = mytcpl.AcceptSocket(); // Accept Any Request From Client and Start a Session


ns = new NetworkStream(mysocket); // Receives The Binary Data From Port


pictureBox1.Image = Image.FromStream(ns);


mytcpl.Stop(); // Close TCP Session


if (mysocket.Connected == true) // Looping While Connected to Receive Another Message

{

while (true)

{

listen(); // Back to First Method

}

}

myns.Flush();

}

catch (Exception) { button1.Enabled = true; myth.Abort(); }

}


This program is very simple to understand there is a lot of tutorials and codes available on internet for network and socket programming in .Net. Network programming is very easy in .Net compared to VC++ programming. This code will give introduction to beginners in network programming in .Net. You can send binary data, images or files through network.I just combine screen capture code and network programming to make this Spy utility.

Sunday, July 6, 2008

Dual Channel Thermometer

Dual channel thermometer is simple PIC ADC based project. It’s a inexpensive thermometer with low cost components .Don’t used any high sensitive or high cost sensor for thermometer. I am using only simple silicon diode. Dual channel means this thermometers have two sensor for pinking temperature from two different place and it displayed on PC using C#.Net programming. To reduce cost I am used only parallel port for interfacing with PC. There is no any additional display devices such as LCD or seven segment display etc.

Circuit Diagram


Simply I draw circuit for this project. You can see that there is no any high cost devices, they are only commonly available components in local market. Two 1N4148 diode is used as temperature sensor. The anode of diodes connecting to VCC through a1K resistors, these resistors are sufficient to flow current through the diodes. Then connect anode of the diode to PIC ADC channels. I am using channel 2 and 3. If you want to measure temperature from two different place just place these diodes to two different places. The ADC output from the PIC put to PORTB. The PORTB connected to data port of the parallel port. To measure two different values from ADC input we are using a control port of the parallel port for switching between two different ADC input.

Software

I am using mikroC compiler for PIC programming. It is very easy to coding in mikroC. You can download demo version from their website.Here is PIC fimware for dual channel thermometer.


unsigned int temp;

void main() {

ADCON1 = 0x80; // Configure analog inputs and Vref

TRISA = 0xFF; // PORTA is input

TRISB = 0; // PORTB is output

TRISD =0xFF; //receive control port instructions

do {

if(PORTD.F2) //checking control port inputs

{

temp = ADC_Read(1)>>2; // Get results of AD conversion from channel 1

PORTB = temp; // Send lower 8 bits to PORTB

}

else

{

temp = ADC_Read(2)>>2; // Get results of AD conversion from channel 2

PORTB = temp; // Send lower 8 bits to PORTB

}

} while(1);

}

C#.Net programming

To reduce cost of display devices I used parallel port programming in c#. It is very easy to develop software for read data parallel port. But you need bi-directional parallel port and Inpout32.dll for reading parallel port.