windowsdelphifocusfocusmanager

Starting my app when USB device connected


I need to starting my app when our USB device connected.

My first attempt at this is a background application that pays attention to when USB devices are plugged in. When it notices our device connected, it calls ShellExecute( ) and starts our application.

This works nicely except in Windows 8. Supposing we're on the "Start" screen in Windows 8. In that situation, the application starts in the background and the start screen remains in front.

I think this is a "focus" problem since what I'm actually hoping to do is "steal focus" from the Start screen.

http://blogs.msdn.com/b/oldnewthing/archive/2009/02/20/9435239.aspx

Since my background application doesn't have focus (the Start screen has focus; besides, my application doesn't have a UI), it can't give focus away to my foreground application.

Let me say that in general, I hate focus stealing. Starting the app the user wants to use is a great help to our users.

How can I fix this problem? Maybe the answer is to programmatically ask the start screen to start my app but I don't see a way to do that:

http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/9ed23f32-0708-48a8-9ff7-5fce6dbe123f/windows-8-start-screen-api

Maybe I'm approaching this in the wrong way. Suggestions?


Solution

  • You're going about this the wrong way. Although you can write a program to constantly monitor the USB ports to detect when something is connected, there's no need. The OS is already doing that anyway.

    Register an Autoplay handler for your device type and class. This is distinct from the old AutoRun feature, which would automatically execute programs found on an inserted file system.

    You can begin with an overview of the feature from the November 2001 issue of MSDN Magazine.

    Your handler will be a COM DLL. You can register the DLL as a handler, and register the handler with events you want to handle. You can either perform everything in the DLL, or you can put the bulk of the functionality in your application and just use the DLL as a proxy between the OS and your program.