Archive for the ‘Programming’ Category.

Extract OcxState data

At work we have recently spent effort to remove our technical debt. So far we have introduced Unicode into the native code, migrated our VB6 to VB.NET, upgraded to .NET 4.0 and now we are working on 64bit support.

However, we have used the Microsoft FlexGrid quite extensively in our VB6 (now VB.NET) components and it does not support 64bit, therefore we decided to build a compatible control which wraps the existing WinForms DataGridView control.

We discovered quite early on that the properties set through the designer of an ActiveX control does not generate designer code, but instead it is serialized as a resource and restored through the OcxState property.

Always being the one to reduce the workload I devised a way of extracting this information so that we can redo the designer set properties. I found out that I could give the control another OcxState from another ResX and I could just look at the changed properties.

In the project (download below) I created a form with the desired control on it, I hacked around with the designer generated code to allow me to override the OcxState.

private readonly AxHost.State state;

public DummyForm()
    : this(null)
{
}

public DummyForm(AxHost.State state)
{
    this.state = state;
    InitializeComponent();
}

//
// axMSFlexGrid1
//
this.axMSFlexGrid1.Location = new System.Drawing.Point(69, 75);
this.axMSFlexGrid1.Name = "axMSFlexGrid1";
if (state == null)
    this.axMSFlexGrid1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMSFlexGrid1.OcxState")));
else
    this.axMSFlexGrid1.OcxState = state;

I used the ResXResourceReader and the ResourceSet classes to get the OcxState from my desired ResX source (the project that has the state in it I want to know). Then using reflection I compared the default state of the control (using the OcxState that the designer created) to the state I desired (with my own OcxState extracted from another ResX) and I now know what properties were changed on the designer.

In this project I have referenced the Microsoft FlexGrid but in theory it could be any ActiveX control.

Download Project

AssocQueryString (ANSI version) does not work in XP or Vista

This is going back awhile but colleague and I had a hell of a time recently trying to solve a problem where the Windows API method “AssocQueryString” did not seem to work in XP.

After creating a test application in various different configurations and testing it on several platforms we found that the ANSI version of AssocQueryString just did not work.

It was not until this evidence came to light that we finally found a connect article (in the depths of my browser history) describing this issue.

Our codebase might be quite rare (it is still currently 100% ANSI) and I would expect many to already be using Unicode… the workaround was to use it in Unicode and use conversions to interact with the rest of the codebase.

So yeah… hopefully others might find this elightening and save hours of hair pulling.

How to invoke a method on a COM object in C#

Where I work we often need to invoke methods on a COM object that has been developed in VB6 or in .NET. I wrote this helper class for speed it along.

public static class ActiveX
{
    public static object Invoke(string @namespace, string @class, string method, params object[] parameters)
    {
        var progId = string.Format("{0}.{1}", @namespace, @class);
        return Invoke(progId, method, parameters);
    }

    public static object Invoke(string progId, string method, params object[] parameters)
    {
        var type = Type.GetTypeFromProgID(progId);
        var comObject = Activator.CreateInstance(type);

        return type.InvokeMember(method, BindingFlags.InvokeMethod, null, comObject, parameters);
    }
}

Generic attributes would be awesome

Generic attributes would be awesome

Composite WPF Presentation Model item template

Neat little template I designed for use at work which is an improvement over the ViewModel code snippet.

Download PresentationModel item template

How to use

It is a bit unusual the way I have written this. Instead of specifying the file name for the item, it is the view name that is to be used.

When prompted for the ViewName, specifying “MainWindow” will create:

A xaml user control called MainWindowView.xaml.

An interface called IMainWindowView.

An interface called IMainWindowViewModel.

A class called MainWindowViewModel.

(Some of this is very similar to the ViewModel code snippet)

Interface Properties

Basically rip-offs of the prop and propg code snippets you get with Visual Studio but without the scope or accessor keywords.

