I want to use the Wininet function InternetCheckConnection to check whenever the machine is connected to the internet and can access a specific host.
The problem is that this function is always returning false, no matter the URL I put on it.
MSDN link
Following combination works for me on Windows 7 and Windows XP SP3:
InternetCheckConnection("http://www.google.com", FLAG_ICC_FORCE_CONNECTION, 0) ;
GetLastError() returns 0 if the connexion is possible and it returns
12029 (The attempt to connect to the server failed) if not.
Following combonations don't work for me :
InternetCheckConnection(NULL, FLAG_ICC_FORCE_CONNECTION, 0) ;
GetLastError() returns 12016 (The requested operation is invalid).
InternetCheckConnection(NULL, 0, 0) ;
InternetCheckConnection(("http://www.google.com", 0, 0) ;
for both GetLastError() returns 2250 (The network connection could not be found).
Have you checked GetLastError() ? If I read MSDN correctly, you would need to check for ERROR_NOT_CONNECTED to determine whether you're truly offline.
Just a wild guess, but perhaps this is due to a personal firewall blocking all outgoing connections for one of the Windows DLLs and / or your application?
According to Microsoft API document InternetCheckConnection is deprecated.
[InternetCheckConnection is available for use in the operating systems specified in the Requirements section. It may be altered or unavailable in subsequent versions. Instead, use NetworkInformation.GetInternetConnectionProfile or the NLM Interfaces. ]
Instead of this API we can use INetworkListManager interface to check whether Internet is connected or not for windows platform.
Here below is the win32 codebase :
#include <iostream>
#include <ObjBase.h> // include the base COM header
#include <Netlistmgr.h>
// Instruct linker to link to the required COM libraries
#pragma comment(lib, "ole32.lib")
using namespace std;
enum class INTERNET_STATUS
{
CONNECTED,
DISCONNECTED,
CONNECTED_TO_LOCAL,
CONNECTION_ERROR
};
INTERNET_STATUS IsConnectedToInternet();
int main()
{
INTERNET_STATUS connectedStatus = INTERNET_STATUS::CONNECTION_ERROR;
connectedStatus = IsConnectedToInternet();
switch (connectedStatus)
{
case INTERNET_STATUS::CONNECTED:
cout << "Connected to the internet" << endl;
break;
case INTERNET_STATUS::DISCONNECTED:
cout << "Internet is not available" << endl;
break;
case INTERNET_STATUS::CONNECTED_TO_LOCAL:
cout << "Connected to the local network." << endl;
break;
case INTERNET_STATUS::CONNECTION_ERROR:
default:
cout << "Unknown error has been occurred." << endl;
break;
}
}
INTERNET_STATUS IsConnectedToInternet()
{
INTERNET_STATUS connectedStatus = INTERNET_STATUS::CONNECTION_ERROR;
HRESULT hr = S_FALSE;
try
{
hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
INetworkListManager* pNetworkListManager;
hr = CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, __uuidof(INetworkListManager), (LPVOID*)&pNetworkListManager);
if (SUCCEEDED(hr))
{
NLM_CONNECTIVITY nlmConnectivity = NLM_CONNECTIVITY::NLM_CONNECTIVITY_DISCONNECTED;
VARIANT_BOOL isConnected = VARIANT_FALSE;
hr = pNetworkListManager->get_IsConnectedToInternet(&isConnected);
if (SUCCEEDED(hr))
{
if (isConnected == VARIANT_TRUE)
connectedStatus = INTERNET_STATUS::CONNECTED;
else
connectedStatus = INTERNET_STATUS::DISCONNECTED;
}
if (isConnected == VARIANT_FALSE && SUCCEEDED(pNetworkListManager->GetConnectivity(&nlmConnectivity)))
{
if (nlmConnectivity & (NLM_CONNECTIVITY_IPV4_LOCALNETWORK | NLM_CONNECTIVITY_IPV4_SUBNET | NLM_CONNECTIVITY_IPV6_LOCALNETWORK | NLM_CONNECTIVITY_IPV6_SUBNET))
{
connectedStatus = INTERNET_STATUS::CONNECTED_TO_LOCAL;
}
}
pNetworkListManager->Release();
}
}
CoUninitialize();
}
catch (...)
{
connectedStatus = INTERNET_STATUS::CONNECTION_ERROR;
}
return connectedStatus;
}
Related
I am updating my question to better reflect what I was actually going after. To state a fact about my original confusion quickly, it is incorrect to say that there is a 1-to-1 relationship between "Device Interface Class GUID" and the Device Instance ID. A device can have many device interfaces. As Ben Voigt noted in the comments, see this for more information.
How can one open a handle to a child device after calling the CM_Get_Child (...) function?
Take the following code snip as an example:
#pragma comment (lib, "Setupapi.lib")
#pragma comment (lib, "Cfgmgr32.lib")
#include <iostream>
#include <Windows.h>
#include <Setupapi.h>
#include <Cfgmgr32.h>
#define GUID_STRING_SIZE 40
int main ()
{
CONFIGRET CMResult = CR_SUCCESS;
WCHAR DeviceInstanceID[] = L"USB\\VID_2109&PID_0813\\8&216C1825&0&4\0"; // Parent Device Instance ID.
DEVNODE ParentDeviceNode = (DWORD) 0; // A device instance handle. This handle is bounded to the local machine.
CMResult = CM_Locate_DevNode ((PDEVINST) &ParentDeviceNode, DeviceInstanceID, CM_LOCATE_DEVNODE_NORMAL);
if (CMResult != CR_SUCCESS)
{
std::cout << "No parent device node found." << std::endl;
return -1;
}
else
{
DEVINST NextChildDeviceNode = (DWORD) 0;
CMResult = CM_Get_Child ((PDEVINST) &NextChildDeviceNode, ParentDeviceNode, 0x0); // Gets the first child of the parent node. If this returns "CR_NO_SUCH_DEVNODE," then there is no child attached.
if (CMResult != CR_SUCCESS)
{
std::cout << "No child device node found." << std::endl;
return -2;
}
else
{
ULONG ChildInstanceIDBuffLength = 0;
CMResult = CM_Get_Device_ID_Size (&ChildInstanceIDBuffLength, NextChildDeviceNode, 0x0);
if (CMResult != CR_SUCCESS)
{
std::cout << "Could not get the size of the device instance ID of child device." << std::endl;
return -3;
}
else
{
WCHAR * ChildInstanceIDBuff = (WCHAR *) malloc (ChildInstanceIDBuffLength);
CMResult = CM_Get_Device_IDW (NextChildDeviceNode, ChildInstanceIDBuff, ChildInstanceIDBuffLength, 0x0);
if (CMResult != CR_SUCCESS)
{
std::cout << "Could not actual device instance ID string of child device" << std::endl;
return -4;
}
else
{
std::cout << "Found child device instance ID: ";
std::wcout << ChildInstanceIDBuff << std::endl;
/*
* Open handle to the child device node now!
*/
}
free (ChildInstanceIDBuff);
}
}
}
return 0;
}
How can I use the newly obtained child Device Instance ID to open a handle to the device? CreateFile (...) requires the complete device path, which includes the missing "Device Interface Class GUID."
More specifically, a device path has the following format:
\\?\usb#vid_2109&pid_0813#7&3981C8D6&0&2#{[DEVICE_INTERFACE_GUID]}, where:
[DEVICE_INTERFACE_GUID] - This the "Device Interface Class GUID." This is NOT the same as the "Device Setup Class GUID."
There does not appear to be an easy way to get this "Device Interface Class GUID" without some level of brute force (e.g. CM_Enumerate_Classes (...) using the CM_ENUMERATE_CLASSES_INTERFACE flag). Is there a function I can call to get a handle to a device using only its "Device Instance ID," so that I can then call DeviceIoControl (...) and query information about the device?
if we need open handle to device by Device Instance ID -
first call CM_Locate_DevNodeW function for obtains a device
instance handle to the device node that is associated with a
specified device instance ID on the local machine.
then we need call CM_Get_DevNode_PropertyW function with
DEVPKEY_Device_PDOName - this return name of the physical name object (PDO) that represents a device and we can use it in call NtOpenFile. of course if very want - can use and in call CreateFileW if add L"\\\\?\\Global\\GLOBALROOT" to name, but not view any sense do this.
volatile UCHAR guz = 0;
ULONG OpenDeviceByDeviceID(_Out_ PHANDLE FileHandle, _In_ PWSTR DeviceID)
{
DEVINST dnDevInst;
CONFIGRET CmReturnCode = CM_Locate_DevNodeW(&dnDevInst, DeviceID, CM_LOCATE_DEVNODE_NORMAL);
if (CmReturnCode == CR_SUCCESS)
{
ULONG cb = 0, rcb = 128;
PVOID stack = alloca(guz);
DEVPROPTYPE PropertyType;
union {
PVOID pv;
PWSTR sz;
PBYTE pb;
};
do
{
if (cb < rcb)
{
rcb = cb = RtlPointerToOffset(pv = alloca(rcb - cb), stack);
}
CmReturnCode = CM_Get_DevNode_PropertyW(dnDevInst,
&DEVPKEY_Device_PDOName, &PropertyType, pb, &rcb, 0);
if (CmReturnCode == CR_SUCCESS)
{
if (PropertyType == DEVPROP_TYPE_STRING)
{
DbgPrint("PDOName = %S\n", sz);
#if 1
IO_STATUS_BLOCK iosb;
UNICODE_STRING ObjectName;
OBJECT_ATTRIBUTES oa = { sizeof(oa), 0, &ObjectName };
RtlInitUnicodeString(&ObjectName, sz);
NTSTATUS status = NtOpenFile(FileHandle,
FILE_GENERIC_READ, &oa, &iosb, 0, 0);
return 0 > status ? RtlNtStatusToDosError(status) : NOERROR;
#else
static WCHAR prefix[] = L"\\\\?\\Global\\GLOBALROOT";
alloca(sizeof(prefix) - sizeof(WCHAR));
PWSTR fileName = sz - _countof(prefix) + 1;
memcpy(fileName, prefix, sizeof(prefix) - sizeof(WCHAR));
HANDLE hFile = CreateFileW(fileName, FILE_GENERIC_READ,
0, 0, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE)
{
return GetLastError();
}
*FileHandle = hFile;
return NOERROR;
#endif
}
else
{
CmReturnCode = CR_WRONG_TYPE;
}
}
} while (CmReturnCode == CR_BUFFER_SMALL);
}
return CM_MapCrToWin32Err(CmReturnCode, 0);
}
You can use the CM_Enumerate_Classes function with the CM_ENUMERATE_CLASSES_INTERFACE flag (Requires Windows 8) to get possible values to pass as that third parameter of SetupDiEnumDeviceInterfaces.
Beginning with Windows 8 and later operating systems, callers can use the ulFlags member to specify which device classes CM_Enumerate_Classes should return. Prior to Windows 8, CM_Enumerate_Classes returned only device setup classes.
Note that finding all interfaces on a device is very useful for diagnosing driver problems and/or reverse engineering random peripherals rescued from scrap. But you should know what interface class you are dealing with long before you get to the point of calling CreateFile.
My friend asked me to write a simple program that captures a fingerprint from the built-in reader on a computer and prints out some kind of identifier. I can choose operating system myself, like a laptop with Windows or Linux, or an Android phone.
I thought that would be simple, surely there are many API:s for this, and I noticed that Microsoft themselves actually provides an API for it. Since I can log into my win10 laptop with the fingerprint reader, I know that the reader works.
For some reason the example that Microsoft themselves provide in their documention does not work for me. https://learn.microsoft.com/en-us/windows/desktop/api/Winbio/nf-winbio-winbiocapturesample
I suppose that the people writing those pages forgot to mention some important aspect or step, perhaps there is a way to add permission somewhere in Visual Studio.
After rewriting and trying many things, at least I get ONE step further in the process, but it still fails.
Here is the current version
#include "pch.h"
#include <iostream>
#include "Windows.h"
#include "Stdio.h"
#include "Conio.h"
#include "Winbio.h"
HRESULT CaptureSample();
void capture(WINBIO_SESSION_HANDLE sessionHandle, int flag);
int main()
{
std::cout << "Hello World!\n";
HRESULT x = CaptureSample();
}
HRESULT CaptureSample()
{
HRESULT hr = S_OK;
WINBIO_SESSION_HANDLE sessionHandle = NULL;
WINBIO_REJECT_DETAIL rejectDetail = 0;
// Connect to the system pool.
hr = WinBioOpenSession(
WINBIO_TYPE_FINGERPRINT, // Service provider
WINBIO_POOL_SYSTEM, // Pool type
WINBIO_FLAG_DEFAULT, // Access: Capture raw data
NULL, // Array of biometric unit IDs
0, // Count of biometric unit IDs
WINBIO_DB_DEFAULT, // Default database
&sessionHandle // [out] Session handle
);
if (FAILED(hr))
{
wprintf_s(L"WinBioOpenSession failed. hr = 0x%x\n", hr);
goto e_Exit;
}
wprintf_s(L"Start my fingerprint capturing...\n");
capture(sessionHandle, WINBIO_DATA_FLAG_INTEGRITY);
capture(sessionHandle, WINBIO_DATA_FLAG_PRIVACY);
capture(sessionHandle, WINBIO_DATA_FLAG_SIGNED);
capture(sessionHandle, WINBIO_DATA_FLAG_OPTION_MASK_PRESENT);
capture(sessionHandle, WINBIO_DATA_FLAG_RAW);
capture(sessionHandle, WINBIO_DATA_FLAG_INTERMEDIATE);
capture(sessionHandle, WINBIO_DATA_FLAG_PROCESSED);
hr = WinBioEnrollCapture(sessionHandle, &rejectDetail);
wprintf_s(L"WinBioEnrollCapture hr=%x rejection = %d\n", hr, rejectDetail);
if (sessionHandle != NULL)
{
WinBioCloseSession(sessionHandle);
sessionHandle = NULL;
}
e_Exit:
wprintf_s(L"\n Press any key to exit...");
_getch();
return hr;
}
void capture(WINBIO_SESSION_HANDLE sessionHandle, int flag) {
WINBIO_UNIT_ID unitId = 0;
WINBIO_REJECT_DETAIL rejectDetail = 0;
PWINBIO_BIR sample = NULL;
SIZE_T sampleSize = 0;
wprintf_s(L"\n Calling WinBioCaptureSample. Flag = %d.\n", flag);
HRESULT hr = WinBioCaptureSample(
sessionHandle,
WINBIO_PURPOSE_IDENTIFY,
flag,
&unitId,
&sample,
&sampleSize,
&rejectDetail
);
if (FAILED(hr))
{
if (hr == WINBIO_E_BAD_CAPTURE)
{
wprintf_s(L"\n Bad capture; reason: %d\n", rejectDetail);
}
else if (hr == E_ACCESSDENIED)
{
wprintf_s(L"\n WinBioCaptureSample failed, access denied.");
}
else
{
wprintf_s(L"\n WinBioCaptureSample failed. hr = 0x%x\n", hr);
}
goto e_Exit;
}
wprintf_s(L"\n Swipe processed - Unit ID: %d\n", unitId);
wprintf_s(L"\n Captured %d bytes.\n", sampleSize);
e_Exit:
if (sample != NULL)
{
WinBioFree(sample);
sample = NULL;
}
}
and here is the result of running it:
Hello World!
Start my fingerprint capturing...
Calling WinBioCaptureSample. Flag = 1.
WinBioCaptureSample failed, access denied.
Calling WinBioCaptureSample. Flag = 2.
WinBioCaptureSample failed, access denied.
Calling WinBioCaptureSample. Flag = 4.
WinBioCaptureSample failed, access denied.
Calling WinBioCaptureSample. Flag = 8.
WinBioCaptureSample failed, access denied.
Calling WinBioCaptureSample. Flag = 32.
WinBioCaptureSample failed, access denied.
Calling WinBioCaptureSample. Flag = 64.
WinBioCaptureSample failed, access denied.
Calling WinBioCaptureSample. Flag = 128.
WinBioCaptureSample failed, access denied.WinBioEnrollCapture hr=8009802c rejection = 0
Press any key to exit...
What am I missing?
I think you should try using another API, though your objective is not clear here. You can check the CloudABIS™ which is superscalar, biometrics-as-a-service (BaaS) matching system for fast deployment at lower costs. The API is provided by M2SYS TEchnology and they have an vast array of hardware also.
I'm totally new to networking and just started to use Cap'n Proto too.
This is some sample program from here:
void writeAddressBook(int fd) {
::capnp::MallocMessageBuilder message;
AddressBook::Builder addressBook = message.initRoot<AddressBook>();
::capnp::List<Person>::Builder people = addressBook.initPeople(2);
Person::Builder alice = people[0];
alice.setId(123);
alice.setName("Alice");
alice.setEmail("alice#example.com");
// Type shown for explanation purposes; normally you'd use auto.
::capnp::List<Person::PhoneNumber>::Builder alicePhones =
alice.initPhones(1);
alicePhones[0].setNumber("555-1212");
alicePhones[0].setType(Person::PhoneNumber::Type::MOBILE);
alice.getEmployment().setSchool("MIT");
Person::Builder bob = people[1];
bob.setId(456);
bob.setName("Bob");
bob.setEmail("bob#example.com");
auto bobPhones = bob.initPhones(2);
bobPhones[0].setNumber("555-4567");
bobPhones[0].setType(Person::PhoneNumber::Type::HOME);
bobPhones[1].setNumber("555-7654");
bobPhones[1].setType(Person::PhoneNumber::Type::WORK);
bob.getEmployment().setUnemployed();
writePackedMessageToFd(fd, message);
}
The last line uses writePackedMessageToFd() which takes fd as a file descriptor and message created by MallocMessageBuilder.
I work on Windows with Visual Studio 2017.
I would like to send message to a remote server which will answer with a similar Cap'nP object.
The question is how can I send it and receive the answer?
I tried to initialize and create a socket in the following way:
//Initialize WinSock
WSAData data;
WORD ver = MAKEWORD(2, 2);
int wsResult = WSAStartup(ver, &data);
if (wsResult != 0) {
cerr << "Can't start WinSock, Error #" << wsResult << endl;
return;
}
else {
cout << "Socket initialized!" << endl;
}
//Create socket
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
cerr << "Can't create socket, Error #" << WSAGetLastError() << endl;
WSACleanup();
return;
}
else {
cout << "Socket created!" << endl;
}
If everything went fine socket() return a file descriptor.
So I just used
writePackedMessageToFd(sock, message), but didn't work.
By the way I don't understand this concept since the socket doesn't "know" which IP and port I want to use. I should specify them when I use the connect() function.
I tried to skip the writePackedMessageToFd() function. Connected to the server with connect() and just used Windows' send() function to send the message. Something like this:
string ipAddress = "127.0.0.1"; //IP Address of server
int port = 58661; //Listening port of server
//Initialize WinSock
WSAData data;
WORD ver = MAKEWORD(2, 2);
int wsResult = WSAStartup(ver, &data);
if (wsResult != 0) {
cerr << "Can't start WinSock, Error #" << wsResult << endl;
return;
}
else {
cout << "Socket initialized!" << endl;
}
//Create socket
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
cerr << "Can't create socket, Error #" << WSAGetLastError() << endl;
WSACleanup();
return;
}
else {
cout << "Socket created!" << endl;
}
//Fill in a hint structure
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(port);
inet_pton(AF_INET, ipAddress.c_str(), &hint.sin_addr);
//Connect to server
int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
if (connResult == SOCKET_ERROR) {
cerr << "Can't connect to server, Error #" << WSAGetLastError() << endl;
closesocket(sock);
WSACleanup();
return;
}
else {
cout << "Connected to " << ipAddress << " on port " << port << endl;
}
//Send and receive data
char buf[4096];
//string mess = "Hello";
//Send message
//int sendResult = send(sock, mess.c_str(), mess.size() + 1, 0);
int sendResult = send(sock, (const char*)&message, sizeof(message) + 1, 0);
//Wait for response
ZeroMemory(buf, 4096);
int bytesReceived = recv(sock, buf, 4096, 0);
if (bytesReceived > 0) {
cout << "SERVER>" << string(buf, 0, bytesReceived) << endl;
}
//Close down everything
closesocket(sock);
WSACleanup();
cout << "Connection closed!" << endl;
This one sended something but it was definitely wrong because the server didn't respond.
In brief: I would like to send and receive Cap'n Proto packed messages over TCP connection to a specified IP:port.
How could I accomplish this? I really need some help.
I would really really appreciate Your help! Thanks in advance!
On Windows, socket() does not return a file descriptor. It returns a Windows SOCKET, which is actually a HANDLE cast to an integer. On Windows, "file descriptors" are implemented as a compatibility layer in the C runtime library; they are not directly supported by the OS.
You can use kj::HandleInputStream and kj::HandleOutputStream to perform I/O on sockets on Windows.
kj::HandleOutputStream out((HANDLE)sock);
capnp::writeMessage(out, message);
And:
kj::HandleInputStream in((HANDLE)sock);
capnp::InputStreamMessageReader message(in);
On Windows, one can get any special folder path using SHGetKnownFolderPath or SHGetSpecialFolder (If I remember correctly this last one). However, I want the reverse, I have a path and want to know which special folder it belongs to, if any. I prefer this approach, because to find out if a given path's is in a particular special folder or not, I'll have to enumerate all special folders for all users which is a bit of ugly, but if there's no other way, the sky is the limit :)
I searched it but couldn't find anything useful. So does WinApi has a function to do just that?
Thanks.
You can use IKnownFolderManager::FindFolderFromPath
Available since Vista.
PS: check out the CComPtr<> class for simpler interfacing with COM.
Here is a sample i just made up, showing how to use it:
#include <atlsafe.h>
#include <Shobjidl.h>
#include <comdef.h>
void PrintKnownFolder( const CComPtr<IKnownFolder>& folder )
{
KNOWNFOLDER_DEFINITION def;
HRESULT hr = folder->GetFolderDefinition( &def );
if( SUCCEEDED(hr) ) {
std::wcout << L"Result: " << def.pszName << std::endl;
FreeKnownFolderDefinitionFields( &def );
} else {
_com_error err(hr);
std::wcout << L"Error while querying GetFolderDefinition: " << err.ErrorMessage() << std::endl;
}
}
class CCoInitialize
{
public:
CCoInitialize() : m_hr(CoInitialize(NULL)) { }
~CCoInitialize() { if (SUCCEEDED(m_hr)) CoUninitialize(); }
operator HRESULT() const { return m_hr; }
private:
HRESULT m_hr;
};
bool test()
{
CCoInitialize co;
CComPtr<IKnownFolderManager> knownFolderManager;
HRESULT hr = knownFolderManager.CoCreateInstance( CLSID_KnownFolderManager );
if( !SUCCEEDED(hr) ) {
_com_error err(hr);
std::wcout << L"Error while creating KnownFolderManager: " << err.ErrorMessage() << std::endl;
return false;
}
CComPtr<IKnownFolder> folder;
hr = knownFolderManager->FindFolderFromPath( L"C:\\Users\\All Users\\Microsoft", FFFP_NEARESTPARENTMATCH, &folder );
if( SUCCEEDED(hr) ) {
PrintKnownFolder(folder);
} else {
_com_error err(hr);
std::wcout << L"Error while querying KnownFolderManager for nearest match: " << err.ErrorMessage() << std::endl;
}
// dispose it.
folder.Attach( NULL );
hr = knownFolderManager->FindFolderFromPath( L"C:\\Users\\All Users\\Microsoft", FFFP_EXACTMATCH, &folder );
if( SUCCEEDED(hr) ) {
PrintKnownFolder(folder);
} else {
_com_error err(hr);
std::wcout << L"Error while querying KnownFolderManager for exact match: " << err.ErrorMessage() << std::endl;
}
return true;
}
CCoInitialize borrowed from The Old New Thing
When we try to retrieve the Windows Update Information with WUA API, the following is the process I have followed. But I am bit confused with IUpdate::BundledUpdates property.
Create an IUpdateSearcher
Search based on a search criteria. I provided search criteria as "IsHidden=1 or IsInstalled=1"
You will have a IUpdateCollection as result of search.
Using get_Item in IUpdateCollection, I retrieved each update (IUpdate) and printed the required values (KB numbers in my case).
But again in IUpdate, you have a BundledUpdate property that gives IUpdateCollection with get_BundledUpdates() method. When I iterated over the results of BundledUpdates, I have got no results.
Am I missing something in retrieving bundled updates? (OR) does the criteria I specified includes Bundled Updates as part of first result set of IUpdateCollection?
Also in MSDN, examples are lacking for each interface in WUA API, Could someone provide any resources that explains clearly what each interface in WUA API does?
Added full source code of C++ console Application:
#include <wuapi.h>
#include <iostream>
#include <wuerror.h>
using namespace std;
int main()
{
HRESULT hr;
hr = CoInitialize(NULL);
IUpdateSession* iUpdate;
IUpdateSearcher* searcher;
ISearchResult* results;
BSTR criteria = SysAllocString(L"IsInstalled=1 or IsHidden=1 or IsPresent=1");
hr = CoCreateInstance(CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER,
IID_IUpdateSession, (LPVOID*)&iUpdate);
hr = iUpdate->CreateUpdateSearcher(&searcher);
wcout << L"Searching for updates ..."<<endl;
hr = searcher->Search(criteria, &results);
SysFreeString(criteria);
switch(hr)
{
case S_OK:
wcout<<L"List of applicable items on the machine:"<<endl;
break;
case WU_E_LEGACYSERVER:
wcout<<L"No server selection enabled"<<endl;
return 0;
case WU_E_INVALID_CRITERIA:
wcout<<L"Invalid search criteria"<<endl;
return 0;
}
IUpdateCollection * updateList;
IUpdate *updateItem;
LONG updateSize;
BSTR updateName;
results->get_Updates(&updateList);
updateList->get_Count(&updateSize);
if (updateSize == 0)
{
wcout << L"No updates found"<<endl;
}
for (LONG i = 0; i < updateSize; i++)
{
IStringCollection *KBCollection;
LONG KBCount;
updateList->get_Item(i, &updateItem);
updateItem->get_KBArticleIDs(&KBCollection);
KBCollection->get_Count(&KBCount);
for(int k=0;k<KBCount;k++)
{
BSTR KBValue;
KBCollection->get_Item(k,&KBValue);
wcout << L"KB" << KBValue << endl;
}
//Retrieve the bundled updates
IUpdateCollection *updtCollection;
updateItem->get_BundledUpdates(&updtCollection);
LONG updtBundledCount;
updtCollection->get_Count(&updtBundledCount);
for(LONG j=0;j<updtBundledCount;j++)
{
cout<<"Bundled KBs" <<endl;
IUpdate *bundledUpdateItem;
updtCollection->get_Item(j,&bundledUpdateItem);
bundledUpdateItem->get_KBArticleIDs(&KBID);
if(KBID != NULL)
{
LONG KBCount;
BSTR KBIDValue;
KBID->get_Count(&KBCount);
for(LONG j=0;j<KBCount;j++)
{
wcout << "KB" <<(KBIDValue) << endl;
temp.setKBID(KBIDValue);
}
}
}
}
wcout << L"Total KB Count : " << updateSize << endl;
CoUninitialize();
return 0;
}
I just fix your code in order to retrieve the Bundled Updates.
#include "stdafx.h"
#include <wuapi.h>
#include <iostream>
#include <wuerror.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;
hr = CoInitialize(NULL);
IUpdateSession* iUpdate;
IUpdateSearcher* searcher;
ISearchResult* results;
BSTR criteria = SysAllocString(L"IsInstalled=1 or IsHidden=1 or IsPresent=1");
hr = CoCreateInstance(CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER, IID_IUpdateSession, (LPVOID*)&iUpdate);
hr = iUpdate->CreateUpdateSearcher(&searcher);
wcout << L"Searching for updates ..."<<endl;
hr = searcher->Search(criteria, &results);
SysFreeString(criteria);
switch(hr)
{
case S_OK:
wcout<<L"List of applicable items on the machine:"<<endl;
break;
case WU_E_LEGACYSERVER:
wcout<<L"No server selection enabled"<<endl;
return 0;
case WU_E_INVALID_CRITERIA:
wcout<<L"Invalid search criteria"<<endl;
return 0;
}
IUpdateCollection *updateList;
IUpdate *updateItem;
LONG updateSize;
BSTR updateName;
results->get_Updates(&updateList);
updateList->get_Count(&updateSize);
if (updateSize == 0)
{
wcout << L"No updates found"<<endl;
}
for (LONG i = 0; i < updateSize; i++)
{
IStringCollection *KBCollection;
LONG KBCount;
updateList->get_Item(i,&updateItem);
updateItem->get_Title(&updateName);
wcout<<i+1<<" - "<<updateName<<endl;
IUpdateCollection *updtCollection;
LONG updtBundledCount;
//Retrieve the bundled updates
updateItem->get_BundledUpdates(&updtCollection);
hr = updtCollection->get_Count(&updtBundledCount);
if ((updtBundledCount>0) && (hr ==S_OK))
{
wcout << L"Bundled Updates " <<(updtBundledCount) << endl;
for(LONG j=0;j<updtBundledCount;j++)
{
IUpdate *bundledUpdateItem;
BSTR bundledName;
updtCollection->get_Item(j,&bundledUpdateItem);
bundledUpdateItem->get_Title(&bundledName);
wcout<<L" "<<j+1<<" - "<<bundledName<<endl;
}
}
}
::CoUninitialize();
wcin.get();
return 0;
}