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.