RegisterDeviceNotification Win32 Declare

Hi

I am trying to detect whenever Android device remove or insert. For RegisterDeviceNotification, this code work fine in VB.NET

Public Class DEV_BROADCAST_DEVICEINTERFACE
    Public dbcc_size As Integer
    Public dbcc_devicetype As Integer
    Public dbcc_reserved As Integer
    Public dbcc_classguid As Guid
    Public dbcc_name As Short
End Class
--------
_
Public Shared Function _
    RegisterDeviceNotification(ByVal IntPtr As IntPtr, ByVal NotificationFilter As IntPtr, ByVal Flags As Int32) As IntPtr
End Function
--------

Dim dbi As Win32.DEV_BROADCAST_DEVICEINTERFACE = New Win32.DEV_BROADCAST_DEVICEINTERFACE
Dim size As Integer
size = Marshal.SizeOf(dbi)

dbi.dbcc_size = size
dbi.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE

Dim Buffer As IntPtr
Buffer = Marshal.AllocHGlobal(size)
Marshal.StructureToPtr(dbi, Buffer, True)
Dim r As IntPtr
r = Win32.RegisterDeviceNotification(Handle, Buffer, Win32.DEVICE_NOTIFY_WINDOW_HANDLE)

But when I switch to Xojo, I couldn’t use some functions like Marshal.SizeOf(dbi)
or Marshal.StructureToPtr(dbi, Buffer, True).

How should I do to switch these functions?

Thanks.

Can anyone help me? :frowning:

You cannot use .NET code directly from Xojo. Here’s a recent thread where someone got it working. And the solution.

https://forum.xojo.com/14534-using-xojo-with-measurement-computing-s-universal-library
https://forum.xojo.com/23931-accessing-net-code-from-xojo-using-vb-is-possible

And what you are using isn’t a Win32 declare. It’s a .NET declare. Win32 declares are fully supported.

WFS might have something like this already defined

[quote=200244:@Tim Hare]You cannot use .NET code directly from Xojo. Here’s a recent thread where someone got it working. And the solution.

https://forum.xojo.com/14534-using-xojo-with-measurement-computing-s-universal-library
https://forum.xojo.com/23931-accessing-net-code-from-xojo-using-vb-is-possible[/quote]
Thanks for your help. But that is not really what I need. I try Declare RegisterDeviceNotification function like:

Soft Declare Function RegisterDeviceNotificationW Lib "user32" ( handler as Ptr, interfaceFilter As Ptr,  flags As Integer) As Ptr

I have trouble when changing a DEV_BROADCAST_DEVICEINTERFACE structure to interfaceFilter Ptr. That is my working:

Dim dbi As DEV_BROADCAST_DEVICEINTERFACE
  
Dim mb As New MemoryBlock(size)
Dim buffer As Ptr = mb
buffer.DEV_BROADCAST_DEVICEINTERFACE = dbi
  
Dim result As Ptr = RegisterDeviceNotificationW(AddressOf WndProc, buffer, DEVICE_NOTIFY_WINDOW_HANDLE )

Result is Nil so it’s not working. Could you give me some instruction?

I’m new in Xojo and Windows developer. So it is really hard to study :frowning: Thanks for your help.

I try to seek in WFS but nothing is same what I need.

Try

Dim dbi As DEV_BROADCAST_DEVICEINTERFACE
  
Dim mb As New MemoryBlock(size)
buffer.StringValue(0,size) = dbi.StringValue(false) 

Dim result As Ptr = RegisterDeviceNotificationW(AddressOf WndProc, mb, DEVICE_NOTIFY_WINDOW_HANDLE )

Or simply

Dim dbi As DEV_BROADCAST_DEVICEINTERFACE
  
Dim result As Ptr = RegisterDeviceNotificationW(AddressOf WndProc, dbi, DEVICE_NOTIFY_WINDOW_HANDLE )

@Tim: Not working.

And @dbi is not Ptr so it will be error

I used GetLastError, it return 0xD - ERROR_INVALID_DATA. Some parameters are invalid

Here an example program using this function:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363432(v=vs.85).aspx

I think that different parameters must be specified when calling this function as, for example, the window handle receiving the notification (the 2nd function parameter).

Here the docs for this function:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363431(v=vs.85).aspx

@Maurizio Rossi:

You are right. Window handle type is invalid. I change Declare from:

Soft Declare Function RegisterDeviceNotificationW Lib "user32" ( handler as Ptr, interfaceFilter As Ptr,  flags As Integer) As Ptr

To:

Soft Declare Function RegisterDeviceNotificationW Lib "user32" ( handler as Integer, interfaceFilter As Ptr,  flags As Integer) As Ptr

And when call this fuction, using Me.Handle

 RegisterDeviceNotificationW(Me.Handle,mb, DEVICE_NOTIFY_WINDOW_HANDLE )

Invalid data is not returned. But I get a new error: 0x42A - ERROR_SERVICE_SPECIFIC_ERROR

I think the problem in here is interfaceFilter. It must be a pointer of DEV_BROADCAST_DEVICEINTERFACE Structure. I try but it still wrong.

The example program has all that is needed.
But this isn’t exactly a “basic” program on the complexity side…
Try to translate the example program.

Best regards.

If you can do this already in VB.NET, then perhaps you could do the device notification work in a vb.net helper app. which sends you a TCP/IP message with device change notifications or your own.

Or a DLL.

@Chris Carter : That’s really a good idea to resolve the problem. Thanks you. But I don’t know how to send TCP/IP message from VB.NET helper app to Xojo app. Could you suggest to me any documents?

Thanks

I don’t use vb.net myself, so I’m not sure of the quality of this, but a quick googlefinds…

http://vb.net-informations.com/communications/vb.net_Server_Socket.htm