I'd like to remotely set a value in Registry, using WMI methods as
StdRegProv:SetStringValue for example.
I tried using MSDN example, Method execution is correct, but
SetStringValue return me code errors I can't analyse or understand. Is
somebody could help me to understand what i am doing wrong ?
IWbemLocator *ppiWmiLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &ppiWmiLoc);
IWbemServices *pSvc = NULL;
CComBSTR bstrserv(L"\\\\RemoteComputer\\root\\default");
CComBSTR bstrUsername(m_csUserName), bstrPassword(m_csPassword);
hres = ppiWmiLoc->ConnectServer(bstrserv, NULL, NULL,0, NULL, 0, 0,
&pSvc);
//AfficheErreur(hres);
BSTR MethodName = SysAllocString(L"SetStringValue");
BSTR ClassName = SysAllocString(L"StdRegProv");
IWbemClassObject* pClass = NULL;
hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);
IWbemClassObject* pInParamsDefinition = NULL;
hres = pClass->GetMethod(MethodName, 0,
&pInParamsDefinition, NULL);
IWbemClassObject* pClassInstance = NULL;
hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);
// Create the values for the in parameters
VARIANT varCommand;
varCommand.vt = VT_BSTR;
varCommand.bstrVal = L"SOFTWARE\\Computer\\Luke";
// Store the value for the in parameters
hres = pClassInstance->Put(L"sSubKeyName", 0,
&varCommand, 0);
// Create the values for the in parameters
varCommand.vt = VT_BSTR;
varCommand.bstrVal = L"str";
// Store the value for the in parameters
hres = pClassInstance->Put(L"sValueName", 0,
&varCommand, 0);
// Create the values for the in parameters
varCommand.vt = VT_BSTR;
varCommand.bstrVal = L"toto";
// Store the value for the in parameters
hres = pClassInstance->Put(L"sValue", 0,
&varCommand, 0);
// Execute Method
IWbemClassObject* pOutParams = NULL;
hres = pSvc->ExecMethod(ClassName, MethodName, 0,
NULL, pClassInstance, &pOutParams, NULL);
if (FAILED(hres))
{
MessageBox("Command Failed","SetStringValue",MB_OK);
/* VariantClear(&varCommand);
SysFreeString(ClassName);
SysFreeString(MethodName);
pClass->Release();
pInParamsDefinition->Release();
pOutParams->Release();
pSvc->Release();*/
}
else
{
VARIANT varReturnValue;
CComBSTR bstrret(L"ReturnValue");
BSTR ret;
hres = pOutParams->GetObjectText(0, &ret);
char mess[MAX_PATH];
wsprintf(mess,"%S",ret);
MessageBox(mess,"Error message",MB_OK);
/*hres = pOutParams->Get(bstrret, 0, &varReturnValue, NULL, 0);
SHORT toto = varReturnValue.iVal;*/
hres = ppiWmiLoc->ConnectServer(bstrserv, NULL, NULL, 0, NULL, 0, 0, &pSvc)
You need to call:
hRes = CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE,
NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
That should get you fixed up (I hope).
Later,
Chris
That works perfectly !!!!
Thank you very much.
I found no code that describe that, very few examples in C++.
I have another problem, using SetDWORDValue method.
As I had :
// Create the values for the in parameters
varCommand.vt = VT_BSTR;
varCommand.bstrVal = L"toto";
// Store the value for the in parameters
hres = pClassInstance->Put(L"sValue", 0,
&varCommand, 0);
I'd like to set "lValue" of SetDWORDValue method to 0 or 1. But can't
find type of variant to set it ... I tried VT_UINT, VT_I8 and so
on ... but I got always same -2147217403 error ....
varCommand.vt = VT_UINT;
varCommand.uintVal = 1;
// Store the value for the in parameters
hres = pClassInstance->Put(L"lValue", 0,
&varCommand, 0);
Need some help again...
On 5 mar, 01:00, Chris Richards <g...@aoaforums.com> wrote:
Later,
Chris
IWbemClassObject* pClassInstance;
HRESULT Put(
LPCWSTR wszName,
LONG lFlags,
VARIANT* pVal,
CIMTYPE vtType
);
> >> - Afficher le texte des messages précédents -- Masquer le texte des messages précédents -
Have you tried setting the value into the variant as a BSTR? I've
noticed that some of the WMI classes are documented as returning int32
or something like that, but in fact return an int32 value EXPRESS AS A
STRING.
That being the case, it is not beyond the realm of reason that it might
be expecting the DWORD value expressed as a string in the BSTR element.
Just a thought.
Later,
Chris