C# DLL Crashes when Power is Removed from Opal Kelly Front Panel

The version I am using is: Opal Kelly Board API Version 5.0.2
I am using the following two DLL’s: libFrontPanel-csharp.dll and libFrontPanel-pinv.dll
My project builds, runs and powers on. But if I try to power cycle the power supply that is powering the board the entire app crashes. The power supply was tested without the opal kelly board attached and it power cycles fine without crashing the app. However, with that test we did have a acroname hub hooked up (with devices attached) to test it, and when those devices disconnect the app stays running. Calling close, dispose on the it does not seem to help. I have placed try catches around calls, check for error codes (including NoError) but it still crashes.

The only error catching I can get is from the Windows Event Viewer which has the following information in it:

Faulting application name: OurAPP.exe, version: 1.9.0.5, time stamp: 0x61672732
Faulting module name: libFrontPanel-pinv.DLL, version: 5.0.2.0, time stamp: 0x5b56182a
Exception code: 0xc000041d
Fault offset: 0x000000000003f1cf
Faulting process id: 0xfec
Faulting application start time: 0x01d8ee01e3012e7a
Faulting application path: C:\OUR APP PATH
Faulting module path: C:\OURAPP\OURAPPFOLDER\Libraries\Managed\x64\OpalKelly\libFrontPanel-pinv.DLL
Report Id: d0e60634-923c-4015-bc99-b4b27dfc2a9b
Faulting package full name: 
Faulting package-relative application ID:

OS Name|Microsoft Windows 10 Enterprise|
Version|10.0.19044 Build 19044|
System Type x64-based PC

Side note: I was wondering why in the documentation it mentions a Monitor class but this is unavailable to C# it seems it wasn’t brought over is this correct?

I also did search for similar issues but none them helped me since they were issues like not having both dlls in place, or in the right spot. (I would upload an image but I can’t as I am newb).
I did read this one: Front Panel DLL Crash and others.

Thank you for reaching out to the Opal Kelly forums and welcome!

I notice you have FrontPanel 5.0.2 installed. The newest version is 5.2.11. Could you install the newest version of FrontPanel from Opal Kelly Pins and retest?

Hi hayden,
Yes, I am aware it is not the newest build but I will have to get permission to do this because we are required to stay in sync with production, but I should be able to test in a solo environment maybe. My lead will have to evaluate risk vs reward for updating all of our systems.

Thank you for the reply(and promptness).

So, I finally managed to try this, but I am still having issues.
The first attempt I tried adding it directly to my project and just replace the old version DLL’s, that failed with PINVOKE error. The second attempt I made an empty project and the only thing it is a call to the opal kelly dll. Which I have placed the files in the following locations.

  1. C:\throwAway\opkel_throwaway\ThrowAwayConsoleApp1\bin\x64\Debug
  2. C:\throwAway\opkel_throwaway\ThrowAwayConsoleApp1\bin\Debug
    I get the following error:
    System.TypeInitializationException: ‘The type initializer for ‘OpalKelly.FrontPanel.libFrontPanelPINVOKE’ threw an exception.’
    DllNotFoundException: Unable to load DLL ‘libFrontPanel-pinv’: The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Inner exception is in this image.

Here is another image of the throwAway project:

using System;
using OpalKelly.FrontPanel;

namespace ThrowAwayConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var device = new okCFrontPanelDevices();
            if (device.GetCount() > 0)
            {
                Console.WriteLine("Found Device");
            }
            else
            {
                Console.WriteLine("NO Device Found");
            }

            Console.ReadKey();
        }
    }
}

I am using the 64 bit DLL version and I am building to 64 bit on my 64 bit machine.

The okFrontPanel.dll and libFrontPanel-pinv.dll files need to be in the runtime directory of your application (C:\throwAway\opkel_throwaway\ThrowAwayConsoleApp1\bin\x64\Debug). I was able to recreate your error message using the DESTester Sample provided in the FrontPanel install. I added the csharp library as a reference but did not add the okFrontPanel.dll and libFrontPanel-pinv.dll files to the \x64\Debug directory and received the same error.

Resources: Programming Languages - Opal Kelly Documentation Portal

Yeah, as I said above I added them to those directories specified in the api. So, you are able to reproduce my problem by leaving those files out of those directories, but what happens when you copy them into it? Does it run and work? Because for me no matter where I place them it does not work. It says its missing a manifest if I try and load the dll manually.

I also tried registering into the GAC using the util and it fails as well but I am not the most experienced with GACUTIL.

gacutil /i 'C:\Program Files\Opal Kelly\FrontPanelUSB\API\Csharp\x64\libFrontPanel-pinv.dll'
Microsoft (R) .NET Global Assembly Cache Utility.  Version 4.0.30319.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Failure adding assembly to the cache:   The module was expected to contain an assembly manifest.

I just tried the DESTester Project copied the dll’s over into the debug folder and get the same error. Also, I have added the DLLs to a folder then added to the SYSTEM PATH ENVIR variable.

I believe you are missing okFrontPanel.dll in the runtime directory (this is mentioned in the C# section of the Programming Languages link I posted earlier).

Resources: Programming Languages - Opal Kelly Documentation Portal

I will try again, and I will revist the docs to see if I am just messing something up. Thank you hayden for helping me. I will get a more senior dev to help me if I cannot get it. Thanks again.