WBEM Class Wrapper for Local and Remote Process Creation

Environment: Windows 2000, Visual C++ 6 SP3

Windows 2000 introduces many new great features for us system programmers. Perhaps the best of these features is Microsoft's implementation of WBEM. WBEM is an industry wide initiative to make system management a more cross platform endevour. WBEM is incorporated deeply into Windows 2000, and is the facility by which all local and remote system information is gathered.

WBEM is implemented on the Windows 2000 platform as a system service. This service acts as a object request broker for almost any object in Windows (i.e. Process, Eventlog, Perfmon counters, Drivers, and so on) and can be used to create, query, or modify any of these objects via well documented COM calls.

Below I have implemented a class the will allow the creation of any specified process on a remote or local machine. I have included a test project implemented via a simple MFC dialog. Althought simplistic, the source should give you an idea of what can be accomplished with this new technology.

Make sure that you have included wbemuuid.lib in your linker settings. (This comes with platform SDK)

#include "wbemcli.h"
#include "comdef.h"

class CProcess
{

private:
 IWbemLocator     *pLocator;
 IWbemServices    *pService;
 IWbemClassObject *pInParameters;
 IWbemClassObject *pMethodObject;
 STDMETHODIMP     ConnectToWbem(_bstr_t bstrMachine, 
                                _bstr_t bstrUserID, 
                                _bstr_t bstrPassword);

public:
 CProcess();
 ~CProcess();
 STDMETHODIMP CreateNewProcess(_bstr_t bstrMachine, 
                               _bstr_t bstrUserID, 
                               _bstr_t bstrPassword, 
                               _variant_t vCommandLine);

};

CProcess::CProcess()
{
 CoInitialize(NULL);
}

CProcess::~CProcess()
{
 pInParameters->Release();
 pMethodObject->Release();
 pLocator->Release();
 pService->Release();
 CoUninitialize();
}

STDMETHODIMP CProcess::CreateNewProcess(_bstr_t bstrMachine, 
                                        _bstr_t, 
                                        bstrUserID, 
                                        _bstr_t bstrPassword, 
                                        _variant_t vCommandLine)
{
 HRESULT          hRes = 0;
 IWbemClassObject *pProcess = NULL;
 IWbemClassObject *pOutInst = NULL;

 if(FAILED(ConnectToWbem(bstrMachine, bstrUserID, bstrPassword)))
 {
  return E_FAIL;
 }

 hRes = pService->GetObject(_bstr_t(L"Win32_Process"), 
                            0, 
                            NULL, 
                            &pProcess, 
                            NULL);
 if(FAILED(hRes))
 {
  return E_FAIL; // Program has failed
 }

 hRes = pProcess->GetMethod(_bstr_t("Create"), 
                            0, 
                            &pInParameters, 
                            NULL);
 if(FAILED(hRes))
 {
  return E_FAIL;
 }

 hRes = pInParameters->SpawnInstance(0, &pMethodObject);
 if(FAILED(hRes))
 {
  return E_FAIL;
 }

 hRes = pMethodObject->Put(_bstr_t(L"CommandLine"), 
                           0, 
                           &vCommandLine, 
                           NULL);
 if(FAILED(hRes))
 {
  return E_FAIL;
 }

 hRes = pService->ExecMethod(_bstr_t(L"Win32_Process"),
                             _bstr_t(L"Create"), 
                              0, 
                              NULL, 
                              pMethodObject, 
                              &pOutInst, 
                              NULL);
 if(FAILED(hRes))
 {
  return E_FAIL;
 }

 pProcess->Release();
 pOutInst->Release();
 return S_OK;
}

