How Far Can You Go With Managed Code?

by Milad 21. May 2010 22:15

As a fan of computer science and also a fan of Computer Programming Languages and Compiler Design, I’ve been always asking myself this very question.
There is a fact that Native Code, runs faster on their own platforms but developing software using compilers that create Native Code is hard these days. Also designing compilers that generate Native Code costs so much money and effort.
On the other hand, developing software on platforms like Java or .NET Framework ensures the development team that their code would be so much more portable than Native Code. (Well actually this is the main purpose of designing Virtual Machines.)

But in order to gain the good things that Virtual Machines and Managed Codes provide, we have to lose the power that Native Code gives us.

Now the question is, How much power do we really lose?

Microsoft Singularity Operating System

There has been a project being built in Microsoft Research Labs since 2003, called Singularity OS. The most exciting thing about this project is that kernel, device drivers, and applications are all written in managed code. Shocking! Huh? ;-)

(In order to develop this project Microsoft has designed 2 languages based on C#, called Sing# and Spec#)

Apparently, this project is going to be commercial by the time Microsoft is done building the new generation of it’s Operating Systems currently called Midori.

Other Language-based Systems

There are similar projects going on in Java World too. Like the JNode Project and JX Project and by Sun Microsystems, JavaOS.

User Community Activities

User Communities play a big role in mutation of Software Industry.
In this field, the SharpOS Project plays a big role.

As wikipedia says:

SharpOS is an open source .NET-C# based operating system currently under development by a group of volunteers and presided over by a team of six project administrators (Mircea-Cristian Racasan, Bruce Markham, Johann MacDonagh, Sander van Rossen, Jae Hyun and William Lahti). This operating system is only one of two C# based operating systems released under an open source license. SharpOS is still under major development with only one public version available and a basic command line interface.

Conclusion

I think it is pretty exciting for lazy people like me that Low-Level Geeks are making this much effort to make us not feel bad about ourselves! ;-)
On the other hand, I never thought that a team would even consider writing an Operating System with high level languages. But with all the progress that has been made with CPUs and other hardware, it’s reasonable to think about these kind of projects.

Tags: , , ,

Computer | Programming

Programming Windows Sidebar Gadgets

by Milad 2. May 2010 22:38

It’s been a long time since Windows Vista is introduced to the IT World(And how much we are thankful for Windows 7 being introduced!)

One of the remarkable things about Post-XP Windows, is the cute Gadgets floating around our desktops!

This posts tells you how you can make your own Gadgets for Windows Sidebar*

What is a .gadget file?

If you are a curious Mac user(or possibly Yahoo Widget Engine user.) you may already know the answer to this question.
A .gadget file is just an archive file(.ZIP file.) wrapping a bunch of stuff together and making them ready for running under Sidebar.exe.

What is in a .gadget file?

A simple gadget, must have 2 files:

  • gadget.xml
  • An HTML Startup Page

A more complicated one, can have these files too:

  • CSS Files for Styling
  • Javascript Codes (Interacting with Sidebar APIs)
  • A bunch of Images

Gadget.xml:

The manifest of your Gadget. Everything that sidebar should know about your gadget is stored in this file.
This is a general form of a gadget.xml file:


<?xml version="1.0" encoding="utf-8" ?>
<gadget>
        <name>Sample Gadget</name>
        <namespace>windows.sdk</namespace>
        <version>1.0.0.0</version>
        <author name="Microsoft">
                <info url="msdn.microsoft.com" />
                <logo src="logo.png" />
        </author>
        <copyright>&amp;#169; Microsoft Corporation.</copyright>
        <description>Sidebar gadget sample.</description>
        <icons>
                <icon height="48" width="48" src="icon.png" />
        </icons>
        <hosts>
                <host name="sidebar">                         <!-- New autoscaleDPI node -->
                        <autoscaleDPI>true</autoscaleDPI>                         <base type="HTML" apiVersion="1.0.0" src="sample.html" />
                        <permissions>Full</permissions>
                        <platform minPlatformVersion="1.0" />
                        <defaultImage src="icon.png" />
                </host>
        </hosts>
</gadget>

HTML Startup page:

A really simple page containing your gadget.

Sidebar.exe APIs

Well nobody like a static gadget!
Fortunately, you can spice up your gadgets with some APIs provided by Sidebar.exe. For example you can access Power Information of the machine your gadget is running on. Or the information about machine’s Total Memory and Available Memory or available networks in wireless range etc.

Here is a sample Javascript:
(this code uses Sidebar.exe APIs to get Machine Power Status)


function OnPageLoad() {
    System.Machine.PowerStatus.powerLineStatusChanged = GetPowerStatus;

    GetPowerStatus();
}

