1

I am trying to replicate functionality of a C++ library in C# and one of its features is that it can expose some of its functions to the outside.

Now when i call SetAllAnalog() I get the below mentioned exception. This is not really a Question how to fix it, because I could just wrap the internal functions in another function which I then export.

Although I would like to know why this is happening and if there might be a better way to fix this other than the already mentioned fix.

I have following code:

    [DllExport("OutputAllAnalog", CallingConvention = CallingConvention.StdCall)]
    public static void OutputAllAnalog(int Data1, int Data2)
    {
        if (!_k8055D.Connected || Data1 < 0 || 255 < Data1 || 
                                 Data2 < 0 || 255 < Data2) return;

        _k8055D.AnalogOutputChannel[0] = (double)Data1 / 255 * 5;
        _k8055D.AnalogOutputChannel[1] = (double)Data2 / 255 * 5;
    }

    [DllExport("SetAllAnalog", CallingConvention = CallingConvention.StdCall)]
    public static void SetAllAnalog()
    {
        OutputAllAnalog(255, 255); //exception
        test(); //No exception
    }

    public static void test()
    {

    }

Exception:

An unhandled exception of type 'System.MissingMethodException' occurred in K8055Test.exe

Additional information: Method not found: 'Void K8055Simulation.K8055.OutputAllAnalog(Int32, Int32)'.
Encore
  • 275
  • 2
  • 12

1 Answers1

1

Did you try using different names for the function in the DllExport attribute ExportName parameter vs. the actual name of the function? All the examples I saw were done this way, eg:

[DllExport("OutputAllAnalog", CallingConvention = CallingConvention.StdCall)]
public static void OutputAllAnalogImplementation(int Data1, int Data2)
{
    if (!_k8055D.Connected || Data1 < 0 || 255 < Data1 || 
                             Data2 < 0 || 255 < Data2) return;

    _k8055D.AnalogOutputChannel[0] = (double)Data1 / 255 * 5;
    _k8055D.AnalogOutputChannel[1] = (double)Data2 / 255 * 5;
}

[DllExport("SetAllAnalog", CallingConvention = CallingConvention.StdCall)]
public static void SetAllAnalog()
{
    OutputAllAnalogImplementation(255, 255); //Fixed exception??
    test(); //No exception
}

public static void test()
{

}

This question documents an error caused by similar method names 6 years ago.

This answer links to a Codeplex article explaining the behind-the-scenes wizardry handled by the UnmanagedExports Nuget package.

This answer provides a number of additional references on this topic.

Community
  • 1
  • 1
user423430
  • 3,654
  • 3
  • 26
  • 22
  • Haven't tried yet, but your answer makes a lot of sense, I ended up just going the easy route and having separate methods which export the functionality and call the internal ones. – Encore Dec 19 '16 at 12:14