Monday, January 27, 2014

Programming for daily life - Introduction to programming

It was my great ambition to take a training to non programmers about programming. I had taken training sessions to programmers about various languages and technologies. When I talk to programmers mostly I feel that I and the audience are in same frequency. I was always wondering, what will be the experience if I explain "what is programming" to non-programmers such as HR team,Admin team, QA team, UI team etc... Recently in my company I got a chance to conduct such as session. We named it as "Programming for daily life" and split into 3 sessions.

  • Introduction
  • Debugging
  • More on scripting (Office automation using Excel,Word etc...)
Due to 3 rescheduling of first session itself, there were not may attendees. But I believe they got something. At least what is program, who is programmer and his duties and how a program is executed in machine. This was one of the training I got satisfaction as a trainer. The ppt I used is attached below. If you are not able to view it go directly to the slideshare.

I am really interested to take the session again and again.

Tuesday, January 14, 2014

Running C# in PowerShell

How to develop C# without Visual Studio?

I have't bought a laptop in the last 8 years of my career as the companies I worked and working provided me laptops and allowed to take it to home. Now a days my current company is not allowing me to take laptop to home due to auditing. It was OK in the initial days. But now it started affecting my learning. I tried to install Visual Studio express editions in my old desktop with 1 GB RAM. But it cries. So started thinking about alternatives. Algorithms I can try using Javascript and PowerShell. But recently I had to try something particularly in C#. Basically my problem was how to compile and debug C# applications without VisualStudo ?. I thought of using C# compiler (csc.exe) alone. But it will stop any hope of debugging.

Running C# in PowerShell

When I was reading blogs in Feedly, one of the post got my attention. It was talking about adding Types into PowerShell. If I can inject a type which is created from C# source I can write C# apps in PowerShell. Some more google revealed the technique. Its as follows
  • Wrap the C# class definition in a variable.
  • Use the Add-Type cmdlet to load the class from source code stored in above variable
  • Create object of new class using New-Object as usual and use it.

$cs_source = "
public class Calc
    public void Foo()
        System.Console.WriteLine(""Foo called"");
    public static int Add(int n1, int n2)
        return (n1 + n2);
Add-Type -TypeDefinition $cs_source
#Call static variable
[Calc]::Add(4, 3)
$basicTestObject = New-Object Calc
#Call instance variable

Limitations are there for this method. If we modify the C# code and try to load again it will say

Add-Type : Cannot add type. The type name 'Calc' already exists.

This is the behaviour of .Net runtime. In .Net we cannot change the types once its loaded into an AppDomain. PowerShell runs on .Net and uses single AppDomain. So its not possible to remove and load again unless we start a new PowerShell session. We can adopt different methods based on our situation.

Rename the class along with modification

This might not be possible if we have frequent modifications. Also we need to modify the places we are creating objects.

Run in different session

Keep the original PowerShell code in one file say cstest.ps1 Then invoke it from different PS session. You may open cstest.ps1 in one tab of PowerShell ISE and execute the below script from another tab.

powershell D:\Temp\cstest.ps1

Other alternatives to develop C# code without VisualStudio

Monday, January 6, 2014

What is w32tm.exe in Windows?

If you are always using new computers or if you frequently upgrade machines, you will not face the issue of setting time each and every time you start computer. In my case, I am now using my old computer and its battery is in bad state to keep up the time when I turn off system.

Since I am using it mainly for browsing internet will be always there even though it is low speed. So decided not to replace the batter instead sync the time time with internet servers when ever I turn on the computer.Since I am using Win 7 I am able to do this by below steps

  1. Right click on the digital time shown in task manager and select 'Adjust date / time'
  2. In the opened dialog select 'Internet Time' tab
  3. Click on 'Change Settings'
  4.  Tick the "Synchronize with an Internet time server'
But this does syncing in a scheduled fashion. People says it differently such as one week and we can change by editing the registry etc..Somebody says the setting is only to make sure the time service is running not to correct the time.

But what I need is to set the time after I succeeds in dialing for internet. I can force the sync by using the 'Update now' button in the dialog.But it is too time consuming. So what should I do?

As per my belief, programmer should first write code to solve his problems before he code for others. So started digging for snippets which does this. This time cleverly didn't google for C#. Started with PowerShell as my aim was to have a simple script file which I can double click after connecting to internet. I got some snippets which does more like hitting the time server and updating the time. These are not the command which invokes the 'Update now'. As Windows is following the philosophy of command first approach, I was sure that I can find the corresponding command and I got after some more google. Its nothing but W32tm.exe located at <system drive>:\Windows\System32

Running w32tm.exe

When I simply run the command in the PowerShell window I got the below error

The following error occurred: The service has not been started. (0x80070426)

This is a good finding. Its looking for a service. Is it looking for a windows service in local machine or the internet time server service? I had disabled all the unfamiliar services in my machine as its too slow. After going thorough the services list, I would see one service names "Windows Time" which seems related to the above error message. I cannot enable it permanently as the machine is slow. So wrote in code itself.

net start w32time
w32tm.exe /resync
net stop w32time

When I run this ends with Access is denied.

It is little difficult to run the script in admin privilege in PowerShell. Either we need to have one file to start the actual file or we need to manually open cmd window as admin them execute the required script. So thought of moving top old favorite .bat script. Yes it worked

@echo off
net start w32time
w32tm.exe /resync
net stop w32time

One time I got one issue which tells, cannot do sync as the time difference is big. In those situations just use the /force switch along with /resync. Explore the windows documentation about w32tm if you are interested.

Happy scripting.