Detect existing idle application - c++

I have a problem and I need a approach for the next problem:
Lets say there is an application "A". This application "A" can be used only if you have a license. There are couple of users that use this application and "reserve" the license for them, if the license is free. But, sometimes the user who "reserved" the license, forget to "unreserved".
The main question: How can I detect if a user is not using the application for X minutes (the application is in idle state)?
It doesn't mean that the user is not active on the computer - he is using another applications on the computer, but he doesn't use this particular application "A" . (The user have to login so he can use the license. That means that even he exits the application, minimizes the application or simply the application is in the background of other applications, he still poses the license)
I read about GetLastInputInfo, but I don't believe that it's going to help me in my situation. All I want is to get info if the user is interactive with the application.
I'm using C++.
OS: Windows 10 x64bit.

How about your application hooks the input queue and filters on the messages that YOU think are applicable to the application being used (probably keyup/down/mousemove probably not WM_TIMER or WM_PAINT) and then when you decide it's been long enough since you received one of these messages (set a timer each time one of the selected messages has been received), that's when you release the licence key.

Related

Set audio endpoint devices application specific (programmatically)

Link to the bug report on 'Feedback Hub'
An audio endpoint device, from here on referred to as 'endpoint', is a physical or virtual audio output or input device.
With the Windows 10 April Update 1803 the long overdue 'App volume and device preferences' have been introduced. These settings allow more control over audio stream management as it is now possible to set different endpoints for different applications, no matter whether that particular application comes with an endpoint selection or not.
However, there is an issue where the audio of a program, whose endpoint is non-default, is streamed through the default endpoint (or not at all) after it has been closed and launched again, although the endpoint is displayed correctly in the settings:
As far as I know the issue can be recreated on a Windows 10 machine (version 1803 or higher) with any virtual or physical endpoint and an affected program. I used 'VLC Media Player' in this example (disregarding the fact that it comes with an endpoint selection) as it is well known and widely accessible, which should make it easier to recreate the issue.
What I'm searching for...
... is a programmatically solution to switch between endpoints, which ideally can be launched in form of a script to set the correct endpoint with an application launch.
For my purpose it would be enough to have to adjust the device instance path manually, as the device would be always the same, but I'm not going to complain about a solution which retrieves the device instance path from the registry, too.
Defined endpoints and the device instance path of the device they are using can be retrieved from the subkeys of the key HKEY_USERS\# YOUR SID #\Software\Microsoft\Multimedia\Audio\DefaultEndpoint. I don't know how windows generates the name of the subkeys or where they can be found. If I had to take a wild guess, I'd say these are Application IDs (feel free to correct me if I'm wrong).
The device instance path itself can be found in the Device Manager (under 'Audio inputs and outputs' double click the desired device, navigate to the tab 'Details' and select 'Device instance path' from the 'Property' drop-down menu).
Additionally the entry about Audio Endpoint Devices and Stream Management in the Microsoft Docs might be helpful, but that is way above my head.
A possible but impractical workaround...
... would be, to manually set another endpoint for the application and switch back to desired endpoint at every launch of said application (as shown above).
But not just takes this at least 10 seconds at each and every launch, you might even forget to do this as the audio might just get streamed through the default endpoint *¹.
The alternative to the latter is, that no audio will be streamed at all *² or in some cases it actually works *³.
*¹ e.g.: VLC Media Player, Tom Clancy's Rainbow Six Siege (although the audio will be streamed correctly during the splash screens)
*² e.g.: Call of Duty 4: Modern Warfare, Call of Duty: Modern Warfare 2, Call of Duty: Modern Warfare 3
*³ e.g.: Window Media Player, Microsoft Edge, Firefox
Observations
VLC Media Player comes with an endpoint selection, but so does TeamSpeak 3 and, unlike VLC, it skips the Windows settings completely.
Call of Duty not streaming any audio most likely is connected to the engine as I didn't encounter any other application doing something similar.
Windows Media Player, Microsoft Edge and Firefox are the only programs (I tested so far) which work fine. They have no endpoint selection (I'd know of) and will use the correct endpoint after closing and launching it again. It should be noted, however, that Firefox and Microsoft Edge will show multiple instances in the "App volume and device preferences" when adjusting the endpoint.
Disclaimer
I already tried two 3rd party softwares: 'Audio Router', which didn't work at all and 'CheVolume', which doesn't solve the issue and constantly crashes while doing so.
This question is based on one I asked over at Super User (here), where I didn't get an answer I was able to work with due to my lack of knowledge regarding actual programming (I'm only somewhat familiar with Batch and PowerShell). I'm well aware that neither Stack Overflow nor Super User are script writing services, however, the issue is not being fixed with the Windows 10 October Update 1809 and I see this as a problem which is affecting not just me and with that would be helpful for multiple people after me. Feel free to write a comment or propose an edit if you see this differently.
I'm also not sure whether the tags 'audio-streaming' and 'endpoint' should be used in this context, please propose an edit if they shouldn't or you can think of any better.
Edit - 05/11/18
Using the 3rd party software 'EarTrumpet' I was able to overcome the issue with the 'Call of Duty' games (no audio at all after restarting), however, 'VLC Media Player' would not restart after I assigned a non-default endpoint with 'EarTrumpet' until I closed 'EarTrumpet' again and the issue with 'Tom Clancy's Rainbow Six Siege' remains the same.
Edit - 18/01/19
Added link to a bug report I created on the 'Feedback Hub' 2 month ago.
Edit - 20/01/19
After doing some testing again it should be noted that having 'EarTrumpet' run in the background will keep a non-default endpoint for 'VLC Media Player' across restarts, however, 'VLC Media Player' will only (reliably) restart when the non-default endpoint was set in the 'App volume and device preferences'.
I do not have any solution regarding a programming language to handle such events.
But I can recommend EarTrumpet app to handle this change more quickly https://www.theverge.com/2018/6/13/17457778/eartrumpet-windows-10-audio-app
(Windows store: https://www.microsoft.com/en-us/p/eartrumpet/9nblggh516xp?ranMID=24542&ranEAID=nOD%2FrLJHOac&ranSiteID=nOD_rLJHOac-hUn6PgKuMKwQLdrzRqnPTA&epi=nOD_rLJHOac-hUn6PgKuMKwQLdrzRqnPTA&irgwc=1&OCID=AID681541_aff_7593_1243925&tduid=%28ir__qwqlg6jd0jba3y9hpnbvikaite2xk6kuyv9udtr100%29%287593%29%281243925%29%28nOD_rLJHOac-hUn6PgKuMKwQLdrzRqnPTA%29%28%29&irclickid=_qwqlg6jd0jba3y9hpnbvikaite2xk6kuyv9udtr100&activetab=pivot:overviewtab )
I will update the answer if I find a easy way to script/program a change of output on each app.

C++ Trapping system dialogs in a kiosk system

We have a kiosk system running on Win7 with application written using VS2010 C++. As with kiosk systems, the system is locked down so that the user cannot access the windows system itself, but must do all work using our application.
Unfortunately, we have had one issue so far where a windows system-level dialog has popped up requiring a response. It popped up behind the GUI of our application, so that the user didn't even know it was there, and since it was modal, it blocked further use of the system.
These dialog was the well-known "system needs to be restored" dialog. Since this is a kiosk system, we are wanting to find a way to handle these types of situations in an automated fashion.
I have looked into setting a low level hook using SetWinEventHook() to capture EVENT_SYSTEM_ALERT events. The first problem of course is that I am not sure how to test this, since these events are not common. The second problem is that I am not sure how to handle the information, since there could be a number of different system alert events that pop up modal windows, and so automating a response might get us into more trouble than we might foresee.
My real question here is, if you were in this exact situation, what would be your line of attack. I am concerned I may be going about this the wrong way by trying to capture alerts and somehow automate a response to the resulting system alert window.
Any clues as to a useful direction here would be much appreciated.

controlling an application via c++ code

I am writing an application in C++ in windows, that has a UI (WxWidgets) and user normally use the application via its UI.
Now I have a new requirement, the application needs to start and controlled by another application.
I can not develop a DLL or similar solutions.
I have access to my code (apparently!) and the other applications is developed by other users, but I can give them details on how to control my application.
My question is: How can I allow other applications to control my application via a defined interface?
For simplicity assume that I developed a calculator (has UI) and I want to give other application to do math on my application (for example they may ask my application to add two numbers and so on, As the math is very time consuming, I need to inform them about progress and any error that generate during processing.
Can I open a pipe to communicate?
Any other way to achieve this?
You can use pipes or tcp/sockets with a custom protocol, but probably it's better if you split your application in two parts:
One part that does the computation
The user interface
and publish the first one as an http server responding to JSON requests.
Using a standard protocol can ease up testing and increases interoperability (you can also probably leverage already existing libraries for both implementing the server and the JSON marshalling).
Note that in addition to accepting commands, any error message you are going to show for example in a message box or any other nested event loop like dialog boxes need to be rewired properly; this can be very problematic if message or dialog box come up as the result of calls to external code that you didn't write yourself.
This is the typical change that would have costed 10 if done early and that will cost 1000 now.

Will Windows always allow hooks and/or the journal record?

I'm writing a keylogger/mouse tracker for use in an opensource input heatmapping application basically identical to Razer's newest heatmapping software, but for use with any hardware/OS (using Qt's amazing cross platform SDK). As you would imagine, this involves intercepting keyboard and mouse messages from the kernal when the application is not the main process.
For Windows I was drawn to GetAsyncKeyState, but there's a note on the return value from MSDN about this function returning zero if "the foreground thread belongs to another process and the desktop does not allow the hook or the journal record."
Barging ahead regardless, I wrote a method for getting the keyboard state (that triggers every set interval of time via Qt's QTimer methods) and it just worked:
//The following executes every 100th of a second:
for (int i = 0; i < 256; ++i)
{
keyboardArray[i] = GetAsyncKeyState(i);
}
As I watch this array in the debugger, I can see the values in the array change as I type even when the application is not the main process. So, for my computer at least this function works at monitoring key states when the main thread is not focused on my application.
My question is: In what instances does Windows not allow hooks or the journal record? In other words, are there some versions of Windows and/or privileges a user could have/not have where this method could fail? I don't really have access to a bunch of different machines to test this on.
My specs are Windows 7 Home Premium 64 bit, Intel i7 930 (2.8 GHz, quad core hyper threaded), 12 GB DDR3 1333 MHz memory, 2x Nvidia 460 if any of that helps.
Best Regards,
Weikardzaena
EDIT:
Hans Passant gave me an example of situations where this type of implementation would fail: mainly applications on Windows that include User Interface Privilege Isolation (UIPI). Basically if an application is really important to the operating system (like a command prompt) then this type of message intercept will not work. I even tested it and it's true: my application stops updating the keyboard array when a command prompt is the main thread.
This and what LoPiTaL said suggests that only specific applications will not allow this type of intercept to occur. I'm mainly aiming this application toward gamers who (like myself) would like to see key presses and mouse clicks for their gameplay, so maybe I don't care about this issue as much, but if I want to expand this to general use (including people who use CMD a lot) then it seems like there's actually no way to intercept key messages for those types of elevated applications.
Is that true, or can methods like SetWindowsHookEx still intercept messages to UIPI applications? I was trying to avoid implementing hooks directly because that might be viewed as a virus on people's home machines, and capturing and re-emitting every input message just slows down everything, which in gaming is pretty big deal.

Can I hook return value of "Ctrl+Alt+Del"?

On windows, windows security screen(including log off, sutdown, taskmgr and so on) is appeared when we push ctrl+alt+del.
But I want to make show up my application(MFC) when we hit ctrl+alt+del.
So I need the return value of ctrl+alt+del.
How to get the return value from ctrl+alt+del or what is return value from it?
Could you elaborate on what you want to achieve? You probably will not (and most certainly should not) be able to re-hook the Secure Attention Sequence to perform application-specific actions. What you can do, however, is customize or replace the login component (GINA) that is responsible for handling the SAS -- this might be useful, for example, in kiosk systems where you want to restrict users from logging out.
MSDN Magazine had an article on that a while back, which you might find interesting: http://msdn.microsoft.com/en-us/magazine/cc163803.aspx
On Windows Ctrl + Alt + Del combination is handled by Winlogon process. You cannot interfere with it due to security reasons.
You should write your own msgina.dll; it is not an easy task, but doing it you'll be able to control much more than just Ctrl+Alt+Del.
Ctrl+Alt+Del is a system hot key registered by the process winlogon in early versions of Windows (that is, 2000/XP pre SP1). You cannot override the registration or hook the WM_HOTKEY message (at least not in a safe way). It is understandable that the OS prefers operating system (specifically the Winlogon process) rather than a third party program to get the user's password, but want to be flexible in case the user want to authenticate via fingerprint, smart card, etc read more here .
On Windows 2000/XP the official way to customize the login experience is to write your own Gina (detailed in the article linked in ig2r's reply) but Gina can conflict with each other (e.g. A Think Pad laptop with fingerprint login has problem with McAfee Safeboot, pcAnywhere, etc). Gina is gone in Vista.
On Windows Vista or later the way you can customize the logon experience is credential provider. It's extensibility is limited, but you still get an HWND via ICredentialProviderCredentialEvents::OnCreatingWindow in case you want to display a dialog in response of credential provider events. The documented way to use the HWND as the owner of your dialog (be win32 or MFC or whatever).
Of cause there is always the keyboard driver filter approach, provided you know driver development and user mode-kernel mode communication for each Windows version. You can even block the whole keyboard if you want.