罗技SDK c#用法

本文关键字:用法 SDK 罗技 | 更新日期: 2023-09-27 17:53:44

我目前正在尝试使用罗技sdk为我的G19。

我能找到的关于这个主题的所有信息都是从2012年开始的,许多方法都改了名字,我决定尝试做一个新的。net Wrapper。

但是但是我被困住了,哪儿也去不了。

我首先创建了一个库项目。下面是库代码:

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
    }
}

然后,在一个虚拟应用程序中,我试图调用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));

现在的问题是:对于每一行,我得到一个pinvokestack失衡异常。除了方法名之外,没有其他详细信息。

这里是一个链接到罗技SDK供参考

Edit:更改代码以反映由于答案而导致的代码更改

编辑2

这是我做的。net包装感谢你的回答:https://github.com/sidewinder94/Logitech-LCD

把它放在这里作为参考

罗技SDK c#用法

这是因为DllImport属性默认为stdcall调用约定,但罗技SDK使用cdecl调用约定。

另外,c++中的bool只占用1个字节,而c#运行时试图解封送4个字节。您必须告诉运行时使用另一个属性将bool封送为1字节而不是4字节。

所以你的导入最终看起来像这样:

[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);