Invalid query on "Win32_PerfFormattedData_PerfOS_Processor" - windows

My software (with admin priviledges) periodically performs the following query via WMI to know the status of the CPU:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfOS_Processor");
ManagementObjectCollection collection = searcher.Get();
Suddenly (meaning after months where the result of searcher.Get() was always available) the command started sitting down and throwing an "Invalid query" Exception after a timeout. I cannot say what's changed in the machine before this happens.
I confirmed this error by testing it with tool wbemtest:
The error happens everytime, even after reboot. How can I debug it?
System is Windows 10 x64 IoT Enterprise v1607.

The solution is explained in this article.
Anyway, I still have not understood the reason why the counters may get corrupted. Any comment on this?

I ran into this issue on a Windows 7 machine separate from a network and required to be kept around for various reasons.
At first I thought it was an admin thing (the program runs as a normal user) because system event viewer error 2011 hints at that.
However, it's fixed with this:
lodctr /r
I don't fully understand why, but this article was my source:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/4d574e10-17f5-4599-95d6-2492ede3cfef/wmi-query-from-net-application-cause-2011-error-event?forum=netfxbcl

Related

A program caused the program to stop working correctly

I've developed a software using National Instruments LabWindows/CVI and installed the .exe in a Windows 7 32 bit PC with 4GB RAM.
When I run my software, sometimes I get the following error.
"A program caused the program to stop working correctly. Windows will close the program and notify you if a solution is available"
This is very random and sometimes this error never comes.
Can anyone help me to understand this issue please. I've reviewed my software code many times and I am sure that I am not doing anything wrong in the software which causes this error to come up.
Is this anything related to windows and how can I resolve this? Help is much appreciated.
Thanks
Sujith Rajan
I have encountered similar problems several times.
This may happen even with simple programs like a console application used to get input from user and display some data on screen after processing it.
Usually, this is an indication that your computer is unable to provide enough resources to this program or that there is a bug in your code.
It might be random due to the following reasons:
The Processor might already be busy with several demanding tasks and your program needs to be closed due to this. At other times, when it works well, the resources might be available.
Your program might have a certain logical error that appears at runtime only when certain conditions are fulfilled. (such as an erroneous conditional statement)
Your program might have an infinite loop.
Windows suspects your file to be harmful to the system (for some reason).
There are youtube videos that tell you to go set DATA EXECUTION PROTECTION to resolve. This is a red herring. Its also potentially harmful, especially if you are running old dos apps (because you have to for some reason).
If the program throws a unhandled exception , of any sort , you will get this error message.
If you launched it with this code paragraph...
Dim psi As New ProcessStartInfo(pathToTarget)
Dim p As Process = Process.Start(psi)
Dim bIfinished As Boolean = p.WaitForExit(itimeout)
If bIfinished = False Then
p.Kill()
End If
iretVal = p.ExitCode
pathToTarget is the full path to your target exe/bat (TARGET) file
timeout is an integer that represents milliseconds. 2 minutes would be 2*60*1000
bfinished will be true if the program ended by itself. NOTE- this is not the return code. If it failed to finish in (2 minutes in this example) bFinished will be false.
p.ExitCode can be checked to see what the TARGET returned. Typically a 0 is success and anything else is an error code.
This is the message box mentioned by OP, (autoAging happens to be the exe I used to demonstrate this) . It also says 'XYZ has stopped working'. Google needs to know that!
Note that code will continue running in YOUR app so you can do clean up if you like. Clicking or not clicking "Close Program" has no effect on HOST that I have been able to tell.
If you own the code to the TARGET, make sure you handle all errors and return an appropriate code. That way your calling app (HOST) can know how to react.
You also avoid this msgbox.
If you don't own the code to TARGET, you just have to do the best you can. If there is some output you can readily check, do that. Otherwise I'd assume failure and proceed on that assumption.
This message box does consume resources. Although its not a huge issue, enough of them will run your box out of memory.

Killing processes on Windows 7

