Archive for the ‘.Net’ Category

SafeArrayTypeMismatchException

Tuesday, August 11th, 2009

We extended our legacy C++ DCOM application last week, and when the developer wrote the C#.Net end to call the new method, we were getting a  System.Runtime.InteropServices.SafeArrayTypeMismatchException.

The developer that had added the method stated it work, and pointed to his Delphi test app that worked happily.

Reviewing the method code it all looked fine. Single stepping through the code there where no problems.

The help for this exception says:

The exception thrown when the type of the incoming SAFEARRAY does not match the type specified in the managed signature.

But as we are dynamically calling the method like this:

objAddType = Type.GetTypeFromProgID("DCOM_OBJECT_NAME.COMPANY_CLASS_A");
objAdd = Activator.CreateInstance(objAddType);

object[] input = {};

object result = objAddType.InvokeMember("MethodName", BindingFlags.InvokeMethod, null, objAdd, input);

There was no was no signature.

This is for this Method:

VARIANT CompanyClassAObject::MethodName(void)
{
    VARIANT vaResult;
    VariantInit(&vaResult);

    SAFEARRAY* sfa = GetSafeArray();

    vaResult.vt = VT_UI1 | VT_ARRAY;
    vaResult.parray = sfa;

    return vaResult;
}

with the body of GetSafeArray looked like:

    SAFEARRAYBOUND sfabounds[1];
    sfabounds[0].lLbound = 0;
    sfabounds[0].cElements = bytes_needed;
    SAFEARRAY *sfa = SafeArrayCreate(VT_I1, 1, sfabounds);
    if(sfa)
    {
        void *data;
        if(SafeArrayAccessData(sfa, &data) == S_OK)
        {
            memcpy(data, buffer, bytes_needed);
            SafeArrayUnaccessData(sfa);

            delete buffer;
            return sfa;
        }
    }

The problem ended up being that the SafeArray is created as VT_I1 type but when it is put into the variant type it was typed as VT_UI1.  So the .Net runtime was picking up the conflict and correctly complaining, and once you know what the error is, the exception message make sense.  Funny that!

Setting the SafeArrayCreate to use VT_UI1 and every thing worked.

Creating Palette based GIFs

Monday, January 26th, 2009

A question was asked on the mailing list (ages ago) about creating 8-bit GIF files.  I proved this code for simple palette based GIFs, so here it is:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace Project
{
    class ProjectMain
    {
        public static void Main()
        {
            GenerateBackgroundImage(Color.AliceBlue, Color.Beige, Color.Cyan, 10, 10, 10, @"c:\image.gif");
        }

        public static void GenerateBackgroundImage(Color leftColour, Color centerColour,
            Color rightColour, int leftWidth, int centerWidth, int rightWidth, string path)
        {

            int width = leftWidth + centerWidth + rightWidth + 2;
            int height = 10;

            using (Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed))
            {
                Color backColour = Color.White;
                byte idxBackground = 0;
                byte idxLeft = 1;
                byte idxCenter = 2;
                byte idxRight = 3;

                ColorPalette cp = bitmap.Palette;
                cp.Entries[idxBackground] = backColour;
                cp.Entries[idxLeft] = leftColour;
                cp.Entries[idxCenter] = centerColour;
                cp.Entries[idxRight] = rightColour;

                bitmap.Palette = cp;

                fillrect(idxBackground, bitmap, new Rectangle(0, 0, width, height));
                fillrect(idxLeft, bitmap, new Rectangle(0, 0, leftWidth, height));
                fillrect(idxCenter, bitmap, new Rectangle(leftWidth + 1, 0, centerWidth, height));
                fillrect(idxRight, bitmap, new Rectangle(leftWidth + 2 + centerWidth, 0, rightWidth, height));

                bitmap.Save(path, ImageFormat.Gif);
            }
        }

        static void fillrect(byte colorIndex, Bitmap b, Rectangle r)
        {
            BitmapData bmpData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.WriteOnly, b.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            // This code is specific to a bitmap with 8 bits index per pixels.
            int bytes = b.Width * b.Height;
            byte[] Values = new byte[bytes];

            // Copy the RGB values into the array.
            Marshal.Copy(ptr, Values, 0, bytes);

            for (int y = r.Top; y < r.Bottom; y++)
            {
                for (int x = r.Left; x < r.Right; x++)
                {
                    Values[(y * b.Width) + x] = colorIndex;
                }
            }

            // Copy the values back to the bitmap
            Marshal.Copy(Values, 0, ptr, bytes);

            // Unlock the bits.
            b.UnlockBits(bmpData);
        }
    }
}