STDMETHODIMP CProcess::ConnectToWbem(_bstr_t bstrMachine, 
                                     _bstr_t, 
                                     bstrUserID, 
                                     _bstr_t bstrPassword)
{
 HRESULT		hRes = 0;
 _bstr_t		szConnectionPath;

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

 if (FAILED(hRes))
 {
  return E_FAIL; // Program has failed.
 }

 if(SysStringLen(bstrMachine) == 0)
 {
  //connect to local machine
  szConnectionPath += "root\\cimv2"; 
 }
 else
 {
  //set up connection string for remote machine
  szConnectionPath += "\\\\";
  szConnectionPath += bstrMachine;
  szConnectionPath += "\\root\\cimv2";
 }

 //if no UserName and PassWord supplied use NTLM to 
 //grab users access token
 if((SysStringLen(bstrUserID) == 0) 
 && (SysStringLen(bstrPassword) == 0))
 {
  hRes = pLocator->ConnectServer(szConnectionPath, 
                                 NULL, 
                                 NULL, 
                                 0, 
                                 NULL,	
                                 0, 
  0, &pService);
  if FAILED(hRes)
  {
   return E_FAIL; // Program has failed.
  }
 }
 else
 {
  // Connect to the Root\Default namespace with the current user.
  hRes = pLocator->ConnectServer(szConnectionPath, 
                                 bstrUserID, 
                                 bstrPassword, 
                                 0, 
                                 NULL, 
                                 0, 
                                 0, 
                                 &pService);
  if FAILED(hRes)
  {
   return E_FAIL;	   // Program has failed.
  }
 }

 // Set the proxy so that impersonation of the client occurs.
 hRes = 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(FAILED(hRes))
 {
  //user impersonation has failed!
  return E_FAIL;	   // Program has failed.
 }

 return S_OK;
}

Downloads

Download demo project - 12 Kb
Download source - 2 Kb


Comments

  • GGSqu Xfg rRTB

    Posted by NfRmSTMwVs on 11/14/2012 11:33am

    cheap soma carisoprodol urine test - carisoprodol mechanism

    Reply
  • Errors on attempt to connect to remote machine

    Posted by Legacy on 10/19/2001 12:00am

    Originally posted by: Tom M.

    After this line in the sample source class:
    
    

    hRes = pLocator->ConnectServer( szConnectionPath, NULL, NULL, 0, NULL, 0, 0, &pService );

    I get these errors:

    hRes = 0x8001011c = "Remote Calls are not allowed for this process".
    hRes = 0x80040154 = "Class not registered".

    The first error is when trying to call another computer on the network that also is running Windows 2000 professional.

    The second is when trying to call a WindowsNT4 Server machine.

    I haven't been able to find any ideas on what these error messages mean, and how to correct them. Maybe someone can shed some light on the problem? Do I have to have the remote machine configured in any certain fashion?

    Thanks,

    Tom

    Reply
  • BEM Class Wrapper for Local and Remote Process Creation

    Posted by Legacy on 10/05/2001 12:00am

    Originally posted by: RamprasadT

    The class provided CProcess does not seem to be working with a remote system, working on local system though. The information I have provided is

    Machine Name: SURESH
    CommandLine: Notepad
    UserID: SURESH\administrator
    Password: ******

    It seems to be connecting to the local system, and doing everything else alright, but is unable to start the process on the remote system.

    Provide some help on this please.

    Reply
  • How to terminate the process

    Posted by Legacy on 06/04/2001 12:00am

    Originally posted by: kan

    I try the "Terminate" metod of Win32_Process object, but always fail. Could anyone give me a demo?

    Reply
  • Get ReturnValue

    Posted by Legacy on 04/17/2001 12:00am

    Originally posted by: Remo Laubacher

    Nice Code! I'm trying to get the property "ReturnValue"... But How? ReturnValue is the standard value which will be returned after the execution of a method (e.g "Install" from Win32_Product) I need this for writing my history, this is just that I know what happened..

    Hope there is someone which understand my problem ;-)

    Thanks
    Remo

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • The hard facts on SaaS adoption in over 80,000 enterprises: Public vs. private companies Mid-market vs. large enterprise GoogleApps, Office365, Salesforce & more Why security is a growing concern Fill out the form to download the full cloud adoption report.

Most Popular Programming Stories

More for Developers

RSS Feeds