Monday, July 23, 2012

WiX Custom Action - Copy MSI after Install

In WiX there is a functionality to execute application after successfully closing the setup in async mode.
Add following in the .wxs file and execute candle.exe with usual commands.

<Property Id="WixShellExecTarget" Value="[CommonAppDataFolder]Product_Data\test.exe" />
<CustomAction Id="LaunchApplication" BinaryKey="WixCA" DllEntry="WixShellExec" Impersonate="yes" />
<UI>
   <Publish Dialog="ExitDialog" Control="Finish" Event="DoAction" Value="LaunchApplication">NOT Installed</Publish>
</UI>

In command line compilation for Light.exe following "-ext" needed to add.
These extensions will allow use of WixUIExtension.dll and WixUtilExtension.dll.

light.exe d:\abc\test.wixobj -ext WixUIExtension -ext WixUtilExtension -out d:\abc\test.msi

This is the basic logic behind executing any exe after completing setup. This application executes in async mode and in my case I have used it to copy the present MSI from its present location to program data for safekeeping. This copy MSI is a simple implementation of copying file from one location to another hence not including that code here.

Reference
http://windows-installer-xml-wix-toolset.687559.n2.nabble.com/Why-doesn-t-this-work-Opening-PDF-at-Finish-button-click-td3087051.html

Sunday, July 15, 2012

Animate Form - Slowly Show and Close

This is something makes a simple Form more aesthetically rich. Form cosmetic aspects plays major part but launch of a form also plays a major aspects how user perceive it. I was looking ways to do that, one way is to use timer to change the opacity of form during show and hide. Other way and better way to animate it. User need to use user32.dll for it.

Implementation:

Add following reference
using System.Runtime.InteropServices;

Initialization - add following in FormClass

namespace TestNamespace
{
    public partial class TestForm : Form
    {
        [DllImport("user32.dll")]
        static extern bool AnimateWindow(IntPtr hWnd, int time, AnimateWindowFlags flags);
        [Flags]
        enum AnimateWindowFlags
        {
            AW_HOR_POSITIVE = 0x00000001,
            AW_HOR_NEGATIVE = 0x00000002,
            AW_VER_POSITIVE = 0x00000004,
            AW_VER_NEGATIVE = 0x00000008,
            AW_CENTER = 0x00000010,
            AW_HIDE = 0x00010000,
            AW_ACTIVATE = 0x00020000,
            AW_SLIDE = 0x00040000,
            AW_BLEND = 0x00080000
        }
    }
}

Need to handle two form events "XXX_Load" and "XXX_FormClosing" and add following code in that

private void TestForm_Load(object sender, EventArgs e)
{
   AnimateWindow(this.Handle, 1000, AnimateWindowFlags.AW_BLEND | AnimateWindowFlags.AW_ACTIVATE);
}

private void TestForm_FormClosing(object sender, FormClosingEventArgs e)
{
   AnimateWindow(this.Handle, 1000, AnimateWindowFlags.AW_BLEND | AnimateWindowFlags.AW_HIDE);
}

Reference :- http://subversion.assembla.com/svn/nex-framework/trunk/.NEX%20framework/WindowsAPI/user32.cs

Saturday, July 7, 2012

Simple Singleton Object

This is a simple Singleton class created which I have serialized using separate serialized class and saved in a text file in encrypted form. The main reason of having separate Singleton Class object and Serialized Class object is to maintain its basic integrity and keep maintainable easy.

Reference - http://msdn.microsoft.com/en-us/library/ff650316.aspx

To See how to serialize in encrypted form please refer to my previous implementation (link below).
Serialize singleton object to file - encrypted

Implementation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace TestNamespace
{
    public class SingletonClass
    {
        string abc;
        
        // This is the crux of singleton implementation
        private static SingletonClass instance;
        private SingletonClass(){}
        public static SingletonClass Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SingletonClass();
                }
                return instance;
            }
        }

        // standard get and set methods
        public Version ABC
        {
            get { return abc; }
            set { abc = value; }
        }
    }
}

Thursday, July 5, 2012

Serialize Class Object using DES algorithm

There is a specific requirement I have in my present implementation. Need to maintain a data store in the application in which some of the data needed to be stored and fetched whenever application launched again. To achieve created class in Singleton and made it serialized. Some of the objects in the class are not serialized.
I will be posting the implementation of Singleton class object later. Following is the implementation where class object has been serialized and de-serialized. I also have a requirement to encrypt the data.

To achieve this used DESCryptoServiceProvider and CryptoStream.
"TestStoreObj" is the class object which is in Singleton and I am serializing it here.

Note :- You can use Triple DES for it but it will impact performance due to multiple iteration of encryption.

Implementation:

Referred following
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;

Initialization
DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
IFormatter formatter = new BinaryFormatter();
Stream stream = null;

Serialize
stream = new FileStream("MyConfig.txt", FileMode.Create, FileAccess.Write, FileShare.None);
using (CryptoStream cs = new CryptoStream(stream, new DESCryptoServiceProvider().CreateEncryptor(cryptic.Key, cryptic.IV), CryptoStreamMode.Write))
{
    formatter.Serialize(cs, TestStoreObj);
    cs.FlushFinalBlock();
}
stream.Close();

Deserialize
stream = new FileStream("MyConfig.txt", FileMode.Open, FileAccess.Read, FileShare.None);
using (CryptoStream cs = new CryptoStream(stream, new DESCryptoServiceProvider().CreateEncryptor(cryptic.Key, cryptic.IV), CryptoStreamMode.Read))
{
    TestStoreObj = (TestSingletonClass)formatter.Deserialize(cs);
}
stream.Close();

Reference:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.des.aspx