Why is C++ WMI Not Fetching Data? Error: 0xC0000005 - c++

I'm quite new to WMI, I'm following this guide here.
So I pasted that code in the guide and ran it without any issues using the provided query. But when I changed the query to SELECT State FROM Win32_Service I get no data, just the error code 0xC0000005, which I believe is a permissions error?
That same query above works just fine in the PowerShell using the following command:
Get-WmiObject -Query "select State from Win32_Service"
What I've tried
winrm quickconfig
So what gives? Why am I having trouble querying the WMI from my C++ code?
EDIT: I found the line that's causing the crash. I'm not sure why it's crashing though.
wcout << vtProp.bstrVal << endl;

I missed this line of code
hr = pclsObj->Get(L"State", 0, &vtProp, 0, 0);
Change the first argument to the property whose value you'd like to retrieve.

Related

How to use the WinRM C++ API in a simple example

why is my code failing to run a simple executable using WinRM's C++ API?
//main.cpp
int main()
{
ShellClient *shellClient = new ShellClient();
//Set up the shell client here and connect to the localhost.
//This seems to be working fine because I'm handling every
//possible error code, and none of them are being triggered
PCWSTR commandLine = L"\"MyExampleExecutable.exe\"";
isOk = shellClient->RunCommand(commandLine);
if (!isOk)
return 1;
return 0;
}
//ShellClient.cpp
bool ShellClient::RunCommand(PCWSTR command)
{
WSMAN_SHELL_ASYNC createCommandAsync;
ZeroMemory(&createCommandAsync, sizeof(createCommandAsync));
createCommandAsync.operationContext = this;
createCommandAsync.completionFunction = (WSMAN_SHELL_COMPLETION_FUNCTION)CommandCreatedCallback;
WSManRunShellCommand(shellHandle, 0, command, NULL, NULL, &createCommandAsync, &commandHandle);
if (commandHandle == NULL)//It is *always* NULL
{
std::cout << "command handle null" << std::endl;
system("pause");
return false;
}
return true;
}
One possible clue is that my C++ code thinks the shell gets created fine, but in the Event Viewer for my machine, there is this:
WSMan operation CreateShell failed, error code 2150859250
At the time of writing, this lovely error code gives precisely zero results when put into Google, making it rather difficult to know what it means.
Background and common solutions which I have already checked
As documented here and explaned in this video by the same author, most WinRM issues boil down to either connection or authentication problems. In my case, if I deliberately enter incorrect user credentials, I get an authentication error, so I know that my program is connecting and authenticating fine when the correct username and password are supplied. Also:
From the command line, I can connect to my local machine and pretend it's a remote server, for example the following command works fine:
winrs -r:http://localhost:5985 -u:COMPUTERNAME\Jeremy "dir"
winrm quickconfig shows the service is working (which we already know otherwise the winrs command wouldn't work)
winrm get winrm/config shows TrustedHosts = localhost, AllowUnencrypted = true, and all authentication methods are set to true
Following this advice, I have set the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\LocalAccountTokenFilterPolicy = 1
Working in Windows 10
Thank you in advance!
I wasn't aware of this so can't comment on whether it's common knowledge but Microsoft have a nifty error lookup tool where you can enter an error code (after converting it from a normal number to hexadecimal) and it tells you what it means.
In this case, 2150859250 (803381F2 in hex) corresponds to:
ERROR_WINRS_IDLETIMEOUT_OUTOFBOUNDS wsmerror.h
# The WS-Management service cannot process the request. The
# requested IdleTimeout is outside the allowed range.
When setting up the WinRM shell, I was doing the following:
WSMAN_SHELL_STARTUP_INFO startupInfo;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.idleTimeoutMs = 1000;//not large enough!
startupInfo.workingDirectory = L"C:\\";
//other parameters of startupInfo set here
WSManCreateShell(session, 0, shellUri, &startupInfo, NULL, NULL, &createShellAsync, &shellHandle);
Changing idleTimeoutMs from 1000 to a much larger number like 100000 solved the error and my program now works fine.
Since the official docs for this parameter say anything between 0 and 0xFFFFFFFF are valid, it remains a mystery why a value of 1000 is throwing this error. I leave this for somebody more knowledgable than myself to answer, on the off chance that they come across this question.

"Not Found" error occurred while trying to fetch "NumberOfEnabledCore" value from "Win32_Processor" class of WMI

I am trying to extract "NumberOfEnabledCore" value from "Win32_Processor" class of WMI. When I have executed "Get-WmiObject -class Win32_Processor | Select *" in Powershell in Windows 8.1, I am getting error as "Not Found".
However, when I ran the same script in Windows 10, I am getting value for "NumberOfEnabledCore".
Can someone please assist on this WMI script behavior?
Thanks.
Ref: https://msdn.microsoft.com/en-us/library/aa394373(v=vs.85).aspx

C++ Sqlite3 code seems to have issues prepping a statement

I was trying to just query some input to make sure things were inserted correctly.
I was doing the following:
//open database into 'db'
sqlite3_stmt *statement;
string query = "select * from A;";
if(sqlite3_prepare_v2(db, query.c_str(), -1, &statement, 0)==SQLITE_OK){
//...
}else{
cout << "Error prepping statement" << endl;
}
I was following a demo from dreamincode: http://www.dreamincode.net/forums/topic/122300-sqlite-in-c/ I just have no idea what i was doing wrong. Am i using the wrong prepare method, or defining something wrong?
Edit: The Returned Error Code is 1: SQL error or missing database. Am i doing the SQL statement wrong? I triple checked to make sure the DB was indeed open
Edit: RESOLVED I downloaded the database, opened it with a sqlite3 reader... and noticed something i SHOULDve been told about. The schema changed without me knowing without documentation being sent to the entire dev team. I am sending a very nice email to my coworker about that
Schema was changed without documentation or telling anyone on the dev team. I had to dl the db and open it with an sqlite3 reader.
Im going to have words with him about this.

Getting error "The owner SID on a per-user subscription doesn't exist " in DXVA2 but not in DXVAHD while calling ConnectDirect() MSFT API.

I am getting the error "The owner SID on a per-user subscription doesn't exist " in the return value "hr" when I call:
hr = pGraph->ConnectDirect(pOut, pIn, NULL);
I get this error only when I use DXVA2 but it returns S_OK when DXVAHD is used.
Can anyone kindly let me know the reaosn for this.
I suspect the the 3rd argument that I have given for "ConnectDirect()" i.e Media Type is NULL. I must have given some other type is it?
The pOut & pIn for the 2 filter are correct as I could observe from the S_OK return value while getting the pin values.
Since ConnectDirect() is a Microsoft API, I am not able to step into the code tooi order to do furtehr debug.
Can anyone kindly help me in getting this fixed?
Thanks in advance.
The error you are getting is 0x80040207 "There is no common media type between these pins.", VFW_E_NO_ACCEPTABLE_TYPES. There is something in your graph building done incorrectly.

How to get the Last Active Date of a Process?

I have an assignment were in I have to print the last active date of the process using a COM In Proc Server in C++. I tried doing that with getProcessTimes() function, but that gives me an access violation error.
First of all, I want to know if there is anyother command that gives the last active date of the process..
Second what is the problem with the following code
FILETIME ftCreation, ftKernel, ftUser;
GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser);`
I tried memsetting and several other alternatives but nothing works...
Here is an article that demonstrates how to use GetProcessTimes. It includes sample code. Another option is using WMI and the WIN32_Process class, which also has this information. Here is an example of how you would use WMI.