Чтение C# dll из VB6 - PullRequest
       116

Чтение C# dll из VB6

0 голосов
/ 03 августа 2020

У меня есть библиотека классов C#, в которой указаны характеристики Bluetooth LE. Я хочу использовать эту DLL для получения значений чтения в моем приложении VB6. Моя проблема: мне нужен полный код, чтобы получить результат. Это означает, что мне нужно вызвать основную функцию из моей программы VB6, но я понятия не имею, какие параметры мне нужно использовать и как я могу получить правильное возвращаемое значение. Моя Dll:

 using SDKTemplate;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using Windows.Storage.Streams;
using System.Threading.Tasks;
using System.Runtime.Remoting.Messaging;
using System.Runtime.InteropServices;
namespace BleSirLo
{
    public class Program
    {

        public byte[] ret = new byte[6];
        static void Main(string[] args)
        {
            Task t = MainAsync(args);
            t.Wait();
            // or, if you want to avoid exceptions being wrapped into AggregateException:
            //  MainAsync().GetAwaiter().GetResult();
        }

        static async Task MainAsync(string[] args)
        {
            Program p = new Program();
            p.StartBleDeviceWatcher();
        }
            
        private List<DeviceInformation> UnknownDevices = new List<DeviceInformation>();
        private List<DeviceInformation> _knownDevices = new List<DeviceInformation>();
        private IReadOnlyList<GattCharacteristic> characteristics;
        private IReadOnlyList<GattDeviceService> services;

        private GattDeviceService currentSelectedService = null;
        private GattCharacteristic currentSelectedCharacteristic = null;

        private DeviceWatcher deviceWatcher;

        public bool done = false;

        private void StartBleDeviceWatcher()
        {
            // Additional properties we would like about the device.
            // Property strings are documented here https://msdn.microsoft.com/en-us/library/windows/desktop/ff521659(v=vs.85).aspx
            string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected", "System.Devices.Aep.Bluetooth.Le.IsConnectable" };

            // BT_Code: Example showing paired and non-paired in a single query.
            string aqsAllBluetoothLEDevices = "(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")";

            deviceWatcher =
                    DeviceInformation.CreateWatcher(
                        aqsAllBluetoothLEDevices,
                        requestedProperties,
                        DeviceInformationKind.AssociationEndpoint);

            // Register event handlers before starting the watcher.
            deviceWatcher.Added += DeviceWatcher_Added;
            deviceWatcher.Updated += DeviceWatcher_Updated;
            deviceWatcher.Removed += DeviceWatcher_Removed;
            deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
            deviceWatcher.Stopped += DeviceWatcher_Stopped;

            // Start over with an empty collection.
            _knownDevices.Clear();
            //deviceWatcher.Stop();
            deviceWatcher.Start();
            while(true)
                if (done == true)
                    break;

        }

        private void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation deviceInfo)
        {
            //Debug.WriteLine(String.Format("Device Found!" + Environment.NewLine + "ID:{0}" + Environment.NewLine + "Name:{1}", deviceInfo.Id, deviceInfo.Name));

            //notify user for every device that is found
            if (sender == deviceWatcher)
            {
                if ((deviceInfo.Name == "Ei Gude, wie?") || (deviceInfo.Name == "Ei Gude, Wie?"))
                {
                    sender.Stop();
                    ConnectDevice(deviceInfo);
                }
            }
        }
        private void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfo)
        {
        }
        private void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfo)
        {
        }
        private void DeviceWatcher_EnumerationCompleted(DeviceWatcher sender, object args)
        {
        }
        private void DeviceWatcher_Stopped(DeviceWatcher sender, object args)
        {
        }

        //trigger StartBleDeviceWatcher() to start bluetoothLe Operation
        private void Scan()
        {
            //empty devices list
            _knownDevices.Clear();
            UnknownDevices.Clear();

            //finally, start scanning
            StartBleDeviceWatcher();
        }

        private async void ConnectDevice(DeviceInformation deviceInfo)
        {
            //get bluetooth device information
            BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(deviceInfo.Id);
            //Respond(bluetoothLeDevice.ConnectionStatus.ToString());

            //get its services
            GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync();

            //verify if getting success 
            if (result.Status == GattCommunicationStatus.Success)
            {
                //store device services to list
                services = result.Services;

                //loop each services in list
                foreach (var serv in services)
                {
                    //get serviceName by converting the service UUID
                    string ServiceName = Utilities.ConvertUuidToShortId(serv.Uuid).ToString();

                    //if current servicename matches the input service name
                    if (ServiceName == "65520") //ServiceTxtBox.Text)
                    {
                        //store the current service
                        currentSelectedService = serv;

                        //get the current service characteristics
                        GattCharacteristicsResult resultCharacterics = await serv.GetCharacteristicsAsync();

                        //verify if getting characteristics is success 
                        if (resultCharacterics.Status == GattCommunicationStatus.Success)
                        {
                            //store device services to list
                            characteristics = resultCharacterics.Characteristics;

                            //loop through its characteristics
                            foreach (var chara in characteristics)
                            {
                                //get CharacteristicName by converting the current characteristic UUID
                                string CharacteristicName = Utilities.ConvertUuidToShortId(chara.Uuid).ToString();

                                //if current CharacteristicName matches the input characteristic name
                                if (CharacteristicName == "65524")//CharacteristicsTxtBox.Text)
                                {
                                    //store the current characteristic
                                    currentSelectedCharacteristic = chara;
                                    //stop method execution
                                    readBuffer();
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }

        //function that handles the read button event
        private async void readBuffer()
        {
            {
                if (currentSelectedService != null && currentSelectedCharacteristic != null)
                {
                    GattCharacteristicProperties properties = currentSelectedCharacteristic.CharacteristicProperties;

                    //if selected characteristics has read property
                    if (properties.HasFlag(GattCharacteristicProperties.Read))
                    {
                        //read value asynchronously
                        GattReadResult result = await currentSelectedCharacteristic.ReadValueAsync();
                        if (result.Status == GattCommunicationStatus.Success)
                        {
                            int a, b, c, d, e, f;
                            var reader = DataReader.FromBuffer(result.Value);
                            //byte [] input = new byte[reader.UnconsumedBufferLength];
                            reader.ReadBytes(ret);
                            a = ret[0];
                            b = ret[1];
                            c = ret[2];
                            d = ret[3];
                            e = ret[4];
                            f = ret[5];
                            Ret_val(ret);
                        }

                    }
                }
            }
        }

        private void Response_TextChanged(object sender, EventArgs e)
        {

        }
 }

В моей программе VB6 я вызываю библиотеку следующим образом: (Очевидно, что она не работает, но я не знаю, как мне это делать.

    Dim WithEvents CSharpInteropServiceEvents As CSharpInteropService.LibraryInvoke
      Dim load As New LibraryInvokeparam(0) = Me.hwnd
Dim sa as variant
        Set CSharpInteropServiceEvents = load
        sa = load.GenericInvoke("C:\Program Files (x86)\Microsoft Visual Studio\VB98\WINCOM\BleSirLo.dll", "BleSirLo.Program", "Main", param)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...