08、electron调用C#类库的dll

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了08、electron调用C#类库的dll相关的知识,希望对你有一定的参考价值。

参考技术A 前面有详细的介绍,这里就不再赘述了。 Electron+Vue开发轻量级应用

跟调用c++的类库相比,简直不要太简单了。

1. node.js通过edge访问.net动态链接库

c# 调用c++类库控制usb继电器

网上找不到调用此类库的文章,简单写一下,以备后用。

下面是封装后的调用c++类库的类

    public class UsbRelayDeviceHelper
    {
        /// <summary>  
        /// Init the USB Relay Libary  
        /// </summary>  
        /// <returns>This function returns 0 on success and -1 on error.</returns>  
        [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_init", CallingConvention = CallingConvention.Cdecl)]
        public static extern int Init();

        /// <summary>  
        /// Finalize the USB Relay Libary.  
        /// This function frees all of the static data associated with  
        /// USB Relay Libary. It should be called at the end of execution to avoid  
        /// memory leaks.  
        /// </summary>  
        /// <returns>This function returns 0 on success and -1 on error.</returns>  
        [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_exit", CallingConvention = CallingConvention.Cdecl)]
        public static extern int Exit();

        /// <summary>  
        /// Enumerate the USB Relay Devices.  
        /// </summary>  
        /// <returns></returns>  
        [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_enumerate", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr usb_relay_device_enumerate();
        public static UsbRelayDeviceInfo Enumerate()
        {
            IntPtr x = UsbRelayDeviceHelper.usb_relay_device_enumerate();
            UsbRelayDeviceInfo a = (UsbRelayDeviceInfo)Marshal.PtrToStructure(x, typeof(UsbRelayDeviceInfo));
            return a;
        }

        /// <summary>  
        /// Free an enumeration Linked List  
        /// </summary>  
        /// <param name="deviceInfo"></param>  
        [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_free_enumerate", CallingConvention = CallingConvention.Cdecl)]
        public static extern void FreeEnumerate(UsbRelayDeviceInfo deviceInfo);

        /// <summary>  
        /// Open device that serial number is serialNumber  
        /// </summary>  
        /// <param name="serialNumber"></param>  
        /// <param name="stringLength"></param>  
        /// <returns>This funcation returns a valid handle to the device on success or NULL on failure.</returns>  
        [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open_with_serial_number", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int OpenWithSerialNumber([MarshalAs(UnmanagedType.LPStr)] string serialNumber, int stringLength);

        /// <summary>  
        /// Open a usb relay device  
        /// </summary>  
        /// <param name="deviceInfo"></param>  
        /// <returns>This funcation returns a valid handle to the device on success or NULL on failure.</returns>  
        [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open", CallingConvention = CallingConvention.Cdecl)]
        public static extern int Open(UsbRelayDeviceInfo deviceInfo);

        /// <summary>  
        /// Close a usb relay device  
        /// </summary>  
        /// <param name="hHandle"></param>  
        [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close", CallingConvention = CallingConvention.Cdecl)]
        public static extern void Close(int hHandle);

        /// <summary>  
        /// open a relay channel on the USB-Relay-Device  
        /// </summary>  
        /// <param name="hHandle">Which usb relay device your want to operate</param>  
        /// <param name="index">Which channel your want to open</param>  
        /// <returns>0 -- success; 1 -- error; 2 -- index is outnumber the number of the usb relay device</returns>  
        [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open_one_relay_channel", CallingConvention = CallingConvention.Cdecl)]
        public static extern int OpenOneRelayChannel(int hHandle, int index);

        /// <summary>  
        /// open all relay channel on the USB-Relay-Device  
        /// </summary>  
        /// <param name="hHandle">which usb relay device your want to operate</param>  
        /// <returns>0 -- success; 1 -- error</returns>  
        [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open_all_relay_channel", CallingConvention = CallingConvention.Cdecl)]
        public static extern int OpenAllRelayChannels(int hHandle);

        /// <summary>  
        /// close a relay channel on the USB-Relay-Device  
        /// </summary>  
        /// <param name="hHandle">which usb relay device your want to operate</param>  
        /// <param name="index">which channel your want to close</param>  
        /// <returns>0 -- success; 1 -- error; 2 -- index is outnumber the number of the usb relay device</returns>  
        [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close_one_relay_channel", CallingConvention = CallingConvention.Cdecl)]
        public static extern int CloseOneRelayChannel(int hHandle, int index);

        /// <summary>  
        /// close all relay channel on the USB-Relay-Device  
        /// </summary>  
        /// <param name="hHandle">hich usb relay device your want to operate</param>  
        /// <returns>0 -- success; 1 -- error</returns>  
        [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close_all_relay_channel", CallingConvention = CallingConvention.Cdecl)]
        public static extern int CloseAllRelayChannels(int hHandle);

        /// <summary>  
        /// status bit: High --> Low 0000 0000 0000 0000 0000 0000 0000 0000, one bit indicate a relay status.  
        /// the lowest bit 0 indicate relay one status, 1 -- means open status, 0 -- means closed status.  
        /// bit 0/1/2/3/4/5/6/7/8 indicate relay 1/2/3/4/5/6/7/8 status  
        /// </summary>  
        /// <param name="hHandle"></param>  
        /// <param name="status"></param>  
        /// <returns>0 -- success; 1 -- error</returns>  
        [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_get_status", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetStatus(int hHandle, ref int status);
    }

    /// <summary>  
    /// USB relay board info structure  
    /// </summary>  
    [StructLayout(LayoutKind.Sequential, Pack = 8)]
    public class UsbRelayDeviceInfo
    {
        [MarshalAs(UnmanagedType.LPStr)]
        public string SerialNumber;

        public IntPtr DevicePath { get; set; }

        public UsbRelayDeviceType Type { get; set; }

        public IntPtr Next { get; set; }
    }

    public enum UsbRelayDeviceType
    {
        OneChannel = 1,
        TwoChannel = 2,
        FourChannel = 4,
        EightChannel = 8
    }

查找并打开控制器

            info=UsbRelayDeviceHelper.Enumerate();
            if (info == null){
                MessageBox.Show("没有找到控制器");
            }
            else { state = UsbRelayDeviceHelper.Open(info); }

打开第一路继电器,第二个参数表示第几路

 UsbRelayDeviceHelper.OpenOneRelayChannel(state, 1);

关闭第一路继电器,第二个参数表示第几路

UsbRelayDeviceHelper.CloseOneRelayChannel(state, 1);

以上是关于08、electron调用C#类库的dll的主要内容,如果未能解决你的问题,请参考以下文章

c# 调用c++类库控制usb继电器

C#/.NET第六期01C#基础_01:运行环境新建C#程序的流程严格区分大小写类库的含义

C#调用DLL问题

C#/.NET第六期01C#基础_01:运行环境新建C#程序的流程严格区分大小写类库的含义

C# Telnet 类库代码,谨献给还在寻找C# Telnet类库的兄弟

c#怎样创建类库