RE: Using StdRegProv::SetDWORDValue in C++

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hey Luke,

Yes, I know what you mean ;-) For the hDefKey attribute, set:
varCommand.vt = VT_I4;
varCommand.lVal = (long)HKEY_LOCAL_MACHINE;

You can, of course, use the value. I just put the HKEY define for
readability. Anyway, I believe it is VT_I4 that you were looking for.

-Brian

"luke_21" wrote:

Hi,

Here is my code to change a registry DWORD value on a foreign box by
WMI.

IWbemLocator *ppiWmiLoc = NULL;

hres = CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID *) &ppiWmiLoc);

IWbemServices *pSvc = NULL;
CComBSTR bstrserv(L"\\\\MyComputer\\root\\default");
CComBSTR bstrUsername(m_csUserName), bstrPassword(m_csPassword);

hres = ppiWmiLoc->ConnectServer(bstrserv, NULL, NULL,0, NULL, 0, 0,
&pSvc);
if(SUCCEEDED(hres))
{
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);

if(SUCCEEDED(hres))
{
// Connect to the local root\cimv2 namespace
// and obtain pointer pSvc to make IWbemServices calls.
BSTR MethodName = SysAllocString(L"SetDWORDValue");
BSTR ClassName = SysAllocString(L"StdRegProv");

IWbemClassObject* pClass = NULL;
hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);

if(SUCCEEDED(hres))
{
IWbemClassObject* pInParamsDefinition = NULL;
hres = pClass->GetMethod(MethodName, 0,
&pInParamsDefinition, NULL);

if(SUCCEEDED(hres))
{
IWbemClassObject* pClassInstance = NULL;
hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);

if(SUCCEEDED(hres))
{
/*// Create the values for the in parameters
VARIANT varCommand;
varCommand.vt = VT_UINT;
varCommand.uintVal = &H80000002;

// Store the value for the in parameters
hres = pClassInstance->Put(L"hDefKey", 0,
&varCommand, 0);*/

// Create the values for the in parameters
VARIANT varCommand;
varCommand.vt = VT_BSTR;
varCommand.bstrVal = L"System\CurrentControlSet\Control\Lsa";

// 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"ForceGuest";

// 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_I4;
varCommand.lVal = 0;

// Store the value for the in parameters
hres = pClassInstance->Put(L"lValue", 0,
&varCommand, CIM_UINT8);

// 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;*/
}

pClassInstance->Put function needs a Variant. So
I have to pass a dword, but by the variant. As I passed my string
value by Variant : var.vt = VT_BSTR and then var.bstrval =
L"MyVariant"; Do you know what I mean ? I have to find the exact type
I have to use and the exact format I have to use for my DWORD value.

IWbemClassObject* pClassInstance;


HRESULT Put(
LPCWSTR wszName,
LONG lFlags,
VARIANT* pVal,
CIMTYPE vtType
);


.



Relevant Pages

  • RE: IAzClientContext AccessCheck returns 0x80070057
    ... I get a successful access check. ... Could you post a text representation of your XML store? ... > VARIANT varOperationIdArray; ...
    (microsoft.public.platformsdk.security)
  • Re: Invalid use of Null error question
    ... string variable if nothing's been selected: string variables cannot be set ... only store numbers (Byte, Integer and Long can only store integer values, ... Variant is another type of variable. ... Dim strsql As String ...
    (microsoft.public.access.formscoding)
  • Using StdRegProv::SetDWORDValue in C++
    ... CComBSTR bstrUsername, bstrPassword; ... IWbemClassObject* pInParamsDefinition = NULL; ... VARIANT varCommand; ... I have to pass a dword, ...
    (microsoft.public.win32.programmer.wmi)
  • Re: Advice on storage and retrieval
    ... I guess in that interface i was already thinking about implemening using ... ...don't I need Item (as variant) to store and return the actual "thing" ... ..Item as String ...
    (microsoft.public.vb.general.discussion)
  • How to add a user to Terminal Services RDP permissions by using WMI on LongHorn server
    ... permission / security by using WMI. ... // Obtain the initial locator to WMI ... IWbemClassObject *pclsInstObj = NULL; ... VARIANT varTerminalName; ...
    (microsoft.public.windows.terminal_services)