Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I am using this code present at address:

https://learn.microsoft.com/en-us/windows/win32/wmisdk/example--calling-a-provider-method.

I really need to execute ReArmWindows method present in SoftwareLicensingService class, simple command without parameters.

I really cannot do it. Can please you help me.

Giovanni Salemme

Ok Thanks for interest as always this is code:

int main(int iArgCnt, char** argv)
{
    HRESULT hres;

    hres = CoInitializeEx(0, COINIT_MULTITHREADED);
    if (FAILED(hres))
        return 1;

    hres = CoInitializeSecurity(
        NULL,
        -1,                          // COM negotiates service
        NULL,                        // Authentication services
        NULL,                        // Reserved
        RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
        NULL,                        // Authentication info
        EOAC_NONE,                   // Additional capabilities 
        NULL                         // Reserved
    );


    if (FAILED(hres))
    {
        CoUninitialize();
        return 1;
    }

    IWbemLocator* pLoc = NULL;

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

    if (FAILED(hres))
    {
        CoUninitialize();
        return 1;                 // Program has failed.
    }

    IWbemServices* pSvc = NULL;

    hres = pLoc->ConnectServer(
        _bstr_t(L"ROOT\\CIMV2"),
        NULL,
        NULL,
        0,
        NULL,
        0,
        0,
        &pSvc
    );

    if (FAILED(hres))
    {
        pLoc->Release();
        CoUninitialize();
        return 1;                // Program has failed.
    }

    cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;

    hres = CoSetProxyBlanket(
        pSvc,                        // Indicates the proxy to set
        RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx 
        RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx 
        NULL,                        // Server principal name 
        RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
        RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
        NULL,                        // client identity
        EOAC_NONE                    // proxy capabilities 
    );

    if (FAILED(hres))
    {
        cout << "Could not set proxy blanket. Error code = 0x"
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;               // Program has failed.
    }

    // set up to call the Win32_Process::Create method
    BSTR MethodName = SysAllocString(L"ReArmWindows");
    BSTR ClassName = SysAllocString(L"SoftwareLicensingService");

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

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

    hres = pSvc->ExecMethod(ClassName, MethodName, WBEM_FLAG_RETURN_IMMEDIATELY, NULL, NULL, NULL, NULL);

    if (FAILED(hres))
    {
        SysFreeString(ClassName);
        SysFreeString(MethodName);
        pClass->Release();
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;
    }

    // Clean up
    //--------------------------
    SysFreeString(ClassName);
    SysFreeString(MethodName);
    pClass->Release();
    pLoc->Release();
    pSvc->Release();
    CoUninitialize();
    return 0;
}


pSvc->ExecMethod


Return S_OK bu no effect

What I have tried:

I tried hres = pSvc->ExecMethod(ClassName, MethodName, WBEM_FLAG_RETURN_IMMEDIATELY, NULL, pClassInstance, &pOutParams, NULL);

This istruction returns S_OK but really don't do nothing.
Posted
Updated 30-May-23 10:04am
v2
Comments
Richard MacCutchan 29-May-23 3:45am    
Without more details it is impossible to guess what you may be doing wrong. Please use the Improve question link above, and add complete details of what is not working.
Richard MacCutchan 29-May-23 6:53am    
I cannot see the name "ReArmWindows" in the licence details of my system. What is it supposed to represent?
Drakesal 29-May-23 17:26pm    
This command will try to reset Windows license, so he will propose you to restart the computer
Richard MacCutchan 30-May-23 5:41am    
See my suggested solution below.

The "rearm" command can only be used to extend your trial a maximum of three times. Once that is exceeded, you cannot extend it again.

The solution is obvious: stop trying to bypass the activation, and actually purchase a valid license for Windows!
 
Share this answer
 
Comments
Drakesal 30-May-23 4:59am    
Do not consider it like a try to crack software, I'd like only to manage the slmgr.vbs methods in c++ program without call it with createprocess for istance, this is all :)
Thanks Richard as always I will try your solution too, but I have found the solution and i wrote this complete function It does this operation, accept in input The Class, The Method and a string formatted.

