Saturday, September 24, 2011

Creating a network drive with Net use Command in Windows 7.



In this article I would discuss the problem with respect to creating a network drive  for a share in Windows 7 by using the NET USE Command . The problem was that when  I  tried to create a network drive in Windows Xp  using net use . The command  worked absolutely fine .It also showed me a network drive in the Windows Explorer.

But with Windows 7 the command completed successfully but surprisingly the drive was not visible in  Windows Explorer. Let me show  you the steps which I followed
 I used an Administrator command prompt and  typed the following command
   NET USE Z:  \\mycomputer\sharetest
  
The command  completed  successfully  but there wasn’t  a network drive visible in the Windows Explorer.
I  searched  a number forums  and tried several workarrounds and finally got a crude method to get out of it. Many of forums pointed to UAC settings of Windows 7.

Note:  The solution provided below can have side effects on the working of Windows and also may lead to security  implications .The author is not responsible. You would always do it at your own risk.

Following  are the steps followed to solve the problem.

1.     Goto  Start --> run Type msconfig and  press enter you get the tabbed screen shown below



2.     Goto Tools tab-> Select Change UAC settings  and then click on Launch. You would get the screen shown below



3.     Bring down the slider to Never notify.



4.     Click on Ok
5.     Restart  your Computer
6.     Run the command  prompt as Administrator  and the command       

    NET USE Z:  \\mycomputer\sharetest.
7.     Finally you can see the network drive visible in Windows Explorer


Sunday, September 18, 2011

Handling Cross Thread operations in Windows Forms




There are many scenerios in day to day development activities and if you are working with thread you always encounter a problem for cross thread operation.In other words you are not able to update properties of a control from some other thread . Here I have tried to depict a simple scenario of updating a progressbar as a problem.


The Code written below show a problem of cross thread operation.
In the code below I have a created a separate thread and implemented a event mechanism to update the progress bar value from a thread that doesnot own the progressbar.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Threading;

namespace CrossThreadOperation
{
    public partial class Form1 : Form
    {

        /// <summary>
        /// delegate for callback implementation
        /// </summary>
        /// <param name="value">data from the calling thread</param>
        public delegate void CompletedDelegate(int value);

        /// <summary>
        /// event used for notification by the thread
        /// </summary>
        public event CompletedDelegate Completed;
        public Form1()
        {
            InitializeComponent();
          
            // hooking the callback function
            this.Completed += new CompletedDelegate(Form1_Completed);
        }

        void Form1_Completed(int value)
        {
            progressBar1.Value = value ;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread workerThread = new Thread(DoSomeWork);
            workerThread.Start(); 
        }

        private void DoSomeWork()
        {
            ArrayList arr = new ArrayList();

            for (int index = 65; index < 95; index++)
            {
                arr.Add(index);
            }

            Completed(100);

        }
    }
}

When I try to run this code in Debug mode I get an exception as shown below





 To overcome the problem we have to follow the steps mentioned below
1.   Create a delegate with the same signature as the function that updates a control
2.   Create a function that updates the control.
3.   Instantiated the delegate with the function which updates the control
4.   Check whether the control is updated by the main thread or a separate thread by checking the Invoke Required property of the control. Here the separate thread requests the main thread to update the property of the control.
5.   If the control is being updated from a separate thread then update the control from the delegate
6.   If the control is being updated directly from the main thread then directly update the property of the control.


        /// <summary>
        /// delegate for setting the progressBar
        /// </summary>
        /// <param name="value"></param>
        public delegate void SetProgessBarDelegate(int value);

    
        void Form1_Completed(int value)
        {

          //  progressBar1.Value = value ;

            UpdateProgress(value );

        }

      
        private void DoSomeWork()
        {
            ArrayList arr = new ArrayList();

            for (int index = 65; index < 95; index++)
            {
                arr.Add(index);
            }

            Completed(100);

        }


        /// <summary>
        /// function to avoid the exception due to Cross-Thread Operation
        /// </summary>
        /// <param name="value"></param>
        public void UpdateProgress(int value)
        {
            //intantiate the delegate for thread safe action of
            // of updating the progressbar
            SetProgessBarDelegate setProgess = new SetProgessBarDelegate(UpdateProgress);
          
             //if the value is not updated from the main Thread.
            if (progressBar1.InvokeRequired)
            {
                //you can check the thread id using a message box here to verify it yourself
                //here the worker thread requests the main thread
                // to increment the progress bar
                progressBar1.Invoke(setProgess , new object[] { value });

            }
            else
            {
                //The main thread increments the progressbar
                progressBar1.Value =value ;
            }
        }

            }
        }
   


