[wmi][c++][dev-c++] lectures d'informations avec wmi sous devcpp

Soyez le premier à donner votre avis sur cette source.

Vue 10 073 fois - Téléchargée 701 fois

Description

Lecture d'informations WMI en C++ avec DEVC++.

A voir:
- plantage avec certains Firewall (Zone alarme)
- gérer les format DATE et chaînés

Infos:
Compilé en C++
la lib "wbemuuid" est inclue ainsi que les fihcier WMI
dans le répertoire de l'application.

Perfs:
effectuer la même chose avec un script vbs va plus vite :(
utilisation pour exemple de : WMICodeCreator.exe

Résultats : sous forme de fichier html (redirection avec la fichier .bat)

Pensez à renommer le fichier "WMI_READ_TEST.ex" en "WMI_READ_TEST.exe" et exécuter le fichier "bat.txt.bat", le résultat s'affiche dans le fichier "res.html"

Source / Exemple :


//------------------------------------------------------------------------------
// Projet WMI_READ_TEST : Lecture d'informations WMI avec DEVC++
// Auteur               : Hanteville Nicolas
// Fichier              : main.c
// Version              : 0.1
// Date de modification : 12/09/2008
// Site                 : http:\\omni.a.free.fr
//------------------------------------------------------------------------------
/*
     A voir:
            - plantage avec certains Firewall (Zone alarme)
            - gérer les format DATE et chaînés
            
     Infos:
           Compilé en C++
           la lib "wbemuuid" est inclue ainsi que les fihcier WMI 
           dans le répertoire de l'application.  
           
     Perfs:
           effectuer la même chose avec un script vbs va plus vite :(
           utilisation pour exemple de : WMICodeCreator.exe                                                                                     

  • /
//------------------------------------------------------------------------------ #include <stdio.h> #define _WIN32_DCOM #include "WbemCli.h" #include "wbemprov.h" #include "wbemtran.h" #include <objbase.h> /* -lole32 WbemUuid.Lib -loleaut32
  • /
#pragma comment(lib, "ole32.lib") #pragma comment(lib, "oleaut32.lib") #pragma comment(lib, "wbemuuid.lib") //****************************************************************************** //exemple WMI //http://msdn.microsoft.com/en-us/library/aa394558(VS.85).aspx //http://msdn.microsoft.com/en-us/library/aa394554(VS.85).aspx //classes WMI //http://msdn.microsoft.com/en-us/library/aa394084(VS.85).aspx //http://msdn.microsoft.com/en-us/library/aa394585(VS.85).aspx //http://msdn.microsoft.com/en-us/library/aa390423.aspx //http://msdn.microsoft.com/en-us/library/aa286547.aspx //****************************************************************************** int main() { //objet de connexion WMI IWbemLocator *pLoc = NULL; IWbemServices *pSvc = NULL; ULONG uReturn = 0; unsigned short nb=0; //ajouter init standard comme pour snmpS CoInitializeEx(0, COINIT_MULTITHREADED); //http://msdn.microsoft.com/en-us/library/ms693736(VS.85).aspx if(CoInitializeSecurity( NULL,-1,NULL,NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL,EOAC_NONE,NULL ) != S_OK) { printf("erreur - CoInitializeSecurity\n"); CoUninitialize(); return 0; }; //init objet if(FAILED(CoCreateInstance(CLSID_WbemLocator,NULL,CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc)) ) { printf("erreur - CoCreateInstance\n"); pLoc->Release(); CoUninitialize(); return 0; }; //connexion à la base if(FAILED(pLoc->ConnectServer( BSTR(L"ROOT\\CIMV2"), //emplacement NULL, //login actuel NULL, //mots de passe actuel NULL, //langue 0, //securityFlags (par défaut) 0, //authorité (NTLM par défaiut) 0, //context &pSvc)) ) { printf("erreur - ConnectServer\n"); pSvc->Release(); pLoc->Release(); CoUninitialize(); return 0; } // Set the IWbemServices proxy so that impersonation // of the user (client) occurs. if(FAILED(CoSetProxyBlanket( pSvc, // the proxy to set RPC_C_AUTHN_WINNT, // authentication service RPC_C_AUTHZ_NONE, // authorization service NULL, // Server principal name RPC_C_AUTHN_LEVEL_CALL, // authentication level RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level NULL, // client identity EOAC_NONE // proxy capabilities ))) { printf("erreur - CoSetProxyBlanket\n"); pSvc->Release(); pLoc->Release(); CoUninitialize(); return 0; } //****************************************************************************** //liste des processus en cours printf( "<html>\r\n" "<center><BIG>WMI read - test</BIG>(http:\\omni.a.free.fr)</center>" "[Index]<br/><br/>" "<a href=\"#0\">Liste des processus</a><br/>" "<a href=\"#1\">Utilisateurs et groupes</a><br/>" "<a href=\"#2\">Utilisateurs locaux</a><br/>" "<a href=\"#3\">Informations machine</a><br/>" "<a href=\"#4\">Services</a><br/>" "<a href=\"#5\">Logiciels</a><br/>" "<a href=\"#6\">Mise à jour</a><br/>" "<a href=\"#7\">Imprimantes</a><br/>" "<a href=\"#8\">Partages réseaux</a><br/>" "<a href=\"#9\">Gestionnaire de tâches</a><br/>" "<a href=\"#10\">Routes</a><br/>" "<a href=\"#11\">Journaux d'historiques</a><br/>" "<a href=\"#12\">Antivirus</a><br/>" "<a href=\"#13\">Firewall</a><br/><br/><hr/>" "\t<a name=\"0\"></a><big>[Liste des processus:]</big>\r\n"); // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for all the running processes IEnumWbemClassObject* pEnumerator = NULL; if (FAILED(pSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_Process"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery\n"); else { printf("\t<table border=\"1px\">\r\n"); IWbemClassObject *pclsObj; printf("\t\t<tr bgcolor=\"gray\">\r\n\t\t\t<td><font color=\"FFFFFF\">Nom</font></td><td><font color=\"FFFFFF\">Commande</font></td>\r\n\t\t</tr>\r\n"); while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; //nom pclsObj->Get(L"Name", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td><td>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); //CommandLine pclsObj->Get(L"CommandLine", 0, &vtProp, 0, 0); printf("%S</td>\r\n\t\t</tr>\r\n",vtProp.bstrVal); VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); printf("\t</table>\r\n"); } //--------------------------------------------------------------- //liste utilisateurs et groupes printf("\r\n\t<br/><br/><a name=\"1\"></a><big>[Utilisateurs et groupes:]</big>\r\n"); pEnumerator = NULL; uReturn = 0; nb=0; if (FAILED(pSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_Account"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery\n"); else { printf("\t<table border=\"1px\">\r\n"); IWbemClassObject *pclsObj; printf("\t\t<tr bgcolor=\"gray\">\r\n\t\t\t<td><font color=\"FFFFFF\">Nom</font></td><td><font color=\"FFFFFF\">Infos</font></td>\r\n\t\t</tr>\r\n"); while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; //nom pclsObj->Get(L"Name", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td><td>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); //SID pclsObj->Get(L"SID", 0, &vtProp, 0, 0); printf("SID: %S<br/>",vtProp.bstrVal); //Description pclsObj->Get(L"Description", 0, &vtProp, 0, 0); printf("Description: %S<br/>",vtProp.bstrVal); //Domain pclsObj->Get(L"Domain", 0, &vtProp, 0, 0); printf("Domain: %S<br/>",vtProp.bstrVal); //Status pclsObj->Get(L"Status", 0, &vtProp, 0, 0); printf("Etat: %S<br/>",vtProp.bstrVal); //LocalAccount pclsObj->Get(L"LocalAccount", 0, &vtProp, 0, 0); printf("LocalAccount: %S</td>\r\n\t\t</tr>\r\n",vtProp.boolVal?"OUI":"NON"); VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); printf("\t</table>\r\n"); } //--------------------------------------------------------------- //liste utilisateurs et groupes printf("\r\n\t<br/><br/><a name=\"2\"></a><big>[Utilisateurs locaux:]</big>\r\n"); // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for all the running processes pEnumerator = NULL; nb=0; if (FAILED(pSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_UserAccount"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery\n"); else { printf("\t<table border=\"1px\">\r\n"); IWbemClassObject *pclsObj; ULONG uReturn = 0; printf("\t\t<tr bgcolor=\"gray\">\r\n\t\t\t<td><font color=\"FFFFFF\">Nom</font></td><td><font color=\"FFFFFF\">Infos</font></td>\r\n\t\t</tr>\r\n"); while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; //nom pclsObj->Get(L"Name", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td><td>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); //FullName pclsObj->Get(L"FullName", 0, &vtProp, 0, 0); printf("FullName: %S<br/>",vtProp.bstrVal); //SID pclsObj->Get(L"SID", 0, &vtProp, 0, 0); printf("SID: %S<br/>",vtProp.bstrVal); //Caption pclsObj->Get(L"Caption", 0, &vtProp, 0, 0); printf("Caption: %S<br/>",vtProp.bstrVal); //Status pclsObj->Get(L"Status", 0, &vtProp, 0, 0); printf("Status: %S<br/>",vtProp.bstrVal); //Description pclsObj->Get(L"Description", 0, &vtProp, 0, 0); printf("Description: %S<br/>",vtProp.bstrVal); //Domain pclsObj->Get(L"Domain", 0, &vtProp, 0, 0); printf("Domain: %S<br/>",vtProp.bstrVal); //LocalAccount pclsObj->Get(L"LocalAccount", 0, &vtProp, 0, 0); printf("LocalAccount: %s<br/>",vtProp.boolVal?"OUI":"NON"); //Disabled pclsObj->Get(L"Disabled", 0, &vtProp, 0, 0); printf("Disabled: %s<br/>",vtProp.boolVal?"OUI":"NON"); //Lockout pclsObj->Get(L"Lockout", 0, &vtProp, 0, 0); printf("Lockout: %s<br/>",vtProp.boolVal?"OUI":"NON"); //PasswordChangeable pclsObj->Get(L"PasswordChangeable", 0, &vtProp, 0, 0); printf("PasswordChangeable: %s<br/>",vtProp.boolVal?"OUI":"NON"); //PasswordExpires pclsObj->Get(L"PasswordExpires", 0, &vtProp, 0, 0); printf("PasswordExpires: %s<br/>",vtProp.boolVal?"OUI":"NON"); //PasswordRequired pclsObj->Get(L"PasswordRequired", 0, &vtProp, 0, 0); printf("PasswordRequired: %s</td>\r\n\t\t</tr>\r\n",vtProp.boolVal?"OUI":"NON"); VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); printf("\t</table>\r\n"); } //--------------------------------------------------------------- //Information machine printf("\r\n\t<br/><br/><a name=\"3\"></a><big>[Informations machine:]</big>\r\n"); // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for all the running processes pEnumerator = NULL; nb=0; if (FAILED(pSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_ComputerSystem"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery\n"); else { printf("\t<table border=\"1px\">\r\n"); IWbemClassObject *pclsObj; ULONG uReturn = 0; printf("\t\t<tr bgcolor=\"gray\">\r\n\t\t\t<td><font color=\"FFFFFF\">Nom</font></td><td><font color=\"FFFFFF\">Infos</font></td>\r\n\t\t</tr>\r\n"); while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; //Name pclsObj->Get(L"Name", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td><td>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); //UserName pclsObj->Get(L"UserName", 0, &vtProp, 0, 0); printf("UserName: %S<br/>",vtProp.bstrVal); //Workgroup pclsObj->Get(L"Workgroup", 0, &vtProp, 0, 0); printf("Workgroup: %S<br/>",vtProp.bstrVal); //PrimaryOwnerContact pclsObj->Get(L"PrimaryOwnerContact", 0, &vtProp, 0, 0); printf("PrimaryOwnerContact: %S<br/>",vtProp.bstrVal); //PrimaryOwnerName pclsObj->Get(L"PrimaryOwnerName", 0, &vtProp, 0, 0); printf("PrimaryOwnerName: %S<br/>",vtProp.bstrVal); //Description pclsObj->Get(L"Description", 0, &vtProp, 0, 0); printf("Description: %S<br/>",vtProp.bstrVal); //Manufacturer pclsObj->Get(L"Manufacturer", 0, &vtProp, 0, 0); printf("Manufacturer: %S<br/>",vtProp.bstrVal); //Model pclsObj->Get(L"Model", 0, &vtProp, 0, 0); printf("Model: %S<br/>",vtProp.bstrVal); //SystemType pclsObj->Get(L"SystemType", 0, &vtProp, 0, 0); printf("SystemType: %S<br/>",vtProp.bstrVal); //AutomaticManagedPagefile pclsObj->Get(L"AutomaticManagedPagefile", 0, &vtProp, 0, 0); printf("AutomaticManagedPagefile: %s<br/>",vtProp.boolVal?"OUI":"NON"); //AutomaticResetBootOption pclsObj->Get(L"AutomaticResetBootOption", 0, &vtProp, 0, 0); printf("AutomaticResetBootOption: %s<br/>",vtProp.boolVal?"OUI":"NON"); //AutomaticResetCapability pclsObj->Get(L"AutomaticResetCapability", 0, &vtProp, 0, 0); printf("AutomaticResetCapability: %s<br/>",vtProp.boolVal?"OUI":"NON"); //InfraredSupported pclsObj->Get(L"InfraredSupported", 0, &vtProp, 0, 0); printf("InfraredSupported: %s<br/>",vtProp.boolVal?"OUI":"NON"); //NetworkServerModeEnabled pclsObj->Get(L"NetworkServerModeEnabled", 0, &vtProp, 0, 0); printf("NetworkServerModeEnabled: %s<br/>",vtProp.boolVal?"OUI":"NON"); //PartOfDomain pclsObj->Get(L"PartOfDomain", 0, &vtProp, 0, 0); printf("PartOfDomain: %s<br/>",vtProp.boolVal?"OUI":"NON"); //NumberOfProcessors pclsObj->Get(L"NumberOfProcessors", 0, &vtProp, 0, 0); printf("NumberOfProcessors: %d</td>\r\n\t\t</tr>\r\n",vtProp.iVal); VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); } //--------------------------------------------------------------- //Information machine 2 // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for all the running processes pEnumerator = NULL; if (FAILED(pSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_OperatingSystem"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery\n"); else { IWbemClassObject *pclsObj; ULONG uReturn = 0; while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; //Caption pclsObj->Get(L"Caption", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td><td>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); //Version pclsObj->Get(L"Version", 0, &vtProp, 0, 0); printf("Version: %S<br/>",vtProp.bstrVal); //CSDVersion pclsObj->Get(L"CSDVersion", 0, &vtProp, 0, 0); printf("CSDVersion: %S<br/>",vtProp.bstrVal); //Organization pclsObj->Get(L"Organization", 0, &vtProp, 0, 0); printf("Organization: %S<br/>",vtProp.bstrVal); //SystemDevice pclsObj->Get(L"SystemDevice", 0, &vtProp, 0, 0); printf("SystemDevice: %S<br/>",vtProp.bstrVal); //SystemDrive pclsObj->Get(L"SystemDrive", 0, &vtProp, 0, 0); printf("SystemDrive: %S<br/>",vtProp.bstrVal); //SystemDirectory pclsObj->Get(L"SystemDirectory", 0, &vtProp, 0, 0); printf("SystemDirectory: %S<br/>",vtProp.bstrVal); //WindowsDirectory pclsObj->Get(L"WindowsDirectory", 0, &vtProp, 0, 0); printf("WindowsDirectory: %S</td>\r\n\t\t</tr>\r\n",vtProp.bstrVal); VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); printf("\t</table>\r\n"); } //--------------------------------------------------------------- //services printf("\r\n\t<br/><br/><a name=\"4\"></a><big>[Services:]</big>\r\n"); // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for all the running processes pEnumerator = NULL; nb=0; if (FAILED(pSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_Service"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery\n"); else { printf("\t<table border=\"1px\">\r\n"); IWbemClassObject *pclsObj; ULONG uReturn = 0; printf("\t\t<tr bgcolor=\"gray\">\r\n\t\t\t<td><font color=\"FFFFFF\">Nom</font></td><td><font color=\"FFFFFF\">Infos</font></td>\r\n\t\t</tr>\r\n"); while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; //Caption pclsObj->Get(L"Caption", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td><td>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); //Description pclsObj->Get(L"Description", 0, &vtProp, 0, 0); printf("Description: %S<br/>",vtProp.bstrVal); //PathName pclsObj->Get(L"PathName", 0, &vtProp, 0, 0); printf("PathName: %S<br/>",vtProp.bstrVal); //Name pclsObj->Get(L"Name", 0, &vtProp, 0, 0); printf("Name: %S<br/>",vtProp.bstrVal); //ServiceType pclsObj->Get(L"ServiceType", 0, &vtProp, 0, 0); printf("ServiceType: %S<br/>",vtProp.bstrVal); //StartMode pclsObj->Get(L"StartMode", 0, &vtProp, 0, 0); printf("StartMode: %S<br/>",vtProp.bstrVal); //StartName pclsObj->Get(L"StartName", 0, &vtProp, 0, 0); printf("StartName: %S<br/>",vtProp.bstrVal); //State pclsObj->Get(L"State", 0, &vtProp, 0, 0); printf("State: %S<br/>",vtProp.bstrVal); //Started pclsObj->Get(L"Started", 0, &vtProp, 0, 0); printf("Started: %s</td>\r\n\t\t</tr>\r\n",vtProp.boolVal?"OUI":"NON"); VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); printf("\t</table>\r\n"); } //--------------------------------------------------------------- //logiciels printf("\r\n\t<br/><br/><a name=\"5\"></a><big>[Logiciels:]</big>\r\n"); // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for all the running processes pEnumerator = NULL; nb=0; if (FAILED(pSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_Product"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery\n"); else { printf("\t<table border=\"1px\">\r\n"); IWbemClassObject *pclsObj; ULONG uReturn = 0; printf("\t\t<tr bgcolor=\"gray\">\r\n\t\t\t<td><font color=\"FFFFFF\">Nom</font></td><td><font color=\"FFFFFF\">Infos</font></td>\r\n\t\t</tr>\r\n"); while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; //Name pclsObj->Get(L"Name", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td><td>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); //Version pclsObj->Get(L"Version", 0, &vtProp, 0, 0); printf("Version: %S<br/>",vtProp.bstrVal); //InstallDate pclsObj->Get(L"InstallDate", 0, &vtProp, 0, 0); printf("InstallDate: %S</td>\r\n\t\t</tr>\r\n",vtProp.bstrVal); VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); printf("\t</table>\r\n"); } //--------------------------------------------------------------- //Mise à jour printf("\r\n\t<br/><br/><a name=\"6\"></a><big>[Mise à jour:]</big>\r\n"); // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for all the running processes pEnumerator = NULL; nb=0; if (FAILED(pSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_QuickFixEngineering"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery\n"); else { printf("\t<table border=\"1px\">\r\n"); IWbemClassObject *pclsObj; ULONG uReturn = 0; printf("\t\t<tr bgcolor=\"gray\">\r\n\t\t\t<td><font color=\"FFFFFF\">Nom</font></td><td><font color=\"FFFFFF\">Infos</font></td>\r\n\t\t</tr>\r\n"); while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; pclsObj->Get(L"Description", 0, &vtProp, 0, 0); char tmp[MAX_PATH]=""; sprintf(tmp,"%S",vtProp.bstrVal); if (strlen(tmp)) { //Name pclsObj->Get(L"Name", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td><td>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); //Name pclsObj->Get(L"HotFixID", 0, &vtProp, 0, 0); printf("HotFixID: %S<br/>",vtProp.bstrVal); //Version pclsObj->Get(L"Description", 0, &vtProp, 0, 0); printf("Description: %S</td>\r\n\t\t</tr>\r\n",vtProp.bstrVal); } VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); printf("\t</table>\r\n"); /* datetime InstallDate;
  • /
} //--------------------------------------------------------------- //Imprimantes printf("\r\n\t<br/><br/><a name=\"7\"></a><big>[Imprimantes:]</big>\r\n"); // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for all the running processes pEnumerator = NULL; nb=0; if (FAILED(pSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_Printer"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery\n"); else { printf("\t<table border=\"1px\">\r\n"); IWbemClassObject *pclsObj; ULONG uReturn = 0; printf("\t\t<tr bgcolor=\"gray\">\r\n\t\t\t<td><font color=\"FFFFFF\">Nom</font></td><td><font color=\"FFFFFF\">Infos</font></td>\r\n\t\t</tr>\r\n"); while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; //Name pclsObj->Get(L"Name", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td><td>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); //Comment pclsObj->Get(L"Comment", 0, &vtProp, 0, 0); printf("Comment: %S<br/>",vtProp.bstrVal); //Parameters pclsObj->Get(L"Parameters", 0, &vtProp, 0, 0); printf("Parameters: %S<br/>",vtProp.bstrVal); //ServerName pclsObj->Get(L"ServerName", 0, &vtProp, 0, 0); printf("ServerName: %S<br/>",vtProp.bstrVal); //ShareName pclsObj->Get(L"ShareName", 0, &vtProp, 0, 0); printf("ShareName: %S<br/>",vtProp.bstrVal); //SystemName pclsObj->Get(L"SystemName", 0, &vtProp, 0, 0); printf("SystemName: %S<br/>",vtProp.bstrVal); //Version pclsObj->Get(L"Description", 0, &vtProp, 0, 0); printf("Description: %S</td>\r\n\t\t</tr>\r\n",vtProp.bstrVal); VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); printf("\t</table>\r\n"); } //--------------------------------------------------------------- //partages réseaux printf("\r\n\t<br/><br/><a name=\"8\"></a><big>[Partages réseaux:]</big>\r\n"); // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for all the running processes pEnumerator = NULL; nb=0; if (FAILED(pSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_Share"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery\n"); else { printf("\t<table border=\"1px\">\r\n"); IWbemClassObject *pclsObj; ULONG uReturn = 0; printf("\t\t<tr bgcolor=\"gray\">\r\n\t\t\t<td><font color=\"FFFFFF\">Nom</font></td><td><font color=\"FFFFFF\">Infos</font></td><td><font color=\"FFFFFF\">Chemin</font></td>\r\n\t\t</tr>\r\n"); while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; //Caption pclsObj->Get(L"Caption", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td><td>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); //Name pclsObj->Get(L"Name", 0, &vtProp, 0, 0); printf("<td>%S</td><td>",vtProp.bstrVal); //Path pclsObj->Get(L"Path", 0, &vtProp, 0, 0); printf("%S</td>\r\n\t\t</tr>\r\n",vtProp.bstrVal); VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); printf("\t</table>\r\n"); } //--------------------------------------------------------------- //gestionnaire de tâche printf("\r\n\t<br/><br/><a name=\"9\"></a><big>[Gestionnaire de tâches:]</big>\r\n"); // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for all the running processes pEnumerator = NULL; nb=0; if (FAILED(pSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_ScheduledJob"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery\n"); else { printf("\t<table border=\"1px\">\r\n"); IWbemClassObject *pclsObj; ULONG uReturn = 0; printf("\t\t<tr bgcolor=\"gray\">\r\n\t\t\t<td><font color=\"FFFFFF\">Nom</font></td><td><font color=\"FFFFFF\">Infos</font></td>\r\n\t\t</tr>\r\n"); while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; //Caption pclsObj->Get(L"Caption", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td><td>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); //Command pclsObj->Get(L"Command", 0, &vtProp, 0, 0); printf("Command: %S<br/>",vtProp.bstrVal); //Description pclsObj->Get(L"Description", 0, &vtProp, 0, 0); printf("Description: %S<br/>",vtProp.bstrVal); //JobStatus pclsObj->Get(L"JobStatus", 0, &vtProp, 0, 0); printf("JobStatus: %S<br/>",vtProp.bstrVal); //Name pclsObj->Get(L"Name", 0, &vtProp, 0, 0); printf("Name: %S<br/>",vtProp.bstrVal); //Notify pclsObj->Get(L"Notify", 0, &vtProp, 0, 0); printf("Notify: %S<br/>",vtProp.bstrVal); //Owner pclsObj->Get(L"Owner", 0, &vtProp, 0, 0); printf("Owner: %S<br/>",vtProp.bstrVal); //Status pclsObj->Get(L"Status", 0, &vtProp, 0, 0); printf("Status: %S</td>\r\n\t\t</tr>\r\n",vtProp.bstrVal); VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); printf("\t</table>\r\n"); } //--------------------------------------------------------------- //Route réseaux printf("\r\n\t<br/><br/><a name=\"10\"></a><big>[Routes:]</big>\r\n"); // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for all the running processes pEnumerator = NULL; nb=0; if (FAILED(pSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_IP4RouteTable"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery\n"); else { printf("\t<table border=\"1px\">\r\n"); IWbemClassObject *pclsObj; ULONG uReturn = 0; printf("\t\t<tr bgcolor=\"gray\">\r\n\t\t\t<td><font color=\"FFFFFF\">Description</font></td>\r\n\t\t</tr>\r\n"); while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; //Description pclsObj->Get(L"Description", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td></tr>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); printf("\t</table>\r\n"); } //--------------------------------------------------------------- //log printf("\r\n\t<br/><br/><a name=\"11\"></a><big>[Journaux d'historiques:]</big>\r\n"); // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for all the running processes pEnumerator = NULL; nb=0; if (FAILED(pSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_NTLogEvent"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery\n"); else { printf("\t<table border=\"1px\">\r\n"); IWbemClassObject *pclsObj; ULONG uReturn = 0; printf("\t\t<tr bgcolor=\"gray\">\r\n\t\t\t<td><font color=\"FFFFFF\">Nom</font></td><td><font color=\"FFFFFF\">Infos</font></td>\r\n\t\t</tr>\r\n"); while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; //Logfile pclsObj->Get(L"Logfile", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td><td>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); //Message pclsObj->Get(L"Message", 0, &vtProp, 0, 0); printf("Message: %S<br/>",vtProp.bstrVal); //SourceName pclsObj->Get(L"SourceName", 0, &vtProp, 0, 0); printf("SourceName: %S<br/>",vtProp.bstrVal); //Type pclsObj->Get(L"Type", 0, &vtProp, 0, 0); printf("Type: %S<br/>",vtProp.bstrVal); //User pclsObj->Get(L"User", 0, &vtProp, 0, 0); printf("User: %S</td>\r\n\t\t</tr>\r\n",vtProp.bstrVal); VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); printf("\t</table>\r\n"); } //--------------------------------------------------------------- pSvc->Release(); //---------------------------------------------------------------------------- //centre de sécurité pLoc->Release(); CoUninitialize(); CoInitializeEx(0, COINIT_MULTITHREADED); pEnumerator = NULL; nb=0; //http://msdn.microsoft.com/en-us/library/ms693736(VS.85).aspx if(CoInitializeSecurity( NULL,-1,NULL,NULL, RPC_C_AUTHN_LEVEL_DEFAULT,//RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE,//RPC_C_IMP_LEVEL_DEFAULT,//RPC_C_IMP_LEVEL_IMPERSONATE, NULL,EOAC_NONE,NULL ) != S_OK) printf("erreur - CoInitializeSecurity\n"); //objet de connexion WMI IWbemLocator *mpLoc = NULL; IWbemServices *mpSvc = NULL; //init objet if(FAILED(CoCreateInstance(CLSID_WbemLocator,NULL,CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &mpLoc)) ) printf("erreur - CoCreateInstance\n"); //connexion à la base if(FAILED(mpLoc->ConnectServer( BSTR(L"ROOT\\SecurityCenter"), //emplacement NULL, //login actuel NULL, //mots de passe actuel 0, //local 0, //securityFlags 0, //authorité (NTLM domain) 0, //context &mpSvc)) )printf("erreur - ConnectServer SecurityCenter\n"); // Set the IWbemServices proxy so that impersonation // of the user (client) occurs. if(FAILED(CoSetProxyBlanket( mpSvc, // the proxy to set RPC_C_AUTHN_WINNT, // authentication service RPC_C_AUTHZ_NONE, // authorization service NULL, // Server principal name RPC_C_AUTHN_LEVEL_CALL, // authentication level RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level NULL, // client identity EOAC_NONE // proxy capabilities )))printf("erreur - CoSetProxyBlanket SecurityCenter\n"); printf("\r\n\t<br/><br/><a name=\"12\"></a><big>[Antivirus:]</big>\r\n"); // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for all the running processes if (FAILED(mpSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM AntiVirusProduct"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery AntiVirusProduct\n"); else { printf("\t<table border=\"1px\">\r\n"); IWbemClassObject *pclsObj; ULONG uReturn = 0; printf("\t\t<tr bgcolor=\"gray\">\r\n\t\t\t<td><font color=\"FFFFFF\">Nom</font></td><td><font color=\"FFFFFF\">Infos</font></td>\r\n\t\t</tr>\r\n"); while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; //displayName pclsObj->Get(L"DisplayName", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td><td>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); //companyName pclsObj->Get(L"CompanyName", 0, &vtProp, 0, 0); printf("CompanyName: %S<br/>\r\n",vtProp.bstrVal); //OnAccessScanningEnabled pclsObj->Get(L"OnAccessScanningEnabled", 0, &vtProp, 0, 0); printf("OnAccessScanningEnabled: %d<br/>\r\n",vtProp.boolVal?"OUI":"NON"); //productUptoDate pclsObj->Get(L"productUptoDate", 0, &vtProp, 0, 0); printf("productUptoDate: %S<br/>\r\n",vtProp.bstrVal); //versionNumber pclsObj->Get(L"VersionNumber", 0, &vtProp, 0, 0); printf("VersionNumber: %S</td>\r\n\t\t</tr>\r\n",vtProp.bstrVal); VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); printf("\t</table>\r\n"); } printf("\r\n\t<br/><br/><a name=\"13\"></a><big>[Firewall:]</big>\r\n"); // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for all the running processes if (FAILED(mpSvc->ExecQuery( BSTR(L"WQL"), BSTR(L"SELECT * FROM FirewallProduct"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)))printf("erreur - ExecQuery FirewallProduct\n"); else { printf("\t<table border=\"1px\">\r\n"); IWbemClassObject *pclsObj; ULONG uReturn = 0; printf("\t\t<tr bgcolor=\"gray\">\r\n\t\t\t<td><font color=\"FFFFFF\">Nom</font></td><td><font color=\"FFFFFF\">Infos</font></td>\r\n\t\t</tr>\r\n"); while (pEnumerator) { pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); if(0 == uReturn)break; VARIANT vtProp; //displayName pclsObj->Get(L"DisplayName", 0, &vtProp, 0, 0); printf("\t\t<tr%s>\r\n\t\t\t<td>%S</td><td>",nb++%2==1?" bgcolor=\"silver\"":"",vtProp.bstrVal); //companyName pclsObj->Get(L"CompanyName", 0, &vtProp, 0, 0); printf("CompanyName: %S<br/>\r\n",vtProp.bstrVal); //enabled pclsObj->Get(L"Enabled", 0, &vtProp, 0, 0); printf("Enabled: %d<br/>\r\n",vtProp.boolVal?"OUI":"NON"); //versionNumber pclsObj->Get(L"VersionNumber", 0, &vtProp, 0, 0); printf("VersionNumber: %S</td>\r\n\t\t</tr>\r\n",vtProp.bstrVal); VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); printf("\t</table>\r\n"); } printf("\r\n</html>\r\n"); mpSvc->Release(); mpLoc->Release(); CoUninitialize(); // system("PAUSE"); return 0; }

Codes Sources

A voir également

Ajouter un commentaire

Commentaires

devluc
Messages postés
1
Date d'inscription
mardi 11 janvier 2011
Statut
Membre
Dernière intervention
12 janvier 2011
-
Thanks a lot!
cs_omnia
Messages postés
240
Date d'inscription
jeudi 9 janvier 2003
Statut
Membre
Dernière intervention
22 mars 2009
-
Effectivement :p

pour les includes : j'avais fait sa pour ceux qui utilisaient DEVcpp.

pour SecurityCenter2, oui tout a fait pour info c'est documenté sur le msdn de Microsoft.

bon codage
scoubidou944
Messages postés
728
Date d'inscription
mardi 22 avril 2003
Statut
Membre
Dernière intervention
19 janvier 2017
-
J'ai remplacé:
#include "WbemCli.h"
#include "wbemprov.h"
#include "wbemtran.h"

par :
#include <Wbemcli.h>
(j'ai donc supprimé les 3 fichiers en question du projet puisque l'on peut utiliser ceux de VS 2008 Express).

L"ROOT\\SecurityCenter" a été remplacé par L"ROOT\\SecurityCenter2" sur Vista SP1+ il semblerait (je confirme qu'il faut prendre la 2ème version pour 7 Pro pour ne pas avoir des champs vides.
cs_omnia
Messages postés
240
Date d'inscription
jeudi 9 janvier 2003
Statut
Membre
Dernière intervention
22 mars 2009
-
Bonjour,

si ton but est de la communication tu recherche au mauvais endroit, si ce n'est que pour le statut, ton périphérique est visible en USB et non série.
tutu91
Messages postés
2
Date d'inscription
lundi 11 août 2008
Statut
Membre
Dernière intervention
5 novembre 2008
-
Salut

J'ai un pb avec WMI.
J'utilise un adaptateur USB serie et je voudrais récupérer le status du port série. Mais l'adaptateur USB serie n'apparait pas dans la classe Win32_serialport.
Quelqu'un peut-il me dire comment faire?

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.