function GetPowerStatus() {  
    var sMachineInfo;
    // Get the machine details.
    if (System.Machine.PowerStatus.isPowerLineConnected == true) {
        sMachineInfo += "Connected to External Power." + "<br/>";       
    } else {
        var percent = System.Machine.PowerStatus.batteryPercentRemaining;       
        sMachineInfo += "Power Percent Remaining: " + percent.toString() + "%<br/>";
    }

    // Initialize the gadget content.
    var s = document.getElementById("gadgetContent");
    s.InnerHTML = sMachineInfo;
}

Deploying your gadgets:

In order to deploy your gadgets, you should select it’s files and put them into a .ZIP archive. Then change the extension of the file from .ZIP to .Gadget in order to make it visible to Sidebar.exe

Where to now?

http://msdn.microsoft.com/en-us/library/dd370867(VS.85).aspx#development
http://www.codeproject.com/KB/gadgets/InsideBar.aspx

And I really recommend this book (for a quick review, check the book’s source codes.):
Cover image for product 047017661X

And you know the most accurate reference for Microsoft APIs, is MSDN.

* You can use all of these resources for Windows Sideshow Gadget development.

Tags: ,

Computer | Internet | Programming

ASP.NET MVC’s ActionResult in depth

by Milad 22. February 2010 20:47

ActionResult Class is a valuable abstract class in ASP.NET MVC Framework. ActionMethods mostly return an ActionResult.

First let’s see what is an ActionMethod ?

Any class that inherits from System.Web.Mvc.Controller can use action methods to
serve web requests. The requirements for a method to be web-callable as an action method are well documented on http://www.asp.net/mvc. The method
■ Must be public
■ Cannot be a static method
■ Cannot be an extension method
■ Cannot be a constructor, getter, or setter
■ Cannot have open generic types
■ Is not a method of the Controller base class
■ Is not a method of the ControllerBase base class
■ Cannot contain ref or out parameters *

There are some other considerations about ActionMethods like they should not perform data access etc. But this post is about ActionResult class.

According to Reflector, ActionResult’s base type is System.Object and there are 14 classes that are Inherited from this class.

1. ContentResult  —Represents a text result
2. EmptyResult  —Represents no result
3. FileResult  —Represents a downloadable file (abstract class)
4. FileContentResult  —Represents a downloadable file (with the binary content)
5. FilePathResult  —Represents a downloadable file (with a path)
6. FileStreamResult  —Represents a downloadable file (with a file stream)
7. HttpUnauthorizedResult  —Represents the result of an unauthorized  HTTP
request
8. JavaScriptResult  —Represents a JavaScript script
9. JsonResult  —Represents a JavaScript Object Notation (JSON) result that can
be used in an AJAX application
10. RedirectResult  —Represents a redirection to a new URL
11. RedirectToRouteResult  —Represents a result that performs a redirection
given a route values dictionary
12. PartialViewResult  —Base class used to send a partial view to the response
13. ViewResult  —Represents HTML and markup
14. ViewResultBase  —Base class used to supply the model to the view and then render the view to the response

The important point of ASP.NET MVC Framework is it’s Extendibility. So the question here is how can we create a new ActionResult? The answer is quite simple!

namespace MyActionResults
{
    public class SimpleActionResult : System.Web.Mvc.ActionResult
    {
        private string msg;
        public SimpleActionResult(string Message)
        {
            msg = Message;
        }

        public override void ExecuteResult(System.Web.Mvc.ControllerContext context)
        {
            context.HttpContext.Response.Write(msg);
        }
    }
}

This ActionResult, prints a simple message to it’s HTTPContext(Like ContentResult). You can do so many interesting things with this class. Like in MvcContrib Library, There is an XMLResult Class, that puts an XML Output into the response stream.

P.S: This post is mostly inspired by Chapter 4 of Manning ASP.NET MVC in Action Book, titled The controller in depth. I highly recommend this book. :-)

Tags: , , , ,

A simple HTMLHelper for displaying Gravatars

by Milad 2. November 2009 20:44

Practically, The whole world knows about Gravatar(Globally Recognized Avatar).
I wanted to use this awesome service in one of my ASP.NET MVC applications, So I went to it’s documentation to see what should I do. [Well there is a great HTMLHelper in order to perform this, but I have a point!]

The big thing in constructing a Gravatar URL is how to get a string’s hexadecimal MD5 Hash. [as you may or may not know, you can do lots of stuff with MD5 Hash of anything!]
So … This is the answer to this question:

Function GetHash(ByVal text As String) As String
        Dim myhash As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()

        Dim hashed = _
            myhash.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(text))

        Dim sb As StringBuilder = New StringBuilder()

        For Each b In hashed
            sb.Append(b.ToString("x2"))
        Next

        Return sb.ToString()
End Function

Tags: , , ,

ASP.net | Computer | Internet | Programming