Archive for the ‘Windows development’ Category.

Microsoft .NET Framework - Windows-Based Client Development

After 1 month of preparation and a weekend that disappeared I have passed my second MCTS exam. I did use the “MCTS Self-Paced Training Kit (Exam 70-526): Microsoft® .NET Framework 2.0 Windows®-Based Client Development” book for my preparation and Measureup’s online test questions. I did also try ActualTest.com’s question that I did borrow from a friend. I found that the questions many times were almost exactly the same (Found some question with exactly the same options). So I can’t recommend ActualTests.com. Measureup did cover all the areas good except the custom control integration to VS design toolbox and properties dialog. Many questions on the real exam were about this functionality that I had not read about in the book or had any questions about in the practice online tests. So for that area I would recommend to read MSDN.

My study strategy was more or less as last MCTS exam. I did read 1 chapter in the book per day/ every second day (15 chapters in total). I also did plan to do the labs in the book but I never got the time for it. Instead I did concentrate on the lesson and chapter review questions plus the summary in the end of every part of the book. The last weekend that totally disappeared from my life, I did go through all lessons and chapter reviews plus summaries. I also did use the Measureup’s web site very heavily. Went through all 150 questions and read all explanations to all questions (this gave me the most). Another good part with the web questions is that it contain link to relevant MSDN sites for more information.

Measureup
MCTS Self-Paced Training Kit (Exam 70-526): Microsoft® .NET Framework 2.0 Windows®-Based Client Development book

Using an enum type with WCF services

I recently started to work with Windows Communication Foundation and one of the first problems I did run into was how to use enum type in WCF.

This is a small example of a data contract and a service contract/interface that makes use of the enum type.

using System;
using System.ServiceModel;
using System.Runtime.Serialization;
 
namespace Test.DataContracts
{
    [DataContract(Namespace = "Testing", Name = "JobResult")]
    public enum JobResult
    {
        [EnumMember]
        Passed = 0,
        [EnumMember]
        Failed = 1,
        [EnumMember]
        Running = 2,
        [EnumMember]
        TestFailure = 3,
        [EnumMember]
        Queued = 4,
        [EnumMember]
        JobNotFound = 5
    }
}

Continue reading ‘Using an enum type with WCF services’ »

UI automation with accessibility object and win 32 API

In my earlier blogs (Fun with UIAutomation and calc, Fun with UIAutomation and calc 2 (event handling)) I have been talking about how to user uiautomation in .NET. Now it time to dig down bellow uiautomation and go directly to the source WIN 32 API. I have tried to translate the first calculator project to use win 32 API and accessibility object instead. The first thing that we need to-do is to create DllImport statement for user32.dll used for handling more or less all windows UI and oleacc.dll used for accessing the accessibility object. You will find my complete solution in the end of the blog.

An example of DllImport is EnumChildWindows that gives use the possibility to get all child windows to a specific window.

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);

Continue reading ‘UI automation with accessibility object and win 32 API’ »

Working with windows firewall from code

Quite recently I did some automation for an installation program that was changing the firewalls rules. So to be able to test that the installation was working as expected I need a way to access information about the firewall from code and also a way to change the information to verify that the installer was able to repair the rules. I found a lot of smaller code snippets mostly in VB on the net with random quality. Since I’m big fan of C# I did decide to create my one snippet’s in C#

The basic functionality is found in the COM object hnetcfg.dll. So the first thing to-do is to get a firewall manager.

private const string CLSIDFireWallManager = "{304CE942-6E39-40D8-943A-B913C40C9CD4}";
private static NetFwTypeLib.INetFwMgr GetFirewallManager()
{
    Type objectType = Type.GetTypeFromCLSID(new Guid(CLSIDFireWallManager));
    INetFwMgr manager = Activator.CreateInstance(objectType) as NetFwTypeLib.INetFwMgr;
    if (manager == null)
    {
        throw new NotSupportedException("Could not load firewall manager");
    }
 
    return manager;
}

Getting the current profile The profile is used for all firewall rules interactions.

private static INetFwProfile GetCurrentProfile()
{
    INetFwProfile profile;
    try
    {
        profile = GetFirewallManager().LocalPolicy.CurrentProfile;
    }
    catch (System.Runtime.InteropServices.COMException e)
    {
        throw new NotSupportedException("Could not get the current profile (COMException)", e);
    }
    catch (System.Runtime.InteropServices.InvalidComObjectException e)
    {
        throw new NotSupportedException("Could not get the current profile (InvalidComObjectException)", e);
    }
 
    return profile;
}

Is firewall on or of?

public static bool IsWindowsFirewallOn
{
    get
    {
        return GetCurrentProfile().FirewallEnabled;                
    }
 
    set
    {
        GetCurrentProfile().FirewallEnabled = value;
    }
}

Continue reading ‘Working with windows firewall from code’ »