C/C++ USB drive event - c++

Regarding the Windows platform, is their an event I can look for to tell when a USB drive or any type of portable media us plugged in?

You won't get such notifications without first registering for them. Use RegisterDeviceNotification() to do that.
After you have registered your window to receive such notifications, handle the WM_DEVICECHANGE message.

Related

How to detect wake up from sleep mode in windows service?

Good day! I write a windows service, which detects USB Flash drive connection and do something with connected drive. And now after tests i need to reprocess devices after wake up from sleep mode. I solve this problem when service work as windows program. To solve i additionally process DBT_DEVNODES_CHANGED, but service don't receive this message, it receives only DBT_DEVICE_ARRIVAL and DBT_DEVICEREMOVECOMPLETE. I also tried to check all messages handled by service except SERVICE_CONTROL_DEVICEEVENT and SERVICE_CONTROL_STOP when system is going to sleep and i don't get any message.
How can I determine when the system is waking up?
A normal application receives a WM_POWERBROADCAST:PBT_APMRESUMEAUTOMATIC window message, but a service will not get this message if it does not have a window. However, a service can receive SERVICE_CONTROL_POWEREVENT in its HandlerEx callback function, with the same parameters as WM_POWERBROADCAST
Notifies a service of system power events. The dwEventType parameter contains additional information. If dwEventType is PBT_POWERSETTINGCHANGE, the lpEventData parameter also contains additional information.
On Windows 8 and later, you can also use the PowerRegisterSuspendResumeNotification() function:
Registers to receive notification when the system is suspended or resumed.
The service equivalent of WM_DEVICECHANGE is SERVICE_CONTROL_DEVICEEVENT:
Notifies a service of device events. (The service must have registered to receive these notifications using the RegisterDeviceNotification function.) The dwEventType and lpEventData parameters contain additional information.
The best way to detect that system is waking up is process power messages.
Windows must process WM_POWERBROADCAST
Services must add SERVICE_ACCEPT_POWEREVENT to last call to SetServiceStatus and process SERVICE_CONTROL_POWEREVENT
My error was very stupid. I forget to add SERVICE_ACCEPT_POWEREVENT.

How to trigger mouse and keyboard event programmatically in Windows 8?

I am trying to develop a program so that I could use my android phone as mouse. For that i will have a service running on Windows pc which will wait for packets from android phone through socket connection. These packets will contain information about the mouse events like button pressed, position etc. I want to dispatch the event in the system. How would I do that using C/C++? Thanks in advance.
Use the SendInput function to generate simulated keyboard and mouse events.
If you are going to create an actual NT service then you might have a problem because the service will be running in the wrong session. If you still feel you need a service then you might have to spawn a new process that runs as the user in each session with CreateProcessAsUser but it is much simpler just to design it as a normal program that starts when a user logs in by adding a Run registry entry.

Trigger an exe once My device is connected via USB

Once my embedded device is connected to USB port of my PC, it should trigger an exe as an event. How can I achieve this??
Should I create a service to keep monitoring the USB connector bus or is there any default API's available in Windows to achieve this??
thanks.
A simple exe which is started on connect is not possible. But you can write a service or user mode application which listens for device arrival events. WM_DEVICECHANGE is sent to all (registered) applications with a device interface guid which represents which device is plugged in. You can then use this id with the setupapi to see if its your device.
On receiving that event, you can then start your executable.
Depending on your version of Windows it might be possible with a workaround using a AutoRun.inf file in the root folder of a USB drive. For security reasons this is by default turned off, and in Windows 7 not allowed at all.
To achieve the same effect in a more robust way, you need to create a service that monitors whether your device is connected or not (e.g. iTunesHelper that monitors for connected Apple devices).
The easiest solution is probably a trivial UMDF driver. That's basically a small COM component called when your device is connected.

Detecting FireWire cable event within MFC C++?

I'm trying to detect the event when a FireWire cable is plugged into the FireWire port of the PC withing MFC C++ app. I would also like to trigger this even myself as the FireWire connections drops and never is rediscovered withing unplugging the cable and plugging it back in.
Anyone have any experience trying to programmaticaly simulate a unplug-replug event?
You can register yourself to receive device arrival messages. See the help for RegisterDeviceNotification to find an example on how to register yourself.
You can trigger a bus reset notification on the firewire bus, but I don't know if you can do this from user mode, without the help of a kernel mode module.

Microphone plug in event/ Mic attach event Vista

I want to be notified when a microphone jack is plugged in. What is the event fired by the OS(particularly Vista)
The audio panel shows no recording device active if no mic plugged in(vista). This never happened in XP.
Also if my microphone had a "advance control" for eg Bass Boost, Mic Boost(AGC) etc, how can I get the mixer control for the same to control it.
I'm using MFC for development.
Thanks
I believe that you want to implement IMMNotificationClient and handle the OnDeviceAdded event.
If you want the various controls in Vista, you're better off using the Device Topology API to get the controls (IAudioLoudness, etc.) Otherwise, you need to enumerate the mixer device IDs & ask them for their endpoint IDs, then compare the endpoint ID to find the real device you're interested in.