Question

I'm trying to make a PInvoke call to register any USB device connection notification.

[DllImport("user32.dll", SetLastError = true)]
protected static extern IntPtr RegisterDeviceNotification(IntPtr hwnd, DeviceBroadcastInterface oInterface, uint nFlags);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
public class DeviceBroadcastInterface
{
    public int Size;
    public int DeviceType;
    public int Reserved;
    public Guid ClassGuid;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string Name;
}

A handle to the window, normally obtained from OnHandleCreated in Winforms application is required to make such PInvoke calls. Is there a way of obtaining a handle of the usercontrol in silverlight 5?

Was it helpful?

Solution

Silverlight, unlike WPF, does not expose window handles. However, you can, if you try hard enough, find the handle to your window. This article describes the method. Essentially you call the FindWindow function to get hold of the window handle.

And remember that you won't get a handle for your user control because only top level windows have handles under Silverlight. Silverlight controls are not windowed.

As an aside, are you quite sure that your struct is packed? That would be quite unusual.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top