WBEM Class Wrapper for Local and Remote Process Creation | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 6, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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(LWin32_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(LCommandLine”),
                           0,
                           &vCommandLine,
                           NULL);
 if(FAILED(hRes))
 {
  return E_FAIL;
 }
 hRes = pService->ExecMethod(_bstr_t(LWin32_Process”),
                             _bstr_t(LCreate”),
                              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 RootDefault 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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.