Finally we observe that the progress bar increments smoothly

Saturday, September 10, 2011

Deploying a JAVA application with Wix Tool Set

As I was trying to build a installer for java application using Wix that could detect JRE Installation and install only if JRE was present. I stumbled across a number of forums and blogs.Finally I found a good solution. The key steps involved were 
 1.Setting the working directory for a java application in the start menu shortcut properties
 2. Check the registry for existence of specific JRE versions .


The highlighted Grey text in the mark up shown below emphasises the same


<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
      <Product Id="07f0003f-4290-4f4c-9442-c2d203f64b4f" Name="TestJar " Language="1033" Version="1.0.0.0" Manufacturer="abc" UpgradeCode="00e83a73-37c7-4ec0-88f4-9612dcb05a0d">
            <Package InstallerVersion="200" Compressed="yes" />
            <Media Id="1" Cabinet="media1.cab" EmbedCab="yes" />
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder"  Name="ProgramFilesFolder">
        <Directory Id="INSTALLLOCATION" Name="TestJar">
          <Component Id="component1" Guid="323CBA27-E825-4b15-B297-ABBEA9B686DA">
            <File Id='Test.jar' Name='Test.jar'  DiskId='1'                                      Source="E:\JavaProj\Bin\Test.jar" Vital='yes'/>
            <File Id='Start.bat' Name='Start.bat'  DiskId='1'
                   Source="E:\JavaProj\Bin\Start.bat" Vital='yes'/>
          </Component>
</Directory>   
      <Directory Id="StartMenuFolder" >
        <Directory Id ="ProgramMenuFolder">
          <Directory Id="TestJar" Name ="TestJar">
            <Component Id ="component3" Guid ="92B0EDF4-664E-4707-AD32-066DD3993A57"  >
              <Registry Root="HKCU" Key ="Software\[Manufacturer]\[ProductName]" Value ="[INSTALLLOCATION]" Type ="string" KeyPath ="yes" />
              <Shortcut Id="MyTestShortCutA"  Directory ="TestJar "  Name="testjar "   Icon ="icon1" Show="normal" WorkingDirectory="INSTALLLOCATION"  Target ="[!start.bat]">

            <Icon Id="icon1" SourceFile="E:\JavaProj\Bin\images\User1.ico" />
              </Shortcut>
            </Component >
          </Directory >
        </Directory>
      </Directory>
    </Directory >
    </Directory >
        <Property Id="JAVA_CURRENT_VERSION">
      <RegistrySearch Id="JRE_KEY" Root="HKLM" Key="SOFTWARE\JavaSoft\Java Runtime Environment" Name="EVersion" Type="raw"  />
    </Property>
    <Condition Message="Java not installed  Please install JRE 1.6 or later."><![CDATA[(Installed OR JAVA_CURRENT_VERSION) AND JAVA_CURRENT_VERSION >= "1.6"]]></Condition>      
    <Feature Id='Complete' Level='1' ConfigurableDirectory ='INSTALLLOCATION'>
      <ComponentRef Id='component1' />
      <ComponentRef Id='component3' />
    </Feature >
     <Property Id="WIXUI_INSTALLDIR" Value="INSTALLLOCATION" ></Property>
    <WixVariable Id="WixUIBannerBmp" Value="E:\JavaProj\Bin\images\Test.bmp" />
    <WixVariable Id="WixUIDialogBmp" Value="E:\JavaProj\Bin\images\blank.bmp" />
    <UIRef Id="WixUI_InstallDir" /> 
      </Product>
</Wix>


Note:Registry search keys may change depending on different JRE Versions.