Пример #1
0
        public static void Shutdown()
        {
            if (_dllHandle != IntPtr.Zero)
            {
                _shutdown?.Invoke();

                _shutdown               = null;
                _setTargetDevice        = null;
                _setLightingForScanCode = null;
                _setLightingForKeyName  = null;

                FreeLibrary(_dllHandle);
                _dllHandle = IntPtr.Zero;
            }
        }
Пример #2
0
        public static bool Init()
        {
            // Handle the case when the library is already loaded.
            if (_dllHandle != IntPtr.Zero)
            {
                return(true);
            }

            _dllHandle = LoadLibraryEx(DllPath, IntPtr.Zero, 0);
            if (_dllHandle == IntPtr.Zero)
            {
                return(false);
            }

            var initAddress = GetProcAddress(_dllHandle, "LogiLedInit");

            if (initAddress == IntPtr.Zero)
            {
                Shutdown();
                return(false);
            }

            var success =
                ((LogiLedInit)Marshal.GetDelegateForFunctionPointer(initAddress, typeof(LogiLedInit)))();

            if (!success)
            {
                Shutdown();
            }

            var shutdownAddress        = GetProcAddress(_dllHandle, "LogiLedShutdown");
            var setTargetDeviceAddress = GetProcAddress(_dllHandle, "LogiLedSetTargetDevice");
            var setLightingForKeyWithScanCodeAddress =
                GetProcAddress(_dllHandle, "LogiLedSetLightingForKeyWithScanCode");
            var setLightingForKeyWithKeyNameAddress =
                GetProcAddress(_dllHandle, "LogiLedSetLightingForKeyWithKeyName");

            foreach (var address in new[]
                     { shutdownAddress, setTargetDeviceAddress, setLightingForKeyWithScanCodeAddress, setLightingForKeyWithKeyNameAddress })
            {
                if (address == IntPtr.Zero)
                {
                    Shutdown();
                    return(false);
                }
            }

            _shutdown =
                (LogiLedShutdown)Marshal.GetDelegateForFunctionPointer(shutdownAddress,
                                                                       typeof(LogiLedShutdown));
            _setTargetDevice =
                (LogiLedSetTargetDevice)Marshal.GetDelegateForFunctionPointer(setTargetDeviceAddress,
                                                                              typeof(LogiLedSetTargetDevice));
            _setLightingForScanCode =
                (LogiLedSetLightingForKeyWithScanCode)Marshal.GetDelegateForFunctionPointer(
                    setLightingForKeyWithScanCodeAddress,
                    typeof(LogiLedSetLightingForKeyWithScanCode));
            _setLightingForKeyName =
                (LogiLedSetLightingForKeyWithKeyName)Marshal.GetDelegateForFunctionPointer(
                    setLightingForKeyWithKeyNameAddress,
                    typeof(LogiLedSetLightingForKeyWithKeyName));

            foreach (var methodDelegate in new object[]
                     { _shutdown, _setTargetDevice, _setLightingForScanCode })
            {
                if (methodDelegate == null)
                {
                    Shutdown();
                    return(false);
                }
            }

            return(success);
        }