Monday, January 31, 2011

Web page to access clipboard exception with Watin

I have been working on a solution to get the clipboard working with Watin automated program. because security exception is really annoying!

here is goes,

class Program
{
[STAThread]
static void Main(string[] args) {


SecurityAlertDialogHandler securityAlertDialogHandlerMock = new SecurityAlertDialogHandler();

using (IE ie = new IENoWaitForComplete("url goes here")) {

ie.AddDialogHandler(securityAlertDialogHandlerMock);

//GetPicture(ie.Images[0], ie);
ie.WaitForComplete();

IEElement banner = ieImages[0].NativeElement as IEElement;

IHTMLElement bannerHtmlElem = banner.AsHtmlElement;
IEElement bodyNative = ie.Body.NativeElement as IEElement;
mshtml.IHTMLElement2 bodyHtmlElem = (mshtml.IHTMLElement2)bodyNative.AsHtmlElement;
mshtml.IHTMLControlRange controlRange = (mshtml.IHTMLControlRange)bodyHtmlElem.createControlRange();

controlRange.add((mshtml.IHTMLControlElement)bannerHtmlElem);
controlRange.execCommand("Copy", false, System.Reflection.Missing.Value);
controlRange.remove(0);

if (Clipboard.GetDataObject() != null) {
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Bitmap)) {
System.Drawing.Image image = (System.Drawing.Image)data.GetData(DataFormats.Bitmap, true);
// do something here
}
}
}
}

}

class IENoWaitForComplete : IE
{
public IENoWaitForComplete(string url) : base(url) { }

public override void WaitForComplete(int timeOutPeriod) {
// Skip Wait logic
}
}

class SecurityAlertDialogHandler : BaseDialogHandler
{
private bool _hasHandledSecurityAlertDialog;

public bool HasHandledSecurityAlertDialog {
get { return _hasHandledSecurityAlertDialog; }
}

public override bool HandleDialog(Window window) {
new WinButton(1, window.Hwnd).Click();
return true;
}

public override bool CanHandleDialog(Window window) {
return window.StyleInHex == "94C808CC";
}

}

Tuesday, January 25, 2011

C# Enum.prase in Java or Enum valueOf in C#

this is how to use it in Java

Java
------
public enum Direction {
NORTH, SOUTH, EAST, WEST
}

Direction home = Direction.valueOf(“SOUTH”);

C#
------

System.Enum.Parse(typeof(Direction), "SOUTH",true);

varibale++ vs ++varibale confustion in Java

I just noticed that variable name ++ assignments can be bit confusing (may be due to the fact that I have been a Microsoft based programmer from almost 10 years).

here goes.

Eg,

int r = 4;
int result = r++;

guess whats the result value going to be? it is still 4. Because Java won't perform the actual calculation of the operator until you use variable r somewhere in the code.

however if you use

int r = 4;
int result = ++r;

this will perform the arithmetic operation inline and return the result to 'result' variable.

Friday, January 14, 2011

C# Format.String ( {0} ) equivalent in Java ?

Last few days I have been learning Java. When i used C#, String.Format({0}) was one of my favorites and I was wondering how to do this in Java.

String.format("some string here %s ", valuehere);

Thursday, January 06, 2011

How to fix Eazfuscator.NET unhandled exception error in WPF?

Today, I wanted to obfuscate one of the WPF applications that we are planing to deploy soon. While
googling I found this grate open source product Eazfuscator.NET and I downloaded the latest beat version and gave it a try.

After the obfuscating process completed I ran the .exe file to check whether everything is working as it suppose to but it didn't and I got this exception instead.

"Description: The process was terminated due to an unhandled exception. Exception Info: System.Windows.Markup.XamlParseException"

Now confused. I had no idea how to fix it ! googled it again and got nothing! So i decided to play around it and see.

1. Create a separate window. set it as the start-up Url in App.xaml file Eg,
StartupUri="DummyWindow.xaml"

2. If it's working, which it did for me.

3. Try looking at the MainWindow.xaml event bindings with the MainWindow.xaml.cs file.

most if the time, when it obfuscates, it will obfuscates events as well, If you have any events Like
Window_Loaded make sure they are not being obfuscated using the flag

[System.Reflection.Obfuscation(Exclude = true)]

Eg.

[System.Reflection.Obfuscation(Exclude = true)] private void Window_Loaded(object sender, RoutedEventArgs e) {

}


4. Open the VS 2010 editor, Change it to release mode and rebuild.

you should see a message like,

Obfuscating assembly 'xxxxx.exe'...

then run, You should get an error message if you have any problems starting up which should be easy to diagnose.

Hope this will help someone. Eazfuscator.NET is a grate free product and I hope it will stay that way !