K8055 Exception - C#/.Net

We’re using the K8055 board to programatically pause a printer. Everything is working wonderfully (great product)! However, the one issue that I have is when I close my application (C# - Visual Studio 2008 Professional) while running inside of Visual Studio I get two error dialogs. The first one says:

The exception unknown software exception (0x0eedfade) occurred in the application at location 0x7c812a6b.

I click Okay on that and I get a second error dialog:

Runtime error 217 at 00009C34.

If I run my app outside of VS it runs fine with no exception. I’ve played around with the DLL imports to see if that made any difference and it does not. Although I could deploy the app since it runs fine outside of VS I’m concerned that I’m creating a resource leak. Here’s a class that I developed for the board:

Never mind…I answered my own question. I was using the K8055.dll when I should have been using the K8055_c.dll. Here’s the corrected class that now does not error when the application quits in VS:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace Label_Applicator_Manager
{

    public enum Address : int
    {
        Address0 = 0,
        Address1 = 1,
        Address2 = 2,
        Address3 = 3
    }

    public enum Channel : int
    {
        Channel1 = 1,
        Channel2 = 2,
        Channel3 = 3,
        Channel4 = 4,
        Channel5 = 5
    }

    public enum Counter : int
    {
        Counter1 = 1,
        Counter2 = 2
    }

    public class Velleman_K8055 : IDisposable
    {
        [System.Runtime.InteropServices.DllImport("K8055D_C", CallingConvention = CallingConvention.StdCall )]
        public static extern unsafe int OpenDevice(int address);
        [System.Runtime.InteropServices.DllImport("K8055D_C", CallingConvention = CallingConvention.StdCall)]
        public static extern unsafe void CloseDevice();
        [System.Runtime.InteropServices.DllImport("K8055D_C", CallingConvention = CallingConvention.StdCall)]
        public static extern unsafe void WriteAllDigital(int data);
        [System.Runtime.InteropServices.DllImport("K8055D_C", CallingConvention = CallingConvention.StdCall)]
        public static extern unsafe void ClearDigitalChannel(int channel);
        [System.Runtime.InteropServices.DllImport("K8055D_C", CallingConvention = CallingConvention.StdCall)]
        public static extern unsafe void ClearAllDigital();
        [System.Runtime.InteropServices.DllImport("K8055D_C", CallingConvention = CallingConvention.StdCall)]
        public static extern unsafe void SetDigitalChannel(int channel);
        [System.Runtime.InteropServices.DllImport("K8055D_C", CallingConvention = CallingConvention.StdCall)]
        public static extern unsafe void SetallDigitial();
        [System.Runtime.InteropServices.DllImport("K8055D_C", CallingConvention = CallingConvention.StdCall)]
        public static extern unsafe bool ReadDigitalChannel(int channel);
        [System.Runtime.InteropServices.DllImport("K8055D_C", CallingConvention = CallingConvention.StdCall)]
        public static extern unsafe int ReadAllDigital();
        [System.Runtime.InteropServices.DllImport("K8055D_C", CallingConvention = CallingConvention.StdCall)]
        public static extern unsafe void ResetCounter(int counter);
        [System.Runtime.InteropServices.DllImport("K8055D_C", CallingConvention = CallingConvention.StdCall)]
        public static extern unsafe int ReadCounter(int counter);
        [System.Runtime.InteropServices.DllImport("K8055D_C", CallingConvention = CallingConvention.StdCall)]
        public static extern unsafe void SetCounterDebounceTime(int counter, int time);

        private Address boardAddress;
        private bool disposed = false;


        public Velleman_K8055(Address _address)
        {
            boardAddress = _address;
            int addr = (int)boardAddress;

            // Attempt to open the board at the specified address
            if (OpenDevice(addr) != (int)boardAddress)
            {
                throw new Exception("Could not open interface board at address " + boardAddress.ToString());
            }

        }

        public void Close()
        {
            CloseDevice();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!disposed)
            {
                Close();
                disposed = true;
            }
        }

        public bool Read(Channel channel)
        {
            return ReadDigitalChannel((int)channel);
        }

        public void CounterReset(Counter counter)
        {
            ResetCounter((int)counter);
        }

        public int CounterRead(Counter counter)
        {
            return ReadCounter((int)counter);
        }

        public void SetCDT(Counter counter, int time)
        {
            SetCounterDebounceTime((int)counter, time);
        }


    }
}

Strange that you got the error messages when K8055D.DLL was used.
I have used this K8055D.DLL version 3.0.2.0 without any problems in this tiny C# test project:
box.net/shared/zpn4j0xh7c

Yes, very strange indeed! I downloaded your test project and it does not crash on my system when closing. I copied you DLLImport statements into my project and it crashes with those as well. I went back to using K8055d_c.dll and all is fine.

One difference (that may or may not be a factor) is I’m using a BackgroundWorker to handle polling of the board. I then use a timer to check the value polled from the board in my form. I even tried using just a single timer to check the poll the board/check the value but still crashed on exit. And I’m polling the board at 250ms intervals, slower than the 100ms that you have in your test project.

I also wonder if it has something to do with where I placed the DLLImport in my class.

I’d be happy to send you the project to test on your system.

Just to check the DLL version:
In my project the K8055D.DLL is located in the folder: \K8055_tst\bin\x86\Debug
Is your application using the DLL in the System32 folder? Is it version 3.0.2.0. ?

It would be interesting to check your code…
You can email it to me to: (email address removed)

Yes, I placed the dll in the system32 folder. I’ll email the project to you soon.

… and the version is 3.0.2.0 ?

I changed your program to use the K8055D.DLL.
Everything seems to work fine!
No error messages displayed - strange…!

One difference found:
You are using C# - Visual Studio 2008 Professional.
I’m using Visual C# 2008 Express edition…

Bingo! Somehow I had a K8055d.dll from 2003. Must have gotten it from an old example project. I replaced it with version 3.0.2.0 and whaddayaknow…it’s not crashing anymore on close!

Thanks for the fast response! Issue resolved…

Nice that it is now solved :slight_smile:
The DLL version was the issue!

I’ll continue checking your interesting software operation…

Know more about…C# Exception Handling

Mitch