Question

I'm currently trying to use the logitech sdk for my G19.

All the informations i could find on this topic dated from 2012 and many methods changed name, i decided to try to make a new .NET Wrapper.

But but i'm stuck and not getting anywhere.

I first created a library project. Here is the library code :

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Logitech_LCD
{


    /// <summary>
    /// Class containing necessary informations and calls to the Logitech SDK
    /// </summary>
    public class NativeMethods
    {
        #region Enumerations
        /// <summary>
        /// LCD Types
        /// </summary>
        public enum LcdType
        {
            Mono = 1,
            Color = 2,
        }

        /// <summary>
        /// Screen buttons
        /// </summary>
        [Flags]
        public enum Buttons
        {
            MonoButton0 = 0x1,
            ManoButton1 = 0x2,
            MonoButton2 = 0x4,
            MonoButton3 = 0x8,
            ColorLeft = 0x100,
            ColorRight = 0x200,
            ColorOK = 0x400,
            ColorCancel = 0x800,
            ColorUp = 0x1000,
            ColorDown = 0x2000,
            ColorMenu = 0x4000,
        }
        #endregion

        #region Dll Mapping
        [DllImport("LogitechLcd.dll", CallingConvention = CallingConvention.Cdecl))]
        public static extern bool LogiLcdInit(String friendlyName, LcdType lcdType);

        [DllImport("LogitechLcd.dll", CallingConvention = CallingConvention.Cdecl))]
        public static extern bool LogiLcdIsConnected(LcdType lcdType);
        #endregion
    }
}

Then, in a dummy app, i tried to call LogiLcdInit :

Console.WriteLine(Logitech_LCD.NativeMethods.LogiLcdIsConnected(Logitech_LCD.NativeMethods.LcdType.Color));
Console.WriteLine(Logitech_LCD.NativeMethods.LogiLcdInit("test", Logitech_LCD.NativeMethods.LcdType.Color));
Console.WriteLine(Logitech_LCD.NativeMethods.LogiLcdIsConnected(Logitech_LCD.NativeMethods.LcdType.Color));

Now the problem is : for each of these lines, i'm getting a PInvokeStackImbalance exception. No more details except the method name.

Here is a link to the Logitech SDK for reference

Edit : changed the code to reflect the codes changes due to the answers

EDIT 2

Here is the .NET wrapper i made thanks to your answers : https://github.com/sidewinder94/Logitech-LCD

Just placing it here to be used as a reference.

Was it helpful?

Solution

That's because the DllImport attribute defaults to the stdcall calling convention, but the Logitech SDK uses the cdecl calling convention.

Additionally, bool in C++ takes only 1 byte, when the C# runtime is trying to unmarshal 4 bytes. You must tell the runtime to marshal bool as 1 byte instead of 4 bytes using another attribute.

So your imports end up looking like this:

[DllImport("LogitechLcd.dll", CallingConvention=CallingConvention.Cdecl)]
[return:MarshalAs(UnmanagedType.I1)]
public static extern bool LogiLcdInit(String friendlyName, LcdType lcdType);

[DllImport("LogitechLcd.dll", CallingConvention=CallingConvention.Cdecl)]
[return:MarshalAs(UnmanagedType.I1)]
public static extern bool LogiLcdIsConnected(LcdType lcdType);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top