I'm debugging plugins on Windows 7 and of course the plugin host (Cubase5.exe) occasionally crashes because of errors in the plugin. On XP or Vista, I could always restart it immediately and continue working. But on Windows 7, even though Cubase appears to close, it is still visible in Task Manager and I cannot kill it by any means. After a minute or two, it disappears by itself. In the mean time, I can't work because the plugin DLL is still locked by the process.
Does anyone know why this happens on Windows 7? I've already tried disabling Automatic Error Reporting but that didn't help. I've tried attaching cdb to Cubase, but I get:
Cannot debug pid 5252, NTSTATUS 0xC0000001
"{Operation Failed} The requested operation was unsuccessful."
Debuggee initialization failed, NTSTATUS 0xC0000001
"{Operation Failed} The requested operation was unsuccessful."
I tried following the instructions here but it appears this is only possible if I connect a second machine to my computer to debug it remotely.
I finally found the solution, using this article:
http://blogs.technet.com/b/markrussinovich/archive/2005/08/17/unkillable-processes.aspx
This required installing the Windows Debugging Tools for Windows (nice name) and LiveKd, but by following the steps outlined I was able to track which driver was causing the process to hang: it turned out to be the 64-bit driver for the M-Audio Oxygen 8 V2 controller I'm using. Unfortunately no driver update is available.
Anyway, if anyone encounters a similar problem, this is the way to solve it.
Have you tried Process Explorer by Mark Russinovich? It is really useful for "killing":)
If you have error reporting enabled, it's possible that werfault.exe has Cubase open to write a minidump for crash reporting purposes.
This is just a stab in the dark but it might be your problem.
One thing you can try is to check with Process Monitor what Cubase is doing. Set a filter so that everything with a process name containing "cubase" will be recorded. It could be that you are facing some timeout issue when Cubase wants to exit.
you can end the process the service is running under. You can find this process by going to the Services tab of the Task Manager, right-clicking, and selecting Go To Process(you need to click the Show processes from all users button.). Note that one process may host multiple services (especially if it's svchost.exe), and ending the process will kill all those services. Also, this is an unclean exit, and may cause data corruption depending on what the service(s) was doing when you killed it.
Depending on which specific service you are trying to stop, there may be a cleaner way to simulate failure.

Detect/Redirect core dumps (when a software crashes) on Windows

For my work, I need to create a service that will detect an abnormal program termination and, instead of displaying a message to the user (default behavior), send the generated core dump to a remote server.
I'm pretty sure this can be done, but I have absolutely no clue on where to start. Is there any API/registry settings for this ?
Thank you.
One method is to install an Unhandled Exception Filter and then write a minidump from it which you can then upload to some place of your choosing. I wouldn't totally disregard Windows Error Reporting -- that's an addition to any crash reporting of your own. If your application is for public release then registering for Windows Error Reporting is well worthwhile as you get information about which crashes users are encountering in the wild and when crashes have been fixed you can add a response code to point them to a new version or other relevant information.
Another tool that may be useful depending on how your application is deployed in your organisation is to run Adplus on a user's machine which will collect together crash dumps. This is more useful for one-off crashes that seem to affect an individual user but aren't reproducible in a development environment.
Some other useful links:
http://www.debuginfo.com/articles/effminidumps.html - some useful sample code
http://www.codeproject.com/KB/debug/postmortemdebug_standalone1.aspx
It seems my question was either obvious or stupid (both ?) but anyway, i found some interesting pages after some researches.
Here are the links I found useful:
Track application crashes and disable Windows Error Reporting at the same time!
Disable error reporting

Identify a reboot

Is there any "Boot session ID" or (reliable) "Boot timestamp"?
For an installation I need to detect that a scheduled reboot took place indeed.
I guess I could do a dummy MoveFileEx() with MOVEFILE_DELAY_UNTIL_REBOOT, but i did hope for something easier.
(We have to install a 3rd party package that sometimes behaves erratically after an repair/update. In that state, accessing the device may even lock up the system)
(Windows XP, Vista, 7)
For things like this, WMI (Windows Management Instrumentation) is often a good starting place. I know you can get current uptime directly through it, which may allow you to determine if a machine recently rebooted.
Here is a blog post with some code samples as well:
http://blogs.technet.com/heyscriptingguy/archive/2004/09/07/how-can-i-tell-if-a-server-has-rebooted.aspx
Depending on your implementation language, you probably just want to pull out the query code from the vbscript.
Apparently Windows has the equivalent of "uptime". Here's more info: http://support.microsoft.com/kb/555737
As I understand it, this should tell you how long ago the system was booted. Will that information solve your problem?
You could search the System event log for event 6009 from the EventLog source - this is the first event recorded after each reboot.
I think the best answer has already been given here: Find out if computer rebooted since the last time my program ran?
That seems to be the simplest way. Use GlobalFindAtom() to see if it exists and create it, with GlobalAddAtom(), if it doesn't. It will persist beyond the execution of your program. If your application runs again, and sees that the atom exists, then then it isn't the first run since reboot.
If the computer is restarted, then the atom won't exist, indicating that this is the first run of your program since the reboot.

Terminating intermittently

Has anyone had and solved a problem where programs would terminate without any indication of why? I encounter this problem about every 6 months and I can get it to stop by having me (the administrator) log-in then out of the machine. After this things are back to normal for the next 6 months. I've seen this on Windows XP and Windows 2000 machines.
I've looked in the Event Viewer and monitored API calls and I cannot see anything out of the ordinary.
UPDATE: On the Windows 2000 machine, Visual Basic 6 would terminate when loading a project. On the Windows XP machine, IIS stopped working until I logged in then out.
UPDATE: Restarting the machine doesn't work.
Perhaps it's not solved by you logging in, but by the user logging out. It could be a memory leak and logging out closes the process, causing windows to reclaim the memory. I assume programs indicated multiple applications, so it could be a shared dll that's causing the problem. Is there any kind of similarities in the programs? .Net, VB6, Office, and so on, or is it everything on the computer? You may be able to narrow it down to shared libraries.
During the 6 month "no error" time frame, is the system always on and logged in? If that's the case, you may suggest the user periodically reboot, perhaps once a week, in order to reclaim leaked memory, or memory claimed by hanging programs that didn't close properly.
You need to take this issue to the software developer.
The more details you provide the more likely it will be that you will get an answer: explain what exact program was 'terminating'. A termination is usually caused by an internal unhandled error, and not all programs check for them, and log them before quitting. However I think you can install Dr Watson, and it will give you at least a stack trace when a crash happens.

Resources