Home > Blockchain >  PInvoke C dll from C# throws Exception
PInvoke C dll from C# throws Exception

Time:11-08

The project I am working on has a case where I have to read and display hardware info, for which they have a function written in a C DLL, and I am writing a C# stub to PInvoke it. I am fairly new to PInvoke, and hold beginner status in C# and C in general, as I mostly work in the Java space.

The issue here is that I am getting an exception, I feel like there is some marshaling issue going on here, which is causing this exception, but please correct me if I am wrong. Can you please help me spot the issue?

Also, I did try combining multiple MarshalAs options and different data types. In addition to the complication as it is, the function takes in a reference to a struct as an argument, and the struct itself has a nested struct within. Also, it returns a long as a flag I believe, when called with no arguments returns 0 with no exception, which is interesting.

===========================================================================================================
Part of sample hardware operation C   dll header. 
===========================================================================================================
#define APOINTER *
typedef unsigned char BYTETYPE;
typedef BYTETYPE  CHARTYPE;
typedef unsigned long int ULONGTYPE;
typedef HDINFO APOINTER PTR_HDINFO;

typedef struct VERSION {
    BYTETYPE major;
    BYTETYPE minor;
}VERSION;

typedef VERSION APOINTER PTR_VERSION;

typedef struct HDINFO {
    VERSION hardwareVersion;
    CHARTYPE hardwareManufactureID[32];         /* blank padded */
    ULONGTYPE hardwareflags;                     /* must be zero */
    CHARTYPE hardwareDesc[32];     /* blank padded */
    VERSION apiVersion;
}HDINFO;

typedef HDINFO APOINTER PTR_HDINFO;

extern "C" __declspec(dllexport) ULONGTYPE GetHardwareInfo(PTR_HDINFO pSoInfo);
===========================================================================================================
Sample C# code for PInvoke. Throws Exception
===========================================================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;


namespace ConsoleHardwareApp
{

    [StructLayout(LayoutKind.Sequential, Size =1)]
    struct VERSION
    {
        byte majorVersion;  
        byte minorVerison;  
    }

    [StructLayout(LayoutKind.Sequential, Size =1)]
    struct HDINFO
    {
        VERSION hardwareVersion;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        string hardwareManufactureID;
        int hardwareflags;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        string hardwareDesc;
        VERSION apiVersion; 
    }
    class Program
    {
        [DllImport("sampleHardwareOp.dll")]
        public static extern int GetHardwareInfo(ref IntPtr hdInfoPtr);
        
        static void Main(string[] args)
        { 
            HDINFO hdInfo = new HDINFO();
            IntPtr hdInfoPtr = new IntPtr();
            hdInfoPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HDINFO)));
            Marshal.StructureToPtr<HDINFO>(hdInfo, hdInfoPtr, false);
            int rv = 1;
            rv = GetHardwareInfo(ref hdInfoPtr);    //Exception thrown here
            Console.WriteLine(rv); 
            Console.ReadKey();
        }
    }
}

The exception:

ConsoleHardwareApp.Program::GetHardwareInfo' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

CodePudding user response:

Without concise details about which C compiler was used to make the DLL, what size its long data type is (may be 32bit or 64bit), what alignment settings are used, if any structure padding is present, etc, then a translation to C# is difficult. However, your C# code should probably look something more like the following instead:

namespace ConsoleHardwareApp
{
    [StructLayout(LayoutKind.Sequential/*, Pack=4 or 8*/)]
    struct VERSION
    {
        byte major;
        byte minor;
    }

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi/*, Pack=4 or 8*/)]
    struct HDINFO
    {
        VERSION hardwareVersion;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
        string hardwareManufactureID;
        uint hardwareflags; // or ulong, depending on the C   compiler used
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
        string hardwareDesc;
        VERSION apiVersion;
    }

    class Program
    {
        [DllImport("sampleHardwareOp.dll", CallingConvention=CallingConvention.Cdecl)]
        public static extern uint/*or: ulong*/ GetHardwareInfo(ref HDINFO pSoInfo);

        static void Main(string[] args)
        { 
            HDINFO hdInfo = new HDINFO();
            uint rv = GetHardwareInfo(ref hdInfo);
            Console.WriteLine(rv); 
            Console.ReadKey();
        }
    }
}

The CallingConvention in particular is very important. The default calling convention in C is usually __cdecl (but can be changed in compiler settings), however in C# the default CallingConvention in DllImport is CallingConvention.Winapi, which is __stdcall on Windows. A calling convention mismatch can easily corrupt the call stack, causing the error you are seeing.

  • Related