Capturing windows events in C++ application - c++

I need to implement a c++ program to capture the power source change event time (AC from/to DC) and do some process when the process occurs. I have seen examples using Event Tracing for Windows (ETW). Is there any other mechanism available to capture windows events?

Create a top-level window to receive WM_POWERBOADCAST messages, in which you can check for PBT_APMPOWERSTATUSCHANGE notifications:
Notifies applications of a change in the power status of the computer, such as a switch from battery power to A/C. The system also broadcasts this event when remaining battery power slips below the threshold specified by the user or if the battery power changes by a specified percentage.
Alternatively, you can use RegisterPowerSettingNotification() to receive WM_POWERBROADCAST messages containing PBT_POWERSETTINGCHANGE notifications for GUID_ACDC_POWER_SOURCE events:
The system power source has changed. The Data member is a DWORD with values from the SYSTEM_POWER_CONDITION enumeration that indicates the current power source.
Related: How to get the Windows Power State Message (WM_POWERBROADCAST) when not running a Win32 GUI app?

Related

Jack Audio Kit API: Process Callback and Non-Realtime Functions

I'm planning to write a small program that connects to the Jack Audio Kit on Linux. Whenever there's a new audio available to my program, Jack will call the registered callback. According to the documentation, the callback function should enable "real-time execution".
However, I'd like my program to do some analysis over the data flowing through and displaying this in a window. This is inherently not real-time.
To circumvent this issue, in an exploratory Python script, I used a queue to transfer data into another thread. But although the callback is called, the thread doesn't receive anything. I suspect this is due to the callback being called in another process. The debugger also doesn't stop inside the callback.
I saw, there are several applications for Jack that do other non-real-time things like UI and thelike (e.g., volume meter in Cadence). How is it possible to implement this callback without losing the real-time execution property?

Collect dumps during Windows power events

My application is a Win32 service which is registered for OS power events. When application gets callback of event, operations are performed depending on the type of the power event. During suspend/sleep event, our application tries to do specific networking jobs within 10 seconds before acknowledging the OS to continue with the suspend operation.
Under normal circumstances, my application does job successfully within 10 seconds. But sometimes, operations take too long and fail to complete with in 10 seconds. Application uses various Win32 API functions and I want to check which specific operation is causing the delay. Is there anyway to capture the dump of process hung in completion of jobs when system is going to suspend ?
I tried ProcMon, ProcExplorer, ProcDump but it didn't help. Any ideas on troubleshooting the issue other than adding logs in the code. Thanks.
Windbg Preview tool available on Microsoft store is very helpful for capturing traces of any running process. My use case is to capture the trace of my process when OS hibernates. The trace can be used to time travel debug my application when OS was actually hibernating. A very useful tool from Microsoft. Tutorials on Channel 9 on how to use the tool. Happy learning :)

How to send system events from driver to application layer?

I'm writing a driver for a device with Windows Embedded Compact 7 OS, in which applications written in .NET 3.5 will be running. My requirement is that, I need to send some custom defined system events (for some conditions that occurred in the driver) to these applications so that the corresponding event handlers written in the application should be executed when an event is invoked.
So,
What should I do to invoke/raise such events?
Which function is to be used?
How do a system event differ from a message?
How to add Event handlers in a .NET application?
TIA.
Using the plain win32 API, I would create a named event (i.e. supply a name for CreateEvent()). This can be use across process boundaries, including across the kernel/userspace boundary. You would then simply use WaitForSingleObject() and related functions to check the event state.
If you have a stream driver, you can also call ReadFile() from the application and simply stall inside the according handler function of the driver. This makes it pretty easy to add data to the event, too. Further, it provides separation between different processes that access the driver, or even different instances within the same process. Compare this with the event above, which is effectively system-wide visible and can also be set by different processes, although you can restrict this to some extent.
Another alternative is to use window messages (PostMessage(), SendMessage() etc) which also work across process boundaries. I'm not sure if this works from the kernel though. These would then end up in the "normal" message queue of e.g. the applications main window or any other window you previously told the driver. Compared to the other two approaches, you need a window as target, so it only works one way, and you don't know where a message came from.

Windows Mobile 6.5 -- Call a function when a dialog box is idle? a la WM_KICKIDLE

I'm working on a Windows Mobile 6.5 application that has a dialog box that displays input from a camera and has a button to save a snapshot of the stream. The camera API recommends calling the function that updates the view of stream when the application is idle, via the Windows Message Loop, but doesn't get any more specific than that. After much Googling, I still can't find anything helpful in terms of actually implementing something like this.
Does anyone know how this might be achieved?
You'll have to implement a message loop, not using the conventional GetMessage which blocks until a message exists in the thread's message queue[1], but rather using PeekMessage, which returns false if no message exists[1].
If it returns false, then you do your idle processing. Note that you should divide your idle processing in small enough chunks so that the message loop doesn't cause unresponsiveness to your app.
This is also a classical alternative to threading on 1 cpu or 1 core.
[1] or should be synthesized (painting or timers)

Get number of processors a particular process is running on

I have the processID associated with a process. I have created this process using the CreateProcess() function. During the run of it, I want to track how many processors it runs on and how much time this executable has used on multicore machines.
I want to write C++ code for the same; can anyone help me on this?
I am using Win XP multicore machines.
GetProcessAffinityMask:
Retrieves the process affinity mask for the specified process and the system affinity mask for the system.
GetProcessTimes:
Retrieves timing information for the specified process.
You can capture this level of detail on Vista or later using Event Tracing for Windows (ETW) and the CSwitch event (which is emitted on every context switch).
Various tools (e.g. the Windows Performance Toolkit) capture and visualize this data.
However, this isn't supported on Windows XP.
If you just want know what your typical concurrency is (i.e. how many of your threads are running at a given time) you could regularly sample the perfmon Thread data (from HKEY_PERFORMANCE_DATA). The "Thread State" counter will give you the instantaneous state of every thread in your process (i.e. whether each thread is running or not). Obviously this sampling process will limit the maximum concurrency to (number of processors - 1).
But do you really need this much detail? GetProcessTimes is usually enough.
Update
You can run your app on a test machine and simply measure the utilization of each CPU using perfmon. You should also measure the CPU utilization of each process to ensure nothing else is running unexpectedly.
To capture data for a report, run perfmon as an Administrator.
Navigate to "Performance Monitor" on the right hand side to display the real-time performance chart. Select the objects/counters you want to monitor (i.e. "% Processor Time" for all Processors and Processes). Perfmon should start capturing the data in real time.
Right-click on the graph and select the capture frequency (e.g. if your app is running for hours you probably don't want data every second).
Right-click on the "Performance Monitor" node in the right-hand tree and select "New|Data Collector Set". Enter a name for it and click through the other defaults.
Navigate to your Data Collector Set on the right (under "Data Collector Sets|User Defined"). You can start and stop data collection using the toolbar buttons (or by right-clicking).
Now you've got some data return to the performance monitor graph and select "View Log Data" (the second toolbar button). Select your log file from the Source tab. This displays a graph of your captured data.
Right-click on the graph and select "Save Data As..." You can choose CSV or TSV.
And that's it.