none
Help with handles from C# to VB RRS feed

  • Question

  • Hi, I'm trying to translate the C# code below to VB, but I am having some trouble, particularly with the "OnHandleCreated()" routine. I'm unsure of the correct syntax and where to put it. It should register my application with the system handler for the USB port via a dll for UsbLibrary. I've been struggling with this in various guises for a few days, and I'm sure it is simple but I'm a bit of a N00b and am struggling. 

    I have a main window called "MainWindow.xaml" which I want to use as the base window, but I guess this should go in the Application.xaml.vb code?

    Anyway, really appreciate any help with this!

    James

    C# code: from https://www.bashewa.com/wmr200-usb-dll.php

    using UsbLibrary;
    
       usb = new UsbLibrary.UsbHidPort();
    
       usb.ProductId = 51713;
       usb.VendorId  = 4062;
       usb.OnSpecifiedDeviceArrived += new System.EventHandler(usb_OnSpecifiedDeviceArrived);
       usb.OnSpecifiedDeviceRemoved += new System.EventHandler(usb_OnSpecifiedDeviceRemoved);
       usb.OnDeviceArrived          += new System.EventHandler(usb_OnDeviceArrived);
       usb.OnDeviceRemoved          += new System.EventHandler(usb_OnDeviceRemoved);
       usb.OnDataSend               += new System.EventHandler(usb_OnDataSend);
       usb.OnDataReceived           += new UsbLibrary.DataReceivedEventHandler(usb_OnDataReceived);
    
    
       protected override void OnHandleCreated(EventArgs e)
       {
           base.OnHandleCreated(e);
           usb.RegisterHandle(Handle);
       }
    
    
       protected override void WndProc(ref Message m)
       {
           usb.ParseMessages(ref m);
           base.WndProc(ref m); // pass message on to base form
       }
    
    
       private void cmd_connect()
       {
           try
           {
               usb.CheckDevicePresent();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
       }
    
    
       private void usb_OnDeviceArrived(object sender, EventArgs e)
       {
           msg("Found a Device");
       }
    
    
       private void usb_OnDeviceRemoved(object sender, EventArgs e)
       {
           if (InvokeRequired)
           // Gets a value indicating whether the caller must call an invoke method when        
           // making method calls to the control because the caller is on a different thread    
           // than the one the control was created on.                                          
           // Returns: true if the control's System.Windows.Forms.Control.Handle was created on 
           //          a different thread than the calling thread (indicating that you must make
           //          calls to the control through an invoke method); otherwise, false.        
           {
               Invoke(new EventHandler(usb_OnDeviceRemoved), new object[] { sender, e });
           }
           else
           {
               msg("Device was removed");
           }
       }
    
    
       private void usb_OnSpecifiedDeviceArrived(object sender, EventArgs e)
       {
           msg( "WMR200 device was found: "
              + (usb.SpecifiedDevice.OutputReportLength)
              + " bytes per USB frame");
           cmd_reset();
           cmd_sendD0();
       }
    
    
       private void usb_OnSpecifiedDeviceRemoved(object sender, EventArgs e)
       {
           if (InvokeRequired)
           {
               Invoke(new EventHandler(usb_OnSpecifiedDeviceRemoved), new object[] { sender, e });
           }
           else
           {
               msg("WMR200 was unplugged");
           }
       }
    
    
       private void usb_OnDataSend(object sender, EventArgs e)
       {
           // Nothing, or do something before the data gets send
       }
    
    
       private void send_command(string data)
       {
           try
           {
               if (usb.SpecifiedDevice != null)
               {
                   // as for C# code snipped at: WMR200 Protocol
                   usb.SpecifiedDevice.SendData(data);
               }
               else
               {
                   MessageBox.Show("Sorry but the WMR200 is not present. Plug it in!! ");
               }
    
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
       }
    
    
       private void usb_OnDataReceived(object sender, DataReceivedEventArgs args)
       {
           if (InvokeRequired)
           {
               try
               {
                   Invoke(new DataReceivedEventHandler(usb_OnDataReceived), new object[] { sender, args });
               }
               catch (Exception ex)
               {
                   Console.WriteLine(ex.ToString());
               }
           }
           else
           {
               // as for C# code snipped at: WMR200 Protocol
           }
       }

    Tuesday, May 22, 2018 1:34 AM

Answers

  • Thanks for the reply Cherry (and appologies if this is just an Uber N00b question) but where would I put that code? In the MainPage.xaml.vb, Application.xmal.vb or somewhere else? If I place it in either of the other two it throws a gazzilion errors I am not sure how to interpret. Appreciate your time and help with this. I am learning!

    There are two things:

    First, that is code for Windows Forms applications, not WPF.  So you've got the wrong type of project to use this code sample as-is.

    Second, you need to have that UsbLibrary DLL (documented at the top of the page you linked) referenced in your project.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by Cor Ligthert Tuesday, May 22, 2018 7:20 PM
    • Marked as answer by Opplevelse Wednesday, May 23, 2018 2:57 AM
    Tuesday, May 22, 2018 11:07 AM
    Moderator

All replies

  • Hi Opplevelse,

    Like this?

     Private Sub Frmaccess_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       
            usb = New UsbLibrary.UsbHidPort()
            usb.ProductId = 51713
            usb.VendorId = 4062
            AddHandler usb.OnSpecifiedDeviceArrived, AddressOf usb_OnSpecifiedDeviceArrived
            AddHandler usb.OnSpecifiedDeviceRemoved, AddressOf usb_OnSpecifiedDeviceRemoved
            AddHandler usb.OnDeviceArrived, AddressOf usb_OnDeviceArrived
            AddHandler usb.OnDeviceRemoved, AddressOf usb_OnDeviceRemoved
            AddHandler usb.OnDataSend, AddressOf usb_OnDataSend
            AddHandler usb.OnDataReceived, AddressOf UsbLibrary.DataReceivedEventHandler()
        End Sub
     Protected Overrides Sub OnHandleCreated(ByVal e As EventArgs)
                MyBase.OnHandleCreated(e)
                usb.RegisterHandle(Handle)
            End Sub
    
            Protected Overrides Sub WndProc(ByRef m As Message)
                usb.ParseMessages(m)
                MyBase.WndProc(m)
            End Sub
    
            Private Sub cmd_connect()
                Try
                    usb.CheckDevicePresent()
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try
            End Sub
    
            Private Sub usb_OnDeviceArrived(ByVal sender As Object, ByVal e As EventArgs)
                msg("Found a Device")
            End Sub
    
            Private Sub usb_OnDeviceRemoved(ByVal sender As Object, ByVal e As EventArgs)
                If InvokeRequired Then
                    Invoke(New EventHandler(AddressOf usb_OnDeviceRemoved), New Object() {sender, e})
                Else
                    msg("Device was removed")
                End If
            End Sub
    
            Private Sub usb_OnSpecifiedDeviceArrived(ByVal sender As Object, ByVal e As EventArgs)
                msg("WMR200 device was found: " & (usb.SpecifiedDevice.OutputReportLength) & " bytes per USB frame")
                cmd_reset()
                cmd_sendD0()
            End Sub
    
            Private Sub usb_OnSpecifiedDeviceRemoved(ByVal sender As Object, ByVal e As EventArgs)
                If InvokeRequired Then
                    Invoke(New EventHandler(AddressOf usb_OnSpecifiedDeviceRemoved), New Object() {sender, e})
                Else
                    msg("WMR200 was unplugged")
                End If
            End Sub
    
            Private Sub usb_OnDataSend(ByVal sender As Object, ByVal e As EventArgs)
            End Sub
    
            Private Sub send_command(ByVal data As String)
                Try
    
                    If usb.SpecifiedDevice IsNot Nothing Then
                        usb.SpecifiedDevice.SendData(data)
                    Else
                        MessageBox.Show("Sorry but the WMR200 is not present. Plug it in!! ")
                    End If
    
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try
            End Sub
    
            Private Sub usb_OnDataReceived(ByVal sender As Object, ByVal args As DataReceivedEventArgs)
                If InvokeRequired Then
    
                    Try
                        Invoke(New DataReceivedEventHandler(AddressOf usb_OnDataReceived), New Object() {sender, args})
                    Catch ex As Exception
                        Console.WriteLine(ex.ToString())
                    End Try
                Else
                End If
            End Sub
    

    I use code converter tool to do this:

    http://converter.telerik.com/?utm_medium=product&utm_source=converter&utm_campaign=verb

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 22, 2018 6:06 AM
    Moderator
  • Thanks for the reply Cherry (and appologies if this is just an Uber N00b question) but where would I put that code? In the MainPage.xaml.vb, Application.xmal.vb or somewhere else? If I place it in either of the other two it throws a gazzilion errors I am not sure how to interpret. Appreciate your time and help with this. I am learning!
    Tuesday, May 22, 2018 6:37 AM
  • Thanks for the reply Cherry (and appologies if this is just an Uber N00b question) but where would I put that code? In the MainPage.xaml.vb, Application.xmal.vb or somewhere else? If I place it in either of the other two it throws a gazzilion errors I am not sure how to interpret. Appreciate your time and help with this. I am learning!

    There are two things:

    First, that is code for Windows Forms applications, not WPF.  So you've got the wrong type of project to use this code sample as-is.

    Second, you need to have that UsbLibrary DLL (documented at the top of the page you linked) referenced in your project.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by Cor Ligthert Tuesday, May 22, 2018 7:20 PM
    • Marked as answer by Opplevelse Wednesday, May 23, 2018 2:57 AM
    Tuesday, May 22, 2018 11:07 AM
    Moderator
  • Reed,

    I  always had replied before your reply "MainPage.xaml.vb" but I had no time to make a full sentence at that moment. 

    So when I saw your message I thought, why is Reed writing this. 

    Luckily I saw the  MessageBox.Show(ex.ToString()) without an import so it is no Library, no WPF, no Service, no ASP.Net,

    Therefore only forms fit.

    Good catch

    :-)


    Success
    Cor



    Tuesday, May 22, 2018 7:24 PM
  • EventArgs was the other clue... WPF uses RoutedEventArgs.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 22, 2018 8:56 PM
    Moderator
  • Thanks for the reply Cherry (and appologies if this is just an Uber N00b question) but where would I put that code? In the MainPage.xaml.vb, Application.xmal.vb or somewhere else? If I place it in either of the other two it throws a gazzilion errors I am not sure how to interpret. Appreciate your time and help with this. I am learning!

    There are two things:

    First, that is code for Windows Forms applications, not WPF.  So you've got the wrong type of project to use this code sample as-is.

    Second, you need to have that UsbLibrary DLL (documented at the top of the page you linked) referenced in your project.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Brilliant!! Thank you so much! Fixed the problem, I would never have figured that out myself. Very much appreciated! :)

    -- VB.net N00b but learning!

    Wednesday, May 23, 2018 2:58 AM