I would change fillrect, to seperate out the locking, and Marshal calls, to CreateTempBuffer, FillTempBuffer, and WriteTempBuffer functions, if you were to use fillrect more than a couple times.  But as it stands, the code is correct after each call.

Loving the Visual Studio 2008 compiler

Friday, December 5th, 2008

I have been finding lambda and extension methods really helpful in my game port.

Blobs of C styled single linked list code, when changed to generic lists boil down to one line.

like this:

Item item = player.itemsPtr;
while (item != null)
{
    Item next_item = item.next;

    if (item_type == item.type)
    {
        lose_item(item, player); // just removes from linked list.
    }
    item = next_item;
}

to this:

player.items.RemoveAll(item => item.type == item_type);

much nicer.

Microsoft UNPLUGGED – WPF & VS2008

Sunday, November 16th, 2008

I attended the November Unplugged WPF & VS2008 developer session on Tuesday.

JD’s talk was well balanced, and covered the basics of what WPF is, how the UI and UX work.  He avoided only showing the shiny demo screens and dug deeper into Command and RoutedEvents and how the UI and UX are decoupled.

WPF is so big, and a bit of a mind shift from VB6 drag and drop that I’m not sure there is much more that can be demoed to a general audience.

Kirk’s Visual Studio 2008 talk was fantastic.  Lots of keyboard short-cuts for tasks I’m currently doing via the  mouse, so I’m very pleased to learn the key combo’s.

I’m torn between Crtl + . and F8 being my absolute take-away gems, both will be used heavily.  Nice to see a talk about using the tools better.

There were some good give-away’s, an office 2007 license, a wireless mouse and a 22″ wide screen panel.  Of which I didn’t win any.

This has made me more keen to port CotAB to Silverlight, just for the experience of it.

Before the event I attended a MS feedback discussion group.  So funny to see who of the .Net CHCH scene turned-up.  It was good to talk about issues, impressions, and opinions.  I hope Microsoft guys got the value they were hoping to get.

Cedric’s Coding challenge

Wednesday, July 2nd, 2008

I noticed Cedric’s Coding challenge while reading Robert Fisher’s blog.

Here is an interesting coding challenge: write a counter function that counts from 1 to max but only returns numbers whose digits don’t repeat.

For example, part of the output would be:

  • 8, 9, 10, 12 (11 is not valid)
  • 98, 102, 103 (99, 100 and 101 are not valid)
  • 5432, 5436, 5437 (5433, 5434 and 5435 are not valid)

Also:

  • Display the biggest jump (in the sequences above, it’s 4: 98 -> 102)
  • Display the total count of numbers
  • Give these two values for max=10000

Because I love coding problems, I whipped up a C# 2.0 solution which is not as succinct as some of the functional solutions, but it works on a ‘fast enough for me’ time scale.

using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace ConsoleApplication1
{
    class Program
    {
        static bool[] used = new bool[10];
        static int value = 0;
        static void Main(string[] args)
{
            int gap = 0;
            int last = 0;
            int count = 0;
            Stopwatch timer = new Stopwatch();

            timer.Start();
            int mag = 1;
for (int i = 1; i < 10; i++)
{
                foreach (int x in Step(1, mag))
                {
                    gap = Math.Max(gap, x - last);
                    last = x;
                    count++;
                }

                mag *= 10;
            }
            timer.Stop();
            Console.WriteLine("Count: {0}", count);
Console.WriteLine("Gap: {0}", gap);
            Console.WriteLine("Time: {0}", timer.Elapsed);
            Console.ReadKey();
        }
        static IEnumerable<int> Step(int start, int mag)
        {
            for (int i = start; i < 10; i++)
            {
                if (used[i] == false)
                {
                    if (mag == 1)
                    {
                        yield return value + i;
                    }
                    else
                    {
                       used[i] = true;
                       value += mag * i;

                       foreach (int x in Step(0, mag / 10))
                       {
                           yield return x;
                       }

                       used[i] = false;
                       value -= mag * i;
                   }
               }
           }
       }
    }
}

Giving the following output (for 1 – MaxIn, as compared to 1-10,000 in the original challenge)

Count: 5611770
Gap: 10469135
Time: 00:00:02.8350765