During execution check if method needs parameters, if yes, check in the parameters string if found corrent association insert the value in that parameters, and then run the command.

This function with all methods present in SoftwareLicensingService I made it to be general purpose.

#include <comdef.h>
#include <Wbemidl.h>

#pragma comment(lib, "wbemuuid.lib")

HRESULT RunWMIMethod(LPCWSTR pszClass, LPCWSTR pszMethod, LPCWSTR pszParameters)
{
    HRESULT hr = S_OK;

    // Step 1: --------------------------------------------------
    // Initialize COM. ------------------------------------------
    // If hr = S_FALSE already initialized so we can continue.

    hr = CoInitializeEx(0, COINIT_MULTITHREADED);

    hr = CoInitializeEx(0, COINIT_MULTITHREADED);

    if (FAILED(hr) && hr != S_FALSE)
        return hr;

    // Step 2: --------------------------------------------------
    // Set general COM security levels --------------------------
    // If hr = RPC_E_TOO_LATE already called so we can continue.

    hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);

    if (FAILED(hr) && hr != RPC_E_TOO_LATE)
    {
        CoUninitialize();

        return hr;
    }

    // Step 3: ---------------------------------------------------
    // Obtain the initial locator to WMI -------------------------

    IWbemLocator* pLocator = NULL;

    hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLocator);

    if (SUCCEEDED(hr))
    {
        // Step 4: -----------------------------------------------------
        // Connect to WMI through the IWbemLocator::ConnectServer method

        IWbemServices* pService = NULL;

        // Connect to the local root\cimv2 namespace
        // and obtain pointer pSvc to make IWbemServices calls.
        hr = pLocator->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pService);

        if (SUCCEEDED(hr))
        {
            // Step 5: --------------------------------------------------
            // Set security levels for the proxy ------------------------

            hr = CoSetProxyBlanket(pService, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);

            if (SUCCEEDED(hr))
            {
                // Step 6: ______________________________________________
                // Enumerate the class Instace to get the correct name.
                // (this is the problem is MSDN example code,)

                IEnumWbemClassObject* pEnumClassInstances;
                
                hr = pService->CreateInstanceEnum(_bstr_t(pszClass), WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumClassInstances);

                if (SUCCEEDED(hr))
                {
                    IWbemClassObject* pEnumClassInstance;
                    ULONG uInstancesCount = 0;

                    hr = pEnumClassInstances->Next(10000, 1, &pEnumClassInstance, &uInstancesCount);

                    if (SUCCEEDED(hr))
                    {
                        VARIANT ClassPath;
                        VariantInit(&ClassPath);
                        ClassPath.vt = VT_BSTR;
                        
                        hr = pEnumClassInstance->Get(_bstr_t("__Path"), 0, &ClassPath, 0, 0);

                        if (SUCCEEDED(hr))
                        {
                            IWbemClassObject* pClassObJect = NULL;
                            
                            hr = pService->GetObject(_bstr_t(pszClass), 0, NULL, &pClassObJect, NULL);

                            if (SUCCEEDED(hr))
                            {
                                IWbemClassObject* pInParamsDefinition = NULL;
                                IWbemClassObject* pObjectParamsClassInstance = NULL;
                                
                                // Step 7: ___________________________________________
                                // Evalutate if method needs paramters

                                hr = pClassObJect->GetMethod(_bstr_t(pszMethod), 0, &pInParamsDefinition, NULL);

                                // If the Method hasn't parameters ExecMethod will receive NULL in pObjectParamsClassInstance.
                                if (SUCCEEDED(hr) && pInParamsDefinition != NULL && pszParameters != NULL)
                                {
                                    hr = pInParamsDefinition->SpawnInstance(0, &pObjectParamsClassInstance);

                                    if (SUCCEEDED(hr))
                                    {
                                        long lLower, lUpper, lCount;
                                        SAFEARRAY* psaNames = NULL;
                                        CString strPropertyName, strValueName;
                                        CString strParameters(pszParameters);
                                        int nPos = 0;

                                        // Step 8: ______________________________________
                                        // Extract from Object various Property name and compare with input <pszParameters> parsing
                                        // this format ProductKey=W269N-WFGWX-YVC9B-4J6C9-T83GX;Param2=Value;
                                        // when found assing value to correct Property name and insert in pObjectParamsClassInstance.

                                        hr = pObjectParamsClassInstance->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY, NULL, &psaNames);

                                        if (SUCCEEDED(hr))
                                        {
                                            if (SUCCEEDED(SafeArrayGetLBound(psaNames, 1, &lLower)) && SUCCEEDED(SafeArrayGetUBound(psaNames, 1, &lUpper)))
                                            {
                                                for (lCount = lLower; lCount <= lUpper; lCount++)
                                                {
                                                    BSTR PropName = NULL;

                                                    hr = SafeArrayGetElement(psaNames, &lCount, &PropName);

                                                    strPropertyName = PropName;

                                                    nPos = strParameters.Find(strPropertyName);

                                                    if (nPos != -1)
                                                    {
                                                        nPos += strPropertyName.GetLength();

                                                        strValueName = strParameters.Tokenize(_T("= ;"), nPos);

                                                        // Create the values for the in parameters
                                                        VARIANT CommandLine;
                                                        VariantInit(&CommandLine);
                                                        CommandLine.vt = VT_BSTR;
                                                        CommandLine.bstrVal = _bstr_t(strValueName);

                                                        // Store the value for the in parameters
                                                        hr = pObjectParamsClassInstance->Put(strPropertyName, 0, &CommandLine, 0);

                                                        VariantClear(&CommandLine);
                                                    }
                                                }
                                            }

                                            SafeArrayDestroy(psaNames);
                                        }
                                    }

                                    pInParamsDefinition->Release();
                                }

                                // Step 9: ____________________________________________
                                // Execute the method and return eventually the result.

                                IWbemClassObject* pResultsObject = NULL;
                                
                                hr = pService->ExecMethod(ClassPath.bstrVal, _bstr_t(pszMethod), 0, NULL, pObjectParamsClassInstance, &pResultsObject, NULL);

                                VariantClear(&ClassPath);

                                if (SUCCEEDED(hr))
                                {
                                    VARIANT ReturnValue;
                                    VariantInit(&ReturnValue);

                                    hr = pResultsObject->Get(_bstr_t(L"ReturnValue"), 0, &ReturnValue, NULL, 0);

                                    VariantClear(&ReturnValue);
                                }

                                if (pResultsObject)
                                    pResultsObject->Release();

                                if (pObjectParamsClassInstance)
                                    pObjectParamsClassInstance->Release();

                                pClassObJect->Release();
                            }
                        }

                        pEnumClassInstance->Release();
                    }

                    pEnumClassInstances->Release();
                }
            }

            pService->Release();
        }

        pLocator->Release();
    }

    CoUninitialize();

    return hr;
}