iprop

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>iprop</Title>
			<Shortcut>iprop</Shortcut>
			<Description>Code snippet for an automatically implemented property on an interface</Description>
			<Author>Alex Boyne-Aitken</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>type</ID>
					<ToolTip>Property type</ToolTip>
					<Default>int</Default>
				</Literal>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp"><![CDATA[$type$ $property$ { get; set; }$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

ipropg

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>ipropg</Title>
			<Shortcut>ipropg</Shortcut>
			<Description>Code snippet for an automatically implemented property with a 'get' accessor for interfaces</Description>
			<Author>Alex Boyne-Aitken</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>type</ID>
					<ToolTip>Property type</ToolTip>
					<Default>int</Default>
				</Literal>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp"><![CDATA[$type$ $property$ { get; }$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

How to learn Programming

Read a fantastic article on Life Hacker on how to teach yourself computer programming: http://lifehacker.com/5401954/programmer-101-teach-yourself-how-to-code

My only comment would be pick a language that is based on C-like syntax (rather than use Python or VB) because although the syntax may seem harder, the syntax is more transferrable to other languages and it would give you a head start.

In my personal experience, a book only served as a reference. I found books that taught me how to do something soon became too basic for my needs and often there are plenty of guides on the internet that is a good replacement. Instead you will find much of my library consists of reference books, hint, tips and advanced topics.

CyTS library stalling

On our guild website, we have a block in the portal that shows who is currently on teamspeak. This uses the CyTS library to connect and get such information and occasionally it would cause the portal to stop loading.

In each case I would get home, load it up and not find any problems at all. Well today was a different matter, I was able to pin point the exact issue. For some reason the library connects to the server but then stalls when receiving information from the server.

I should tell you know, this is not a perfect fix but it does the job. I did look into other options like switch socket blocking on and off and such other approaches, but it seems that if fgets reads more than 1 byte, it stalls. This is a bug in PHP that has hung around for a while.

I have changed the _readcall function to read the first byte, check it for failure then continue with the rest.

function _readcall()
{
	if (!is_resource($this->sCon))
		return false;

	// HACK : Workaround to fail fast
	$first = fgets($this->sCon, 1);

	if($first === false)
		return false;

	$sRead = '';
	do
	{
		$cRead = $first.fgets($this->sCon);
		$first = '';

		$sRead .= $cRead;
	} while ($cRead != CYTS_SYN && $cRead != CYTS_OK && strtoupper(substr($cRead, 0, 5)) != "ERROR");
	return $sRead;
}

View model property

Because I am so lazy :P

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>View model property snippet</Title>
			<Shortcut>vmprop</Shortcut>
			<Description></Description>
			<Author>Alex Boyne-Aitken</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
        <Literal>
          <ID>name</ID>
          <ToolTip>Property name</ToolTip>
          <Default>Field</Default>
        </Literal>
        <Literal>
          <ID>field</ID>
          <ToolTip>Field name</ToolTip>
          <Default>field</Default>
        </Literal>
        <Literal>
          <ID>type</ID>
          <ToolTip>Type</ToolTip>
          <Default>object</Default>
        </Literal>
			</Declarations>
			<Code Language="csharp">
        <![CDATA[
    $type$ $field$;
    public $type$ $name$
    {
        get { return $field$; }
        set
        {
            if ($field$ == value)
                return;

            $field$ = value;
            SendPropertyChanged("$name$");
        }
    }
			$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

MSBuild creating lots of temp files

UPDATE:

I found a msdn forum post with exactly the same issue: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/42647eff-ecb8-412c-a884-a152b6fdd40d

It turns out that it is NOT msbuild but it is SN.exe leaving the temp files behind when resigning assemblies. I suppose you could easily come to this conclusion when watching the build and those files are flashing by so quickly.

On reflection a custom task will not help either…

This post has more details on the issue with a link to a hot fix for Server 2003: http://blogs.msdn.com/pfedev/archive/2008/10/24/sn-exe-and-empty-temp-files-in-temp.aspx

If you are using Server 2008 I would not recommend installing it.

Continue reading ‘MSBuild creating lots of temp files’ »