Re: [2005] Photo Management
Your first step is to write the code that detects the device being plugged in. You will need to use WMI for this. Do a search on RegisterDeviceNotification.
Re: [2005] Photo Management
Ok, found a sample:
Code:
here is a WinForm application to detect Human Input Devices (HID),
including all kinds of mouse / keyboard / keypad / joystick / etc. If you
want to detect all USB devices, uses GUID_DEVINTERFACE_USB_DEVICE =
"A5DCBF10-6530-11D2-901F-00C04FB951ED" instead.
---- begin ---
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
public Form1()
{
InitializeComponent();
RegisterHidNotification();
}
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case Win32.WM_DEVICECHANGE: OnDeviceChange(ref m); break;
}
base.WndProc (ref m);
}
void OnDeviceChange(ref Message msg)
{
int wParam = (int)msg.WParam;
if(wParam == Win32.DBT_DEVICEARRIVAL) label1.Text = "Arrival";
else if(wParam == Win32.DBT_DEVICEREMOVECOMPLETE) label1.Text =
"Remove";
}
void RegisterHidNotification()
{
Win32.DEV_BROADCAST_DEVICEINTERFACE dbi = new
Win32.DEV_BROADCAST_DEVICEINTERFACE();
int size = Marshal.SizeOf(dbi);
dbi.dbcc_size = size;
dbi.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE;
dbi.dbcc_reserved = 0;
dbi.dbcc_classguid = Win32.GUID_DEVINTERFACE_HID;
dbi.dbcc_name = 0;
IntPtr buffer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(dbi, buffer, true);
IntPtr r = Win32.RegisterDeviceNotification(Handle, buffer,
Win32.DEVICE_NOTIFY_WINDOW_HANDLE);
if(r == IntPtr.Zero)
label1.Text = Win32.GetLastError().ToString();
}
}
class Win32
{
public const int
WM_DEVICECHANGE = 0x0219;
public const int
DBT_DEVICEARRIVAL = 0x8000,
DBT_DEVICEREMOVECOMPLETE = 0x8004;
public const int
DEVICE_NOTIFY_WINDOW_HANDLE = 0,
DEVICE_NOTIFY_SERVICE_HANDLE = 1;
public const int
DBT_DEVTYP_DEVICEINTERFACE = 5;
public static Guid
GUID_DEVINTERFACE_HID = new
Guid("4D1E55B2-F16F-11CF-88CB-001111000030");
[StructLayout(LayoutKind.Sequential)]
public class DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
public short dbcc_name;
}
[DllImport("user32.dll", SetLastError=true)]
public static extern IntPtr RegisterDeviceNotification(
IntPtr hRecipient,
IntPtr NotificationFilter,
Int32 Flags);
[DllImport("kernel32.dll")]
public static extern int GetLastError();
}
}
You will need to translate to VB.NET.
Re: [2005] Photo Management
I will do that. C# isnt very different to vb.net is it?
Re: [2005] Photo Management
Quote:
Originally Posted by tarik666
I will do that. C# isnt very different to vb.net is it?
Nope, not very different at all.
Re: [2005] Photo Management
god this is hard. Can anyone help.
Re: [2005] Photo Management
What do you find hard? Perhaps we can guide you through it.
Re: [2005] Photo Management
Well i dont understand that code.
Re: [2005] Photo Management
Try a converter. It'll be erroneous but most of it will be converted for you and you can convert the 'mistakes' to proper VB.NET code.
Re: [2005] Photo Management
Used InstantVB to convert the code (Not sure how accurate / if it will work at all)
Code:
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Namespace WindowsApplication1
Public Class Form1
Inherits System.Windows.Forms.Form
Private label1 As System.Windows.Forms.Label
Public Sub New()
InitializeComponent()
RegisterHidNotification()
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case Win32.WM_DEVICECHANGE
OnDeviceChange(m)
End Select
MyBase.WndProc (m)
End Sub
Private Sub OnDeviceChange(ByRef msg As Message)
Dim wParam As Integer = CInt(Fix(msg.WParam))
If wParam = Win32.DBT_DEVICEARRIVAL Then
label1.Text = "Arrival"
ElseIf wParam = Win32.DBT_DEVICEREMOVECOMPLETE Then
label1.Text = "Remove"
End If
End Sub
Private Sub RegisterHidNotification()
Dim dbi As New Win32.DEV_BROADCAST_DEVICEINTERFACE()
Dim size As Integer = Marshal.SizeOf(dbi)
dbi.dbcc_size = size
dbi.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE
dbi.dbcc_reserved = 0
dbi.dbcc_classguid = Win32.GUID_DEVINTERFACE_HID
dbi.dbcc_name = 0
Dim buffer As IntPtr = Marshal.AllocHGlobal(size)
Marshal.StructureToPtr(dbi, buffer, True)
Dim r As IntPtr = Win32.RegisterDeviceNotification(Handle, buffer, Win32.DEVICE_NOTIFY_WINDOW_HANDLE)
If r = IntPtr.Zero Then
label1.Text = Win32.GetLastError().ToString()
End If
End Sub
End Class
Friend Class Win32
Public Const WM_DEVICECHANGE As Integer = &H0219
Public Const DBT_DEVICEARRIVAL As Integer = &H8000, DBT_DEVICEREMOVECOMPLETE As Integer = &H8004
Public Const DEVICE_NOTIFY_WINDOW_HANDLE As Integer = 0, DEVICE_NOTIFY_SERVICE_HANDLE As Integer = 1
Public Const DBT_DEVTYP_DEVICEINTERFACE As Integer = 5
Public Shared GUID_DEVINTERFACE_HID As New Guid("4D1E55B2-F16F-11CF-88CB-001111000030")
<StructLayout(LayoutKind.Sequential)> _
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
<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function RegisterDeviceNotification(ByVal hRecipient As IntPtr, ByVal NotificationFilter As IntPtr, ByVal Flags As Int32) As IntPtr
End Function
<DllImport("kernel32.dll")> _
Public Shared Function GetLastError() As Integer
End Function
End Class
End Namespace
Re: [2005] Photo Management
alright. The code isnt running errors. Some I am going to test it out. Just as a thought. Does this detect cameras only or anything that is plugged into the usb port
Re: [2005] Photo Management
Yes, it should detect if any device is plugged in to the USB port. "SHOULD" being the keyword here. I haven't actually used it myself :D
Re: [2005] Photo Management
Well I wonder if vb.net has a way of checking what sort of device it is. I want it to only alert me if its a camera