int main()
{
    //ReArmWindows
    //InstallProductKey

    CString strParameters(_T("ProductKey=AAAAA-BBBBB-CCCC-DDDD-EEEEE;"));

    RunWMIMethod(L"SoftwareLicensingService", L"InstallProductKey", strParameters);

    return 0;
}
 
Share this answer
 
Comments
Richard MacCutchan 31-May-23 3:55am    
Yes, but that is a total different issue.
I have taken the original code from the MSDN link and modified it to try and do what you want. As far as I can see, the following should work, although I have not tested the actual pSvc->ExecMethod call, as I have no intention of destroying or corrupting my licence settings.

C++
#define _WIN32_DCOM

#include <comdef.h>
#include <Wbemidl.h>

#pragma comment(lib, "wbemuuid.lib")

#include <iostream>
using namespace std;

int main(int iArgCnt, char** argv)
{
    HRESULT hres;

    // Step 1: --------------------------------------------------
    // Initialize COM. ------------------------------------------

    hres = CoInitializeEx(0, COINIT_MULTITHREADED);
    if (FAILED(hres))
    {
        cout << "Failed to initialize COM library. Error code = 0x"
            << hex << hres << endl;
        return 1;                  // Program has failed.
    }

    // Step 2: --------------------------------------------------
    // Set general COM security levels --------------------------

    hres = CoInitializeSecurity(
        NULL,
        -1,                          // COM negotiates service
        NULL,                        // Authentication services
        NULL,                        // Reserved
        RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
        NULL,                        // Authentication info
        EOAC_NONE,                   // Additional capabilities 
        NULL                         // Reserved
    );


    if (FAILED(hres))
    {
        cout << "Failed to initialize security. Error code = 0x"
            << hex << hres << endl;
        CoUninitialize();
        return 1;                      // Program has failed.
    }

    // Step 3: ---------------------------------------------------
    // Obtain the initial locator to WMI -------------------------

    IWbemLocator* pLoc = NULL;

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

    if (FAILED(hres))
    {
        cout << "Failed to create IWbemLocator object. "
            << "Err code = 0x"
            << hex << hres << endl;
        CoUninitialize();
        return 1;                 // Program has failed.
    }

    // Step 4: ---------------------------------------------------
    // Connect to WMI through the IWbemLocator::ConnectServer method

    IWbemServices* pSvc = NULL;

    // Connect to the local root\cimv2 namespace
    // and obtain pointer pSvc to make IWbemServices calls.
    hres = pLoc->ConnectServer(
        _bstr_t(L"ROOT\\CIMV2"),
        NULL,
        NULL,
        0,
        NULL,
        0,
        0,
        &pSvc
    );

    if (FAILED(hres))
    {
        cout << "Could not connect. Error code = 0x"
            << hex << hres << endl;
        pLoc->Release();
        CoUninitialize();
        return 1;                // Program has failed.
    }

    cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;


    // Step 5: --------------------------------------------------
    // Set security levels for the proxy ------------------------

    hres = CoSetProxyBlanket(
        pSvc,                        // Indicates the proxy to set
        RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx 
        RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx 
        NULL,                        // Server principal name 
        RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
        RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
        NULL,                        // client identity
        EOAC_NONE                    // proxy capabilities 
    );

    if (FAILED(hres))
    {
        cout << "Could not set proxy blanket. Error code = 0x"
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;               // Program has failed.
    }

    // Step 6: --------------------------------------------------
    // Use the IWbemServices pointer to make requests of WMI ----

    // set up to call the Win32_Process::Create method
    BSTR MethodName = SysAllocString(L"ReArmWindows");
    BSTR ClassName = SysAllocString(L"SoftwareLicensingService");

    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 = pClass->SpawnInstance(0, &pClassInstance);

    cout << "The instance is: 0x" << hex << pClassInstance << endl;

    // Execute Method
    hres = pSvc->ExecMethod(ClassName, MethodName, 0, NULL, pClassInstance, NULL, NULL);

    if (FAILED(hres))
    {
        cout << "Could not execute method. Error code = 0x"
            << hex << hres << endl;
    }

    // Clean up
    //--------------------------
    SysFreeString(ClassName);
    SysFreeString(MethodName);
    pClass->Release();
    pClassInstance->Release();
    if (pInParamsDefinition != NULL)
        pInParamsDefinition->Release();
    pLoc->Release();
    pSvc->Release();
    CoUninitialize();

    return hres != S_OK;
}
 
Share this answer
 
v3
Comments
Drakesal 30-May-23 16:32pm    
No Richard your code return this error WBEM_E_INVALID_METHOD_PARAMETERS.

Ok then my solution close the case thanks for all as always.
Richard MacCutchan 31-May-23 4:16am    
Well according to the documentation:

[in] pInParams

May be NULL if no in-parameters are required to execute the method. Otherwise, this points to an IWbemClassObject that contains the properties acting as inbound parameters for the method execution. The contents of the object are method-specific, and are part of the specification for the provider in question. For more information about constructing input parameters, see Creating Parameters Objects in C++.

And we know that the ReArmWindows method does not take any input parameters from ReArmWindows method of the SoftwareLicensingService class | Microsoft Learn[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900