Sunday, January 20, 2013

Interrupting Cow

Interrupting Cow

It's my son's favorite joke. He thought it was funny :)

Thursday, November 1, 2012

Surviving Hurricane Sandy - Charging my phone on batteries

Alright so Sandy paid us a visit this week. We thought we were in the clear, it was the peek of the storm and we still had power at 8:30pm. Unfortunately we did not have power at 8:31pm.

Of course the first thing you think of is "do I have the bare necessities". No, I already said I didn't have electricity. What am I going to do? My phone was low and batteries and I just started watching "Walking Dead" on Netflix. Was I going to have to miss it?

Not if I had anything to do with it. So I started thinking how could I charge my phone. I have a bunch of electronics, parts, and AA batteries. There had to be something I could do.

So I figured USB devices rely on 5 volts coming from the cable, that must be what charges the devices. So that's it it's simple. Cut a USB cable, connect 5 volts to it via AA batteries, and I'm done.

Except it didn't work. Apparently as I found out there is a communications that happens between the device and the host before the device will try to charge itself from the host.

So then I figured I could power a device that was already a USB host. First I thought of the Beagle Board, or the Raspberry PI. But then I thought what about a USB hub?

I have a portable Targus USB hub, if I could power that, it might work. It worked!



Sure a better man would have prepared himself before the storm, but I'm content being a guy who got to watch his show.

If you want to know some additional details. I did not have a power connector so I rolled up a piece of paper and put the positive wire inside and the negative wire across the outside. Because this was a pretty flimsy connection, I used a piece of pegboard and plastic strap to hold everything in place.

Also I had to swap out batteries routinely. Charging a smart phone takes a lot of juice and if the voltage dropped below 5 volts charging would not. Far from efficient but it did the job.

For next time though I think I'm just going to get one of these ahead of time:

Energizer Energi Charger
Tekkeon MP1580 TekCharge Mobile Power and Battery Charger

or build one of these:

http://www.instructables.com/id/DIY-iPhone-Chargers/

Thursday, September 20, 2012

Configure IIS SSL host header using a UI



IIS has a feature that allows multiple sites to share the same port, but use different domain names. Unfortunately the IIS UI doesn't let you do that with HTTPS sites that use SSL certificates. Host name is literally disabled when when you select HTTPS.

This is only a UI limitation and you can actually assign a hosting name using the `appcmd` command line application. But if you are like me you sometimes prefer a UI especially when doing the same thing repeatedly.

So I created IIS Buddy, it mimics the IIS user interface, but unlike the native UI it lets you assign host names to HTTPS SSL sites just as easily as you can assign host names to regular HTTP site.


Here are some screenshots:

 

Tuesday, August 14, 2012

Inline IL in C#

One of the features I liked in C++ was the ability to include inline assembly. Granted I never found much practical use for it but it was a great way to learn assembly without having to write a whole application in it.

Well a very clever Canadian by the name of Roy has now made that possible in C# and VB.NET. Not only is it a great feature but the way he implemented it is very clever and can be applied to do your own IL manipulation of your binaries.

Basically Roy uses the existing Visual Studio tools ildasm and ilasm to decompile your binary into IL, insert your IL code blocks, and recompile the result.

You specify your IL by enclosing it inside an #if directive, like so:

public static int Add(int n, int n2) {
#if IL
    ldarg n
    ldarg n2
    add
    ret
#endif
    return 0; // place holder so method compiles
}

Adding this feature to your project is as simple as adding a post-build step. You can find the whole article here: http://www.codeproject.com/Articles/438868/Inline-MSIL-in-Csharp-VB-NET-and-Generic-Pointers

Monday, August 13, 2012

Invoke base method using reflection

I came across this problem when I was writing a proxy library. I wanted to invoke the method of the base class but was instead invoking the overriden version. I was able to find the answer on Stack Overflow. Here is the code sample of the problem.
class BaseClass {
    public virtual void Test() { 
        Console.WriteLine("Test() from BaseClass"); 
    }
}

class OverridingClass : BaseClass {
    public override void Test() { 
        Console.WriteLine("Test() from OverridingClass"); 
    }
}

public static void Main() {
    var d = new OverridingClass();
    typeof(BaseClass).GetMethod("Test").Invoke(d, null);
}
The output is Test() from OverridingClass but what I wanted is to call the version of Test() defined in BaseClass.
The answer on Stack Overflow was great. Basically he created a DynamicMethod that would do the job. All I did was take that and generalize it so that it could be used as a drop in replacement for Invoke. So I created the following extension method:
public static object InvokeNotOverride(this MethodInfo methodInfo, 
    object targetObject, params object[] arguments) {
    var parameters = methodInfo.GetParameters();

    if (parameters.Length == 0) {
        if (arguments != null && arguments.Length != 0) 
            throw new Exception("Arguments cont doesn't match");
    } else {
        if (parameters.Length != arguments.Length)
            throw new Exception("Arguments cont doesn't match");
    }

    Type returnType = null;
    if (methodInfo.ReturnType != typeof(void)) {
        returnType = methodInfo.ReturnType;
    }

    var type = targetObject.GetType();
    var dynamicMethod = new DynamicMethod("", returnType, 
            new Type[] { type, typeof(Object) }, type);

    var iLGenerator = dynamicMethod.GetILGenerator();
    iLGenerator.Emit(OpCodes.Ldarg_0); // this

    for (var i = 0; i < parameters.Length; i++) {
        var parameter = parameters[i];

        iLGenerator.Emit(OpCodes.Ldarg_1); // load array argument

        // get element at index
        iLGenerator.Emit(OpCodes.Ldc_I4_S, i); // specify index
        iLGenerator.Emit(OpCodes.Ldelem_Ref); // get element

        var parameterType = parameter.ParameterType;
        if (parameterType.IsPrimitive) {
            iLGenerator.Emit(OpCodes.Unbox_Any, parameterType);
        } else if (parameterType == typeof(object)) {
            // do nothing
        } else {
            iLGenerator.Emit(OpCodes.Castclass, parameterType);
        }
    }

    iLGenerator.Emit(OpCodes.Call, methodInfo);
    iLGenerator.Emit(OpCodes.Ret);

    return dynamicMethod.Invoke(null, new object[] { targetObject, arguments });
}
With this extension method, the only change you have to make to the original code is to change the call to Invoke() to InvokeNotOverride(). Like so:
typeof(BaseClass).GetMethod("Test").Invoke(d, null);
becomes:
typeof(BaseClass).GetMethod("Test").InvokeNotOverride(d, null);
This has worked for my purposes, the method could benefit from caching since I think generating a DyanmicMethod each time is probably going to eat up memory.
You can download the source code from here: MethodInfoExtensions.zip

Monday, August 6, 2012

Change Windows 7 Logon Screen Wallpaper

So I wanted to change the wallpaper on my logon screen for Windows 7. I found some great articles online on how to do it. Then I decided to create a quick little app to make easier for me and anyone else to change it in the future.

It works around the limitations of Windows. For example Windows 7 only supports JPEGs, this tool supports many other formats including PNG, BMP, and TIFF.

It also gets around the Windows file size limit of 256 KB. It gets around this by compressing image and adjusting the size to fit the screen.

The interface is very simple, just a button to select the image you want to make into your wallpaper background, and another button to remove it.

You can download it here: Download

Simpler WPF Binding

WPF has a lot of great concepts. One of my favorite was the implementation of data binding. The syntax though can get pretty tough and you end up having to look it up every time or using some kind of cheat sheet. Also you can't bind directly to methods, you have to wrap methods around commands.

There limitation really slowed me down, luckily WPF binding is extendedable, so I created my own binding which simplified things a lot. It simply allows you to use a simpler syntax. Below are some examples an a link to download the source code.

// before
{Binding Path=PathToProperty, RelativeSource={RelativeSource Self}}
// after
{BindTo PathToProperty}

// before
{Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}
// after
{BindTo Ancestor.typeOfAncestor.PathToProperty}

// before
{Binding Path=PathToProperty, RelativeSource={RelativeSource TemplatedParent}}
// after
{BindTo Template.PathToProperty}

// before
{Binding Path=Text, ElementName=MyTextBox}
// after
{BindTo #MyTextBox.Text}

// BEFORE
// in your class (RelayCommand available separately)
private ICommand _saveCommand;
public ICommand SaveCommand {
 get {
  if (_saveCommand == null) {
   _saveCommand = new RelayCommand(x => this.SaveObject());
  }
  return _saveCommand;
 }
}

private void SaveObject() {
 // do something
}
// in your xaml
{Binding Path=SaveCommand}

// AFTER
// in your class
private void SaveObject() {
 // do something
}
// in your xaml
{BindTo SaveObject()}

The 'BEFORE' example for binding to a method is already shorter than it would have been without the aid of RelayCommand which I don't think of a native part of WPF. You can find the code for it here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

Source Code WpfBindTo.zip
Just the DLL WpfBindTo.dll

Privacy Policy