Where to put WndProc


I’m trying to detect if a USB will be connected.
Normal code for this is:

  Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = &H219 Then
               'code to use
            End If
        End Sub

But I can’t put it in the plugin where I also put the On_Load function


Inherits System.Windows.Forms.Form

is needed for this.

is there a work around for this? Or should I use this somewhere else?

Found a little work around…

Create a splashscreen then call it On_Load plugin:

Dim splash As New SplashScreen1
splash.Enabled = False

then in the splash screen On_Load me.hide

and put:

 Private Const WM_DEVICECHANGE As Integer = &H219
    Private Const DBT_DEVICEARRIVAL As Integer = 32768
    Private Const DBT_DEVICEREMOVECOMPLETE As Integer = 32772

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_DEVICECHANGE Then
        End If
    End Sub

in the splashscreen code.

Is there a better way to keep track of this?

In general, these types of “things” are handled in a message-only window. This can be a form that is invisible or a class that inherits from NativeWindow.

– Dale

Oke. I got a invisible form now but I think a NativeWindow is better.

I found this:

' NativeWindow class to listen to operating system messages. 
Friend Class MyNativeWindowListener
    Inherits NativeWindow

    ' Constant value was found in the "windows.h" header file. 
    Private Const WM_ACTIVATEAPP As Integer = &H1C

    Private parent As Form1

    Public Sub New(ByVal parent As Form1)

        AddHandler parent.HandleCreated, AddressOf Me.OnHandleCreated
        AddHandler parent.HandleDestroyed, AddressOf Me.OnHandleDestroyed
        Me.parent = parent
    End Sub 

    ' Listen for the control's window creation and hook into it.     
    Private Sub OnHandleCreated(ByVal sender As Object, ByVal e As EventArgs)
        ' Window is now created, assign handle to NativeWindow.
        AssignHandle(CType(sender, Form).Handle)
    End Sub 

    Private Sub OnHandleDestroyed(ByVal sender As Object, ByVal e As EventArgs)
        ' Window was destroyed, release hook.
    End Sub

    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
        Protected Overrides Sub WndProc(ByRef m As Message)
        ' Listen for operating system messages 

        Select Case (m.Msg)
            Case WM_ACTIVATEAPP

                ' Notify the form that this message was received. 
                ' Application is activated or deactivated,  
                ' based upon the WParam parameter.
                parent.ApplicationActivated(m.WParam.ToInt32() <> 0)

        End Select 

    End Sub 
End Class

Do I just put it under the Plugin Class or do I need to call it somewhere?

Since I don’t really know what you are doing, I’m not sure what to tell you. But you might consider storing one of these on your plug-in object. Then, create it when your plug-in loads and destroy it when you plug-in unloads…

I have a security file so my plugin will work when the usb is connected. Too prevent people from removing the usb and putting it in another pc so they can use the software mulitple times. I want to keep track if they remove the usb. Thats why I need the WndProc.

I’ll try when I got the time :slight_smile:

Doesn’t a plugin not only unloads when rhino is exited? And when Rhino Exits the plugin will be “disposed”? So there is no need to destroy it?