developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Calling Cards
Condos For Sale
Computer Deals
Build a Server Rack
Boat Donations
Online Shopping
KVM over IP
Televisions
Web Hosting Directory
Memory Upgrades
Data Center Solutions
Career Education
Baby Photo Contest
Shop

 


Download these IBM resources today!
e-Kit: IBM Rational Systems Development Solution
With systems teams under so much pressure to develop products faster, reduce production costs, and react to changing business needs quickly, communication and collaboration seem to get lost. Now, theres a way to improve product quality and communication.

Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.

eKit: Rational Build Forge Express
Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.

Download: IBM Data Studio v1.1
Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

eKit: Rational Asset Manager
Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Best Practices for Developing a Web Site: Checklists, Tips, Strategies & More. Download Exclusive eBook Now.

WBEM Class Wrapper for Local and Remote Process Creation
By Brandon Clark

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 endeavour. 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. Although 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


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Visual C++ Archives

Five Trends for Application Development & Program Management. Download Complimentary Report Now.
Whitepaper: Embeddable Content Platform for OEM's
Whitepaper: XML Processing in Applications--Take the Next Step
Whitepaper: Enterprise Information Integration--Deployment Best Practices for Low-Cost Implementation
Data Sheet: IBM Information Server Blade



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES