A voir également:
- WMI sous DEV C++
- Telecharger dev c++ - Télécharger - Langages
- Massgrave dev - Accueil - Windows
- Dev home. - Télécharger - Développement
- Wmi provider host c'est quoi ✓ - Forum Windows 8 / 8.1
- Dev-pascal - Télécharger - Édition & Programmation
1 réponse
j'en conclus que c'est non ...
pourtant, qu'est ce qui ne va pas :
#include "stdafx.h"
#include "AcWMI.h"
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
HRESULT ConnectServerWMI ( OUT IWbemLocator** ppiWmiLoc ,
OUT IWbemServices** ppiWmiServ ,
const CString csNamespace ,
const CString csUsername ,
const CString csPassword ,
CString& csErrRef )
{
if (NULL == ppiWmiServ || NULL == ppiWmiLoc) {
csErrRef = _T("Invalid argument");
return E_INVALIDARG;
}
HRESULT hres;
hres = CoCreateInstance ( CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER, IID_IWbemLocator ,
(LPVOID *) ppiWmiLoc );
if (FAILED(hres)) {
csErrRef = _T("Failed to create IWbemLocator object.");
return hres;
}
CComBSTR bstrNamespace(csNamespace);
if (csPassword.IsEmpty() || csUsername.IsEmpty())
{
hres = (*ppiWmiLoc)->ConnectServer(bstrNamespace, NULL, NULL ,
0, NULL, 0, 0, ppiWmiServ );
}
else
{
// connect server using password and username
CComBSTR bstrUsername(csUsername), bstrPassword(csPassword);
hres = (*ppiWmiLoc)->ConnectServer(bstrNamespace, bstrUsername,
bstrPassword, 0, NULL, 0, 0,
ppiWmiServ );
}
if (FAILED(hres))
{
(*ppiWmiLoc)->Release();
csErrRef = _T("Failed to connect server.");
return hres;
}
hres = CoSetProxyBlanket( *ppiWmiServ ,
RPC_C_AUTHN_WINNT ,
RPC_C_AUTHZ_NONE ,
NULL,
RPC_C_AUTHN_LEVEL_CALL ,
RPC_C_IMP_LEVEL_IMPERSONATE ,
NULL,
EOAC_NONE);
if(FAILED(hres))
{
(*ppiWmiLoc)->Release();
(*ppiWmiServ)->Release();
csErrRef = _T("Can not set proxy blank.");
return hres;
}
return hres;
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
HRESULT EnumInstancesWMI ( IN IWbemServices* piWmiServ ,
OUT IEnumWbemClassObject** ppiWmiEnum ,
const CString& csInstName, CString& csErrRef )
{
if (NULL == piWmiServ || NULL == ppiWmiEnum)
{
csErrRef = _T("Invalid argument");
return E_INVALIDARG;
}
HRESULT hres;
CComBSTR bstrObjectName(csInstName);
hres = piWmiServ->CreateInstanceEnum( bstrObjectName ,
WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY ,
NULL ,
ppiWmiEnum);
if (FAILED(hres))
{
csErrRef.Format("Could not enumerate %s instances.", csInstName);
return hres;
}
return hres;
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
HRESULT GetLastErrorWMI (CString& csErrRef, HRESULT hresErr)
{
IWbemStatusCodeText * pStatus = NULL;
HRESULT hres = CoCreateInstance(CLSID_WbemStatusCodeText, 0, CLSCTX_INPROC_SERVER,
IID_IWbemStatusCodeText, (LPVOID *) &pStatus);
if(S_OK == hres)
{
CComBSTR bstrError;
hres = pStatus->GetErrorCodeText(hresErr, 0, 0, &bstrError);
if(S_OK != hres)
bstrError = SysAllocString(L"Get last error failed");
USES_CONVERSION;
csErrRef = OLE2T(bstrError);
pStatus->Release();
}
return hres;
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
HRESULT EnumInstPropNameWMI ( IN IWbemClassObject* piappObj,
OUT LPSAFEARRAY* ppsarProp )
{
if (NULL == ppsarProp || NULL == piappObj)
return E_INVALIDARG;
// GetNames methods will create SAFEARRAY,
// but on entry this parameter must point to NULL
if (NULL != *ppsarProp)
{
SafeArrayDestroy(*ppsarProp);
delete *ppsarProp;
*ppsarProp = NULL;
if (NULL == ppsarProp)
return E_INVALIDARG;
}
HRESULT hres;
hres = piappObj->GetNames( NULL,
WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY,
NULL,
ppsarProp);
return hres;
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
HRESULT EnumInstPropNameWMI ( IN IWbemClassObject* piappObj,
OUT CStringArray& psarPropRef )
{
HRESULT hres;
SAFEARRAY* pSafeArrProp = NULL;
psarPropRef.RemoveAll();
hres = EnumInstPropNameWMI(piappObj, &pSafeArrProp );
if (WBEM_S_NO_ERROR != hres)
return hres;
long lLower, lUpper;
SafeArrayGetLBound(pSafeArrProp , 1, &lLower);
SafeArrayGetUBound(pSafeArrProp , 1, &lUpper);
for (long i = lLower; i <= lUpper; ++i)
{
CComBSTR bstrPropName;
if (S_OK != (hres = SafeArrayGetElement(pSafeArrProp, &i, &bstrPropName)) )
{
if (NULL != pSafeArrProp)
SafeArrayDestroy(pSafeArrProp);
return hres;
}
USES_CONVERSION;
psarPropRef.SetAtGrow(psarPropRef.GetSize(), OLE2T(bstrPropName));
}
if (NULL != pSafeArrProp)
SafeArrayDestroy(pSafeArrProp);
return hres;
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
HRESULT ExecMethodWMI ( IN IWbemServices* piWmiServ ,
const CString& csMethodName ,
const CString& csClassName ,
const CString& csClassPath ,
const CString& csObjectPath ,
const CStringArray* csarrPropNames /*=NULL*/,
VARIANT *arrVarInArg/*=NULL*/, const int size_t/*=0*/)
{
// sanity checks
if (NULL == piWmiServ)
return E_INVALIDARG;
if (( NULL != csarrPropNames && NULL == arrVarInArg) ||
( NULL == csarrPropNames && NULL != arrVarInArg) )
return E_INVALIDARG;
if (NULL != csarrPropNames)
{
if(size_t != csarrPropNames->GetSize())
return E_INVALIDARG;
}
IWbemClassObject* pClassObj = NULL;
IWbemClassObject* pOutParam = NULL;
IWbemClassObject* pInParam = NULL;
IWbemClassObject* pInClass = NULL;
IWbemClassObject* pOutClass = NULL;
CComBSTR bstrMethodName ( csMethodName);
CComBSTR bstrClassName ( csClassName );
CComBSTR bstrClassPath ( csClassPath );
CComBSTR bstrObjectPath ( csObjectPath);
HRESULT hres;
hres = piWmiServ->GetObject(bstrClassName, 0, NULL, &pClassObj, NULL);
if (WBEM_S_NO_ERROR == hres)
{
// get the input-argument class object and create an instance.
// pInClass == NULL indicates that no input parameters needed.
hres = pClassObj->GetMethod(bstrMethodName, 0, &pInClass, NULL);
if (WBEM_S_NO_ERROR == hres)
{
if( NULL != pInClass)
{
// create instance copy
if(WBEM_S_NO_ERROR == (hres=pInClass->SpawnInstance(0, &pInParam)) )
{
// set each property
for (long i = 0; i < size_t; ++i)
{
CComBSTR bstrPropName(csarrPropNames->GetAt(i));
hres = pInParam->Put(bstrPropName, 0, &arrVarInArg[i], 0);
// DUF!!! Put failed, check the properties and their types
if (WBEM_S_NO_ERROR != hres)
break;
}
}
}
// finally call the method
if (WBEM_S_NO_ERROR == hres)
hres = piWmiServ->ExecMethod(bstrObjectPath, bstrMethodName, 0, NULL, pInParam, &pOutParam, NULL);
}
}
// free all resources
if (NULL != pOutParam)
{
// but first we get the output parameters here
CComBSTR bstrClassObj;
hres = pOutParam->GetObjectText(0, &(bstrClassObj.m_str));
USES_CONVERSION;
CWnd* pWndMain = AfxGetMainWnd();
MessageBox( pWndMain ? pWndMain->m_hWnd : NULL, OLE2T(bstrClassObj), "Result parameters", MB_OK);
pOutParam->Release();
}
if (NULL != pClassObj) pClassObj->Release();
if (NULL != pInParam) pInParam->Release();
if (NULL != pInClass) pInClass->Release();
if (NULL != pOutClass) pOutClass->Release();
return hres;
}
HRESULT GetClassMethodsWMI ( IN IWbemClassObject* piappObj,
OUT CStringArray& csarrMethods)
{
if (NULL == piappObj)
return E_INVALIDARG;
csarrMethods.RemoveAll();
USES_CONVERSION;
HRESULT hres;
hres = piappObj->BeginMethodEnumeration(0);
while(WBEM_S_NO_ERROR == hres)
{
CComBSTR bstrMethodName;
hres = piappObj->NextMethod(0, &bstrMethodName, NULL, NULL);
if (WBEM_S_NO_ERROR == hres)
csarrMethods.SetAtGrow(csarrMethods.GetSize(), OLE2T(bstrMethodName));
}
return piappObj->EndMethodEnumeration();
}
AcWMI.h :
#if !defined (AFX_AC_WMI_H__INCLUDED_)
#define AFX_AC_WMI_H__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <comdef.h>
#include <Wbemidl.h>
#include <atlbase.h>
#include <atlconv.h>
#include <afxtempl.h>
#ifndef OUT
#define OUT
#endif
#ifndef IN
#define IN
#endif
HRESULT ConnectServerWMI (OUT IWbemLocator** ppiWmiLoc ,
OUT IWbemServices** ppiWmiServ ,
const CString csNamespace ,
const CString csUsername ,
const CString csPassword ,
CString& csErrRef );
HRESULT EnumInstancesWMI (IN IWbemServices* piWmiServ ,
OUT IEnumWbemClassObject** ppiWmiEnum ,
const CString& csInstName ,
CString& csErrRef );
HRESULT GetLastErrorWMI (OUT CString& csErrRef,
IN HRESULT hresErr);
HRESULT EnumInstPropNameWMI ( IN IWbemClassObject* piappObj,
OUT CStringArray& psarPropRef );
HRESULT EnumInstPropNameWMI ( IN IWbemClassObject* piappObj,
OUT LPSAFEARRAY* psarProp );
HRESULT ExecMethodWMI (IN IWbemServices* piWmiServ ,
const CString& csMethodName,
const CString& csClassName ,
const CString& csClassPath ,
const CString& csObjectPath,
const CStringArray* csarrPropNames = NULL,
VARIANT *arrVarInArg = NULL, const int size_t = 0);
HRESULT GetClassMethodsWMI (IN IWbemClassObject* piappObj,
OUT CStringArray& csarrMethods);
#endif // AFX_AC_WMI_H__INCLUDED_
...
..
.
?
pourtant, qu'est ce qui ne va pas :
#include "stdafx.h"
#include "AcWMI.h"
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
HRESULT ConnectServerWMI ( OUT IWbemLocator** ppiWmiLoc ,
OUT IWbemServices** ppiWmiServ ,
const CString csNamespace ,
const CString csUsername ,
const CString csPassword ,
CString& csErrRef )
{
if (NULL == ppiWmiServ || NULL == ppiWmiLoc) {
csErrRef = _T("Invalid argument");
return E_INVALIDARG;
}
HRESULT hres;
hres = CoCreateInstance ( CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER, IID_IWbemLocator ,
(LPVOID *) ppiWmiLoc );
if (FAILED(hres)) {
csErrRef = _T("Failed to create IWbemLocator object.");
return hres;
}
CComBSTR bstrNamespace(csNamespace);
if (csPassword.IsEmpty() || csUsername.IsEmpty())
{
hres = (*ppiWmiLoc)->ConnectServer(bstrNamespace, NULL, NULL ,
0, NULL, 0, 0, ppiWmiServ );
}
else
{
// connect server using password and username
CComBSTR bstrUsername(csUsername), bstrPassword(csPassword);
hres = (*ppiWmiLoc)->ConnectServer(bstrNamespace, bstrUsername,
bstrPassword, 0, NULL, 0, 0,
ppiWmiServ );
}
if (FAILED(hres))
{
(*ppiWmiLoc)->Release();
csErrRef = _T("Failed to connect server.");
return hres;
}
hres = CoSetProxyBlanket( *ppiWmiServ ,
RPC_C_AUTHN_WINNT ,
RPC_C_AUTHZ_NONE ,
NULL,
RPC_C_AUTHN_LEVEL_CALL ,
RPC_C_IMP_LEVEL_IMPERSONATE ,
NULL,
EOAC_NONE);
if(FAILED(hres))
{
(*ppiWmiLoc)->Release();
(*ppiWmiServ)->Release();
csErrRef = _T("Can not set proxy blank.");
return hres;
}
return hres;
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
HRESULT EnumInstancesWMI ( IN IWbemServices* piWmiServ ,
OUT IEnumWbemClassObject** ppiWmiEnum ,
const CString& csInstName, CString& csErrRef )
{
if (NULL == piWmiServ || NULL == ppiWmiEnum)
{
csErrRef = _T("Invalid argument");
return E_INVALIDARG;
}
HRESULT hres;
CComBSTR bstrObjectName(csInstName);
hres = piWmiServ->CreateInstanceEnum( bstrObjectName ,
WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY ,
NULL ,
ppiWmiEnum);
if (FAILED(hres))
{
csErrRef.Format("Could not enumerate %s instances.", csInstName);
return hres;
}
return hres;
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
HRESULT GetLastErrorWMI (CString& csErrRef, HRESULT hresErr)
{
IWbemStatusCodeText * pStatus = NULL;
HRESULT hres = CoCreateInstance(CLSID_WbemStatusCodeText, 0, CLSCTX_INPROC_SERVER,
IID_IWbemStatusCodeText, (LPVOID *) &pStatus);
if(S_OK == hres)
{
CComBSTR bstrError;
hres = pStatus->GetErrorCodeText(hresErr, 0, 0, &bstrError);
if(S_OK != hres)
bstrError = SysAllocString(L"Get last error failed");
USES_CONVERSION;
csErrRef = OLE2T(bstrError);
pStatus->Release();
}
return hres;
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
HRESULT EnumInstPropNameWMI ( IN IWbemClassObject* piappObj,
OUT LPSAFEARRAY* ppsarProp )
{
if (NULL == ppsarProp || NULL == piappObj)
return E_INVALIDARG;
// GetNames methods will create SAFEARRAY,
// but on entry this parameter must point to NULL
if (NULL != *ppsarProp)
{
SafeArrayDestroy(*ppsarProp);
delete *ppsarProp;
*ppsarProp = NULL;
if (NULL == ppsarProp)
return E_INVALIDARG;
}
HRESULT hres;
hres = piappObj->GetNames( NULL,
WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY,
NULL,
ppsarProp);
return hres;
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
HRESULT EnumInstPropNameWMI ( IN IWbemClassObject* piappObj,
OUT CStringArray& psarPropRef )
{
HRESULT hres;
SAFEARRAY* pSafeArrProp = NULL;
psarPropRef.RemoveAll();
hres = EnumInstPropNameWMI(piappObj, &pSafeArrProp );
if (WBEM_S_NO_ERROR != hres)
return hres;
long lLower, lUpper;
SafeArrayGetLBound(pSafeArrProp , 1, &lLower);
SafeArrayGetUBound(pSafeArrProp , 1, &lUpper);
for (long i = lLower; i <= lUpper; ++i)
{
CComBSTR bstrPropName;
if (S_OK != (hres = SafeArrayGetElement(pSafeArrProp, &i, &bstrPropName)) )
{
if (NULL != pSafeArrProp)
SafeArrayDestroy(pSafeArrProp);
return hres;
}
USES_CONVERSION;
psarPropRef.SetAtGrow(psarPropRef.GetSize(), OLE2T(bstrPropName));
}
if (NULL != pSafeArrProp)
SafeArrayDestroy(pSafeArrProp);
return hres;
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
HRESULT ExecMethodWMI ( IN IWbemServices* piWmiServ ,
const CString& csMethodName ,
const CString& csClassName ,
const CString& csClassPath ,
const CString& csObjectPath ,
const CStringArray* csarrPropNames /*=NULL*/,
VARIANT *arrVarInArg/*=NULL*/, const int size_t/*=0*/)
{
// sanity checks
if (NULL == piWmiServ)
return E_INVALIDARG;
if (( NULL != csarrPropNames && NULL == arrVarInArg) ||
( NULL == csarrPropNames && NULL != arrVarInArg) )
return E_INVALIDARG;
if (NULL != csarrPropNames)
{
if(size_t != csarrPropNames->GetSize())
return E_INVALIDARG;
}
IWbemClassObject* pClassObj = NULL;
IWbemClassObject* pOutParam = NULL;
IWbemClassObject* pInParam = NULL;
IWbemClassObject* pInClass = NULL;
IWbemClassObject* pOutClass = NULL;
CComBSTR bstrMethodName ( csMethodName);
CComBSTR bstrClassName ( csClassName );
CComBSTR bstrClassPath ( csClassPath );
CComBSTR bstrObjectPath ( csObjectPath);
HRESULT hres;
hres = piWmiServ->GetObject(bstrClassName, 0, NULL, &pClassObj, NULL);
if (WBEM_S_NO_ERROR == hres)
{
// get the input-argument class object and create an instance.
// pInClass == NULL indicates that no input parameters needed.
hres = pClassObj->GetMethod(bstrMethodName, 0, &pInClass, NULL);
if (WBEM_S_NO_ERROR == hres)
{
if( NULL != pInClass)
{
// create instance copy
if(WBEM_S_NO_ERROR == (hres=pInClass->SpawnInstance(0, &pInParam)) )
{
// set each property
for (long i = 0; i < size_t; ++i)
{
CComBSTR bstrPropName(csarrPropNames->GetAt(i));
hres = pInParam->Put(bstrPropName, 0, &arrVarInArg[i], 0);
// DUF!!! Put failed, check the properties and their types
if (WBEM_S_NO_ERROR != hres)
break;
}
}
}
// finally call the method
if (WBEM_S_NO_ERROR == hres)
hres = piWmiServ->ExecMethod(bstrObjectPath, bstrMethodName, 0, NULL, pInParam, &pOutParam, NULL);
}
}
// free all resources
if (NULL != pOutParam)
{
// but first we get the output parameters here
CComBSTR bstrClassObj;
hres = pOutParam->GetObjectText(0, &(bstrClassObj.m_str));
USES_CONVERSION;
CWnd* pWndMain = AfxGetMainWnd();
MessageBox( pWndMain ? pWndMain->m_hWnd : NULL, OLE2T(bstrClassObj), "Result parameters", MB_OK);
pOutParam->Release();
}
if (NULL != pClassObj) pClassObj->Release();
if (NULL != pInParam) pInParam->Release();
if (NULL != pInClass) pInClass->Release();
if (NULL != pOutClass) pOutClass->Release();
return hres;
}
HRESULT GetClassMethodsWMI ( IN IWbemClassObject* piappObj,
OUT CStringArray& csarrMethods)
{
if (NULL == piappObj)
return E_INVALIDARG;
csarrMethods.RemoveAll();
USES_CONVERSION;
HRESULT hres;
hres = piappObj->BeginMethodEnumeration(0);
while(WBEM_S_NO_ERROR == hres)
{
CComBSTR bstrMethodName;
hres = piappObj->NextMethod(0, &bstrMethodName, NULL, NULL);
if (WBEM_S_NO_ERROR == hres)
csarrMethods.SetAtGrow(csarrMethods.GetSize(), OLE2T(bstrMethodName));
}
return piappObj->EndMethodEnumeration();
}
AcWMI.h :
#if !defined (AFX_AC_WMI_H__INCLUDED_)
#define AFX_AC_WMI_H__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <comdef.h>
#include <Wbemidl.h>
#include <atlbase.h>
#include <atlconv.h>
#include <afxtempl.h>
#ifndef OUT
#define OUT
#endif
#ifndef IN
#define IN
#endif
HRESULT ConnectServerWMI (OUT IWbemLocator** ppiWmiLoc ,
OUT IWbemServices** ppiWmiServ ,
const CString csNamespace ,
const CString csUsername ,
const CString csPassword ,
CString& csErrRef );
HRESULT EnumInstancesWMI (IN IWbemServices* piWmiServ ,
OUT IEnumWbemClassObject** ppiWmiEnum ,
const CString& csInstName ,
CString& csErrRef );
HRESULT GetLastErrorWMI (OUT CString& csErrRef,
IN HRESULT hresErr);
HRESULT EnumInstPropNameWMI ( IN IWbemClassObject* piappObj,
OUT CStringArray& psarPropRef );
HRESULT EnumInstPropNameWMI ( IN IWbemClassObject* piappObj,
OUT LPSAFEARRAY* psarProp );
HRESULT ExecMethodWMI (IN IWbemServices* piWmiServ ,
const CString& csMethodName,
const CString& csClassName ,
const CString& csClassPath ,
const CString& csObjectPath,
const CStringArray* csarrPropNames = NULL,
VARIANT *arrVarInArg = NULL, const int size_t = 0);
HRESULT GetClassMethodsWMI (IN IWbemClassObject* piappObj,
OUT CStringArray& csarrMethods);
#endif // AFX_AC_WMI_H__INCLUDED_
...
..
.
?