Monthly Archives: March 2011

Controlling your PC via the internet: A XML Approach

I’ve recently come across thousands of paid apps that allow you to control your pc via the internet through a mobile phone / tablet pc. I couldn’t help pondering over how to code this or implement this in real life. A very simple, stupid approach which I will introduce here through this tutorial should teach you how to easily create a simple framework for access.

This should give you a fair idea how it is going to be implemented

Overview

Again, just so you know – This isn’t the ideal way to implement it but as I said – this is the quick and dirty base you can set up within a few hours.

For this tutorial you will need the following:

  1. A Shared/Dedicated Hosting Web Space with PHP and FTP access
  2. Visual Studio 2010 (C#)
  3. A good php and xml code editor like Dreamweaver
  4. A testing server (Optional)
  5. A mobile device with internet access (For testing!)

I will briefly explain how it works:-

  • A C# executable will be running on your PC (Which you will code of course) which establishes a connection to the server and “listens” (keeps downloading the file every X seconds)
  • The information is downloaded, parsed and actions are taken according to the values found after parsing.
  • This executable shall issue the commands to the .NET API to control your PC ( I will show you a simple mouse demo )
  • A PHP front end interface will allow the user to issue commands to the PC. An HTTP GET request will be dispatched to the server with a variable(number) that tells PHP to write that number out into an XML file and keep it on the server
  • The file is again pulled by the computer and commands are executed.

Lets Start!

1. Coding the C# Application

Fire Up Visual Studio 2010. Open a new C# Project (Windows Forms Application). Make your UI Similar to this.

Four Button Controls , Two Labels, Two Textboxes,A timer, A Status Strip

The Buttons are just for testing your code for moving your mouse. Before I explain what everything is for, start implementing the simple code for moving your cursor. Take a look at this

Cursor.Position = new Point(Cursor.Position.X + d1, Cursor.Position.Y + d2);

This is the crux of the program. This will move your cursor to the X,Y Position already present + the displacement in pixels (d1=displacement in x direction and d2=displacement in y) Replace d1 and d2 with integer values and watch the fun!

private void btnDown_Click(object sender, EventArgs e)
{
 Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y + 10);
}

Once you double click on any of the buttons – just like Visual Basic, code is generated. In that paste the given line with modifications. Keep in mind the coordinate axes meant for computer screens and give values accordingly. I have showed the code for the down button.

Our next job is to create the sample xml file we will use for parsing and upload it to the server.

Now some of you might ask, why XML? The answer is simple. XML is a generic, simple markup language which allows any language to communicate with any other language. The parsing functions for XML might differ but the result is unique. If you are not familiar with XML you can read a bit on http://www.w3schools.com/xml/default.asp

Our XML file structure will be something like this

<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
 <Command>
 <id>263</id>
 <instruction>1</instruction>
 </Command>

<root>

Our ‘root’ nodes shall hold a Command object which has the following properties:

id: A number to keep track of which command was executed previously

instruction: A number which we assign a special meaning to – it tells which instruction is to be executed. For example instruction =1 could mean Mouse Up, Instruction=2 could mean Mouse Down, and so on.

We can also have Multiple Commands if we plan to extend it. For example Open Media Player. Play the file. Exit ; Well you get it!

Now you could code the xml file in any text editor you like, just make sure it is well formed. Once you have done that, save it temporarily on your drive. Open up your FTP Manager Program / Login into your shared hosting control panel File Manager

Transfer the xml file to some directory for testing. I have named mine “remote”.

Keep the absolute location in mind (http://myhosting.com/remote.xml) or whatever , because our program is going to need this.

Now we have to write a function that parses the XML file in C# So we know what to do.

Specify the using directive on the System.Xml namespace so that you are not required to qualify XmlTextReader declarations later in your code. You must use the using directive before any other declarations.

Create an instance of an XmlTextReader object, and populate it with the XML file. Typically, the XmlTextReader class is used if you need to access the XML as raw data without the overhead of a DOM; thus, the XmlTextReader class provides a faster mechanism for reading XML. The XmlTextReader class has different constructors to specify the location of the XML data.

Read through the XML. (Note that this step demonstrates an outer “while” loop, and the next two steps demonstrate how to use that loop to read the XML.) After you create the XmlTextReader object, use the Read method to read the XML data. The Read method continues to move through the XML file sequentially until it reaches the end of the file, at which point the Read method returns a value of “False.”

The code above should help you with the parsing of XML files. I have used Booleans to check at what point we are in the xml file.

while (reader.Read())
{
    // Do some work here on the data.
	Console.WriteLine(reader.Name);
}
Console.ReadLine();

The last three steps were from http://support.microsoft.com/kb/307548 Please refer for more information.

 

I have also handled exceptions for when there is no internet connectivity.

Now where does this function go? Simple! The timer should execute this function every “X” seconds to get the right Data.

private void timer1_Tick(object sender, EventArgs e)ReadMyXML();That should take care of getting and parsing the data.  Your demo is ready and should run with the static xml file.

}

 

Now the question arises, can we generate this file dynamically? Can we get a user to enter a link in his browser passing a parameter he wants so that the xml file is generated?  for example http://harsh.alkutkar.com/remote/?c=1 will generate an xml file that contains a “Mouse Up” command!

The answer is , yes, We can use a dynamic web language such as PHP or python to generate this. PHP shall parse the URL entered in the browser and find the parameter (here it is ‘c’) and its value (1).

Here is the php code for writing out an XML file in the way we want. Make sure permissions for writing files are enabled on your server or it gives an error when you try to write a file.

PHP Code for Index.php

PHP Code for Index.php

Thats it! Try your new link http://mysite.com/remote/index.php?c=3 or something and enjoy the fun. You could do the following modifications to make it fully featured

1. Provide user security with user name and passwords

2. Make an Android App that will have a nice UI and issue those commands

3.Add Code for more functionality – Playing Music, Powerpoint (Google them!)

Let me know on my email harshalkutkar@gmail.com if you do come up with something!

Till then Cheerio!