Windows - How do I disable the "Wrong Volume" error message - c++

My application is reading/writing data to a removable media (USB DOK) in the background. The problem is that when the USB is removed while the app is working, the computer pops up an error message:
Wrong Volume
The wrong volume is in the drive. Please insert volume into drive E:.
Cancel Try Again Continue
This happens during operations such as GetFileSize, ReadFile. Obviously, since the app is supposed to work in the background, I would like to suppress those messages and fail silently.
BTW - It seems that the process giving those message is not my process, but CSRSS.EXE (although the cause is definitely the operation from my process).
One direction I am considering is switching to NtQueryInformationFile, NtReadFile, etc., but I'd rather not...
Thanks

Try calling:
SetErrorMode(SEM_NOOPENFILEERRORBOX);
At the beginning of your main function.
From the documentation:
The system does not display a message box when it fails to find a file. Instead, the error is returned to the calling process.

I suggest you properly disconnect your hardware using the icon in the windows system tray so that it's not being accessed when you pull it out of the drive. Or at least exit your ap first.

Related

How to be sure that mount command is actually finished in qt process

I'm developing a program written in qt5, where i need to mount some devices (USB DRIVE) and show in a GUI the contents of that device.
I'm using QProcess() to mount the device, and after that i explore the folder where the device should be mounted. Unfortunately only few files can be found.
I tryed to add a QThread::sleep() after the mount command, and this is actually work for small devices. So it seems that the correctness of my approach depends on how much i wait for the system to actually mount the device.
pr3.start("mount foo foo");
pr3.waitForFinished(-1);
QThread::sleep(6); //This is a patch but it fail depending on the amount of file stored in the device
Is there any way to be sure when the QProcess("mount") has finished correctly?
I tryed the wawtforfinished(-1) method of QProcess but it doesn't
seems to work.
Thanks
First thing, I would advise you to use the following signal finished() and connect it to a slot that checks the exit code and the exit status instead of using waitForFinished().
Indeed, the documentation states about waitForFinished() that:
Warning: Calling this function from the main (GUI) thread might cause your user interface to freeze.
Of course it assumes you have an event loop.
Other thing, your mount foo foo command seems wrong. The first parameter is the device itself and the second one is the directory where to mount the device. You cannot give twice the same parameter.
Please let me know if this does not help to solve your issue.
I tryed the wawtforfinished(-1) method of QProcess but it doesn't
seems to work.
That is big red flag something strange is going on.
When starting child processes programaticly a lot of error handling and reporting is typically in order.
Also this could include, but is not limited to, that the mount command is not working as expected, etc.

Minifilter: Block applications with notification

I'm writing a minifilter to block application execution. The minifilter will request a file scan on IRP_MJ_CREATE to the usermode apps. The usermode application will scan whether to allow the PE file (.exe/.dll/etc) execution or no.
Currently, when the usermode apps says no, the minifilter will issue an access denied status, and cancelling the file open. (Yes, using FltCancelFileOpen)
The problem when issuing access denied return value is, from the user perspective, they will get a message box from the system like this:
Another example, when blocking the specific dll from being loaded, another messagebox will appear:
What I want to accomplish is to still deny the open but suppress the message box and have a notification of my own, which is a user friendly error message indicating the apps were blocked. Example are like windows 8 smartscreen feature, which will notify the user when running blocked exe without any messagebox saying access denied or similar.
How can I do that?
Let's take the DLL example. You get that error because there's code in Windows equivalent to
if (!LoadLibrary(szDllName))
{
MessageBox("Application Error", ...);
}
else
{
DllMain = GetProcAddress("DllMain");
DllMain(DLL_PROCESS_ATTACH);
So, if you don't want the first branch of the code to be taken, you should allow the DLL to load. There's no third option.
The Windows 8 example is misleading. If you're Microsoft, of course you can add that third option.
[edit]
On second thought, did you cancel the operation using FltCancelFileOpen ? If not, then how did you do it?

DirectInput8 EnumDevices sometimes painfully slow

Sometimes (in about 50% of runs), EnumDevices takes 5-10 seconds to return. Normally it is almost instant. I couldn't find any other reports of this kind of behaviour.
When things are this slow, it's ok to profile by watching stdout :) This:
std::cout << "A";
directInput8Interface->EnumDevices(DI8DEVCLASS_GAMECTRL, MyCallback, NULL, DIEDFL_ATTACHEDONLY);
std::cout << "C";
...
BOOL CALLBACK MyCallback(LPCDIDEVICEINSTANCE, LPVOID)
{
std::cout << "B";
return DIENUM_CONTINUE;
}
Seems to hang at a random point through enumerating devices - sometimes it'll be before the callback is called at all, sometimes after a couple, and sometimes it will be after the last call to it.
This is clearly a simplified chunk of code; I'm actually using the OIS input library ( http://sourceforge.net/projects/wgois/ ), so for context, please see the full source here:
http://wgois.svn.sourceforge.net/viewvc/wgois/ois/trunk/src/win32/Win32InputManager.cpp?revision=39&view=markup
There doesn't seem to be anything particularly fruity going on there though, but possibly something in their initialisation could be the cause - I don't know enough about DI8 to spot it.
Any ideas about why it could be so slow will be greatly appreciated!
EDIT:
I've managed to catch the hang in an etl trace file and analysed it in Windows Performance Analyzer. It looks like EnumDevices eventually calls through to DInput8.dll!fGetProductStringFromDevice, which calls HIDUSB.SYS!HumCallUSB, which calls KeWaitForSingleObject and waits. 9 times out of 10 (literally - there are 10 samples in the trace) this returns very quickly (324us each), with the readying callstack containing usbport.sys!USBPORT_Core_iCompleteDoneTransfer followed by HIDUSB.SYS!HumCallUsbComplete, which looks quite normal.
But 1 time in 10, this takes almost exactly 5 seconds to return. On the readying callstack is ntkrnlmp.exe!KiTimerExpiration instead of the HIDUSB.SYS function. I guess all this indicates that the HIDUSB.SYS driver is querying devices asynchronously with a timeout of 5 seconds, and sometimes it fails and hits this timeout.
I don't know whether this failure is associated with any one device in particular (I do have a few USB HIDs) or if it's random - it's hard to test because it doesn't always happen. Again, any information anyone can give me will be appreciated, though I don't hold out any hope for Microsoft fixing this any time soon given the odd situation DirectInput is in!
Perhaps I'll just have to start initialising input earlier, asynchronously, and accept that sometimes there'll be a 5 second delay before user input can happen.
I was running into this too, largely as an end user, but it's been annoying the hell out of me for years. I didn't realize it was this issue until I ran into it on an open source project and was able to debug it.
Turns out it was my USB Headphone DAC (The Objective DAC from Massdrop), it installs the driver: wdma_usb.inf_amd64_134cb113911feba4\wdma_usb.inf for Device Instance ID USB\VID_262A&PID_1048&MI_01\7&F217D4F&0&0001 and then shows up in Device Manager under Sound, video and game controllers as: ODAC-revB USB DAC and, under Human Interface Devices as: USB Input Device and HID-compliant consumer control device.
I have no idea what the HID entries do but... When they are enabled and this DAC is set as the Audio Output device both IDirectInput8_CreateDevice and EnumDevices are painfully slow. Disabling the "USB Input Device" entry seems to cause no negative effects and completely solves my issue.
Changing the Audio output from the DAC to anything else also weirdly solved the issue.
This was so bad that it made the Gamepad Configuration dialog joy.cpl unusable, hanging and eventually crashing.
I was wanting this to just be a comment but I don't have enough rep for it, and this is pretty much the only place on the internet that describes this problem though so hopefully this helps someone else one day!
I had the same issue. I have a Corsair K65 LUX RGB keyboard. I updated CUE and it seems to have fixed the issue
Got same issue when having my Corsair K55 Keyboard. Changing the keyboard of USB port fixes the issue for a while, but then it comes back later on. So it seems to be a buggy drivers issue.
As DaFox has pointed out, a likely cause appears to be certain device drivers being enabled. I contacted JDS Labs support (who sell one device which happens to install one such driver) and they kindly pointed out that the root cause is actually a bug within Windows (not the installed driver), and they actually provide the solution on their troubleshooting page. See Games hang or experience loading delays, which explicitly mentions VID_262. Disabling this driver fixes the issue without apparent side effects (under the condition that that is the only driver triggering the bug). As for what exactly is going wrong within Windows, here there be dragons.
So I guess the go-to solution (for users) is to scrape all the troubleshooting and FAQ pages for all devices which you have ever connected to your system and see if there is a mention of delays/lag caused by a driver.
As a software developer, you will probably want to benchmark the execution time of the affected code and kindly tell the user there is something wrong with their system configuration and where to look for how to fix it in case it is unreasonably long.
Same issue with Corsair K70 Keyboard.
Quickly reconnecting keyboard fixes this, until next time. Usually happens after some DirectInput devices removed from the system or go to sleep.
This has been plaguing me as a developer and my friend as a user for years. All games using DInput, SDL SDL_INIT_JOYSTICK or anything depending on that, took extremely long to initialize.
It was caused by a faulty driver of a DAC, and as pointed out by DaFox, disabling the corresponding USB Input Device resolved the issue. Although it's labeled with a different manufacturer name, the vendor IDs match.
The hardware ID of the device is USB\VID_262A&PID_9023&REV_0001&MI_00.
Same issue appears to happen with a Steelseries Apex 7 keyboard. Unplugging and plugging that keyboard back again got rid of 3 freezes (of 10 seconds each) while enumerating USB devices.

How to close Error MessageBox generated at the start up of the device from other application using c++?

I have my application say test.exe which starts whenever i boot my mobile device.
I also kept dummy.log file at MyDevice\Windows\startup\ folder so whenevr i restart my device i see that error message box appears for dummy.log file.
I observed in the task manager that shell32 size gets increase whenever i boot my device with the above configuration.
And also observe that the shell32 size gets increase due to test.exe and whenever there is an error messagebox after booting the device.
If i close the error message box then shell32 size gets decreasing and test.exe doesnot create any problem.
Only Shell32 size gets increase in the above circumstances, in other cases it works fine.
I don't know how the error message box is linked with my test.exe application.
I debugged and checked and only option i get in my mind is to close the error message box from my application i.e. test.exe.
Edited Section:-
Could anyone let me know how to check & close the error message box from my application i.e. "test.exe"?
2nd time editing:-
Please also let me know how the error message box could be linked with my application i.e. "test.exe"? Is it anything related to timer? I am not doing anything for the outside windows in my application
Please reply thanks.
I think you can use FindWindow API and then send a WM_CLOSE
for more information read this Stackoverflow question
Get the handler to the proccess using this.
Send a mouse event /keystroke to the relvet area using this
Make the program to run on start-up using this
You can see where to send the message on the screen by using spy++ which is usually included in visual studio.

Monitoring open programs with Win32

I've searched the web, and various forums, but I can't find one thing, how to continually monitor open programs and then close another program (not the one being monitored) if something happens to the program being monitored.
For example, say that there is an already open Notepad window, and then the user or some other program opens a Windows Explorer window. I want to know how to close the Notepad window without interacting with the Windows Explorer window (other than realizing that it is indeed open), as well as closing the Notepad window if the user closes the Windows Explorer window.
Thanks in advance! :D
On windows, you can use PSAPI (The Process Status API) to know when processes are started and terminate. The EnumProcesses function can give you this information.
A more reliable method to determine that a process terminated (since process ids can be reused) is to wait on its process handle (you will need the SYNCHRONIZE access right) which you can obtain using OpenProcess and the process' id obtained from EnumProcesses.
To terminate a process, there is always TerminateProcess. To call TerminateProcess, you will need a handle to the process with the PROCESS_TERMINATE access right. All of this assumes that you have the privileges needed to perform these actions on the process to be terminated.
One thing to be aware of is that processes and programs - or at least what the user regards as a program - are not necessarily the same thing.
If you use the PSAPI to get a list of all the processes running, you'll see a lot of background process that don't correspond to open windows at all. There's also cases where a single process can have multiple top-level windows open. So while you have simple cases like notepad where once notepad.exe process corresponds to one Notepad window, you also have cases like:
Word, where one word process handles all the word documents currently open (one process, many windows)
Explorer, where a single exploere.exe process handles all the open explorer windows, and also things like control panel windows and the task bar.
Chrome (and other browsers), where each tab gets its own process (many processes, one window!)
Using TerminateProcess is perhaps not the best way to close an app: it's not directly equivalent to clicking the close button. It forcibly terminates the entire process there and then, without giving it any chance to clean up. If you do this on Word, when it restarts, it will go into 'recovery mode', and act as though it hadn't shut down cleanly the last time. It's best left as a last resort if a process has stopped responding. Also, if you TerminateProcess on a process like Word or Explorer, you'll end up closing all windows owned by that process, not just one specific one.
Given all of this, if you want to essentially write some sort of program manager, you might be better off taking a window-centric approach rather than a process centric one. Instead of monitoring running processes, monitor top-level application windows.
There are several ways to listen for changes to windows; SetWinEventHook with EVENT_CREATE/DESTROY is one way to listen for HWNDs being created or destroyed (you'll need to do filtering here, since it will tell you about all HWNDs - and more! - but you only care about top-level ones, and only app ones at that). SetWindowsHookEx may have other options that could work here (WH_CBT). You can also use EnumWindows to list the windows currently present (again, you'll need to filter out owned dialogs and tooltips, currently invisible HWNDs, etc).
Given a HWND, you can get process information if needed by using GetWindowThreadProcessId.
To close a window, sending WM_SYSCOMMAND/SC_CLOSE is the best thing to try first: this is closer to clicking the close button, and it gives the app a chance to clean up. Note that some apps will display a "are you sure you wish to close?" dialog if you haven't saved recently - again, it's consistent with clicking the close button with the mouse.
The most well-known way of doing this on Windows is to use the Process Status API. You can use this API to enumerate processes However, this API is annoying in that it doesn't guarantee you get the full list or processes.
A better way to enumerate processes is using the Tool Help Library, which includes a way to take a complete snapshot of all processes in the system at any given time.
You need the Microsoft PSAPI (Processes API), for example to see the open processes you can use the openProcess function.