开启远程桌面编程方法-wmic

来源:互联网 发布:js下拉框选中触发事件 编辑:程序博客网 时间:2024/06/13 01:06

之前的文章中提到使用编程的方法开启远程桌面

以下附上完整的code,编译通过

 

 

#include "stdafx.h"

 

#include "atlbase.h"
#include "Wbemcli.h"
#include "comutil.h"
#include <comutil.h>
#include <stdio.h>

#pragma comment(lib, "comsuppw.lib")
#pragma comment(lib, "kernel32.lib")


#define _WIN32_DCOM
using namespace std;

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


void initeCom()
{
 HRESULT hr = CoInitializeEx(0,COINIT_MULTITHREADED);
 if(SUCCEEDED(hr))
 {
  hr = CoInitializeSecurity(
   NULL,
   -1,
   NULL,
   NULL,
   RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
   RPC_C_IMP_LEVEL_IMPERSONATE,
   NULL,
   EOAC_NONE,
   NULL);
   
 }

 IWbemLocator *pLocator = NULL;
 if (SUCCEEDED(hr))
  {  
   // Obtain the initial locator to Windows Management  
   // on a particular host computer.  
   hr = CoCreateInstance(
    CLSID_WbemLocator,      
    0,      
    CLSCTX_INPROC_SERVER,       
    IID_IWbemLocator,
    (LPVOID *) &pLocator);
  }
  IWbemServices *pNamespace = NULL;
  _bstr_t sNamespace(L"ROOT\\CIMV2\\TERMINALSERVICES");//root/cimv2/TerminalServices
  if (SUCCEEDED(hr))
  {   
   // Connect to the root\cimv2 namespace with the   
   // current user and obtain pointer pSvc   
   // to make IWbemServices calls.   
   hr = pLocator->ConnectServer(      
    sNamespace,             
    // WMI namespace       
    NULL,                      
    // User name      
    NULL,                      
    // User password       
    0,                          
    // Locale       
    NULL,                     
    // Security flags     
    0,                           
    // Authority       
    0,                   
    // Context object     
    &pNamespace   
    // IWbemServices proxy      
    );
  }
  if(SUCCEEDED(hr))   
   {
    wprintf(L"Connected to %s WMI namespace.\n", sNamespace);
   }
  if (SUCCEEDED(hr))
  {   
   // Set the IWbemServices proxy so that impersonation   
   // of the user (client) occurs.  
   hr = CoSetProxyBlanket(      
    pNamespace,                  
    // the proxy to set       
    RPC_C_AUTHN_WINNT,           
    // authentication service       
    RPC_C_AUTHZ_NONE,            
    // authorization service     
    NULL,                      
    // Server principal name    
    RPC_C_AUTHN_LEVEL_PKT_PRIVACY,    
    // authentication level      
    RPC_C_IMP_LEVEL_IMPERSONATE, 
    // impersonation level      
    NULL,                       
    // client identity      
    EOAC_NONE                   
    // proxy capabilities      
    );
  }
  IEnumWbemClassObject* pEnumerator = NULL;
  if(SUCCEEDED(hr))
  {  
   hr = pNamespace->ExecQuery(
    bstr_t("WQL"),   
    bstr_t("SELECT * FROM Win32_TerminalServiceSetting"),  
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
    NULL,      
    &pEnumerator);
  }
  IWbemClassObject * pObject = NULL;
  ULONG uReturn = 0;

  while (pEnumerator)
  {   
   hr = pEnumerator->Next(WBEM_INFINITE,
    1,       
    &pObject,
    &uReturn);   
   if(0 == uReturn)   
   {      
    break;  
   } 
   if(SUCCEEDED(hr)) 
   {      
    // Get the current Value of AllowTSConnections  
    VARIANT v;     
    hr = pObject->Get(L"AllowTSConnections",0,&v,0,0);  
    if (SUCCEEDED(hr) && (V_VT(&v) == VT_I4))    
    {           
     wprintf(L"AllowTSConnections = %d.\n", V_I4(&v));   
    }     
    else     
    {         
     wprintf(L"Error in getting specified object\n"); 
    }     
    VariantClear(&v);
   }  
   if(SUCCEEDED(hr))  
   {     
    // Call the method SetAllowTSConnections(1)   
    _bstr_t sMethodName = L"SetAllowTSConnections";  
    _bstr_t sClassName = L"Win32_TerminalServiceSetting";   
    IWbemClassObject* pClass = NULL;   
    hr = pNamespace->GetObject(sClassName, 0, NULL, &pClass, NULL);   
    IWbemClassObject* pInParamsDefinition = NULL;    
    hr = pClass->GetMethod(sMethodName, 0, &pInParamsDefinition, NULL);  
    IWbemClassObject* pClassInstance = NULL;  
    hr = pInParamsDefinition->SpawnInstance(0, &pClassInstance); 
    // Create the values for the in parameters     
    VARIANT v;     
    V_VT(&v) = VT_I4;     
    V_I4(&v) = 1;      
    // Store the value for the in parameters     
    hr = pClassInstance->Put(L"AllowTSConnections", 0, &v, 0);   
    // Get the context from which to execute the method     
    CIMTYPE pType;    
    LONG pFlavor;      
    VARIANT var;    
    hr = pObject->Get(L"__PATH", 0, &var, &pType, &pFlavor);   
    // Execute Method     
    IWbemClassObject* pOutParams = NULL; 
    hr = pNamespace->ExecMethod(var.bstrVal, sMethodName, 0,     
     NULL, pClassInstance, &pOutParams, NULL);   
    if (SUCCEEDED(hr))      
    {        
     wprintf(L"SetAllowTSConnections(%d) OK.\n", V_I4(&v));
    }      
    else   
    {       
     wprintf(L"SetAllowTSConnections(%d) FAILED.\n", V_I4(&v)); 
    }    
    VariantClear(&v); 
    VariantClear(&var);  
   }
  }
  if(pLocator != NULL) 
   pLocator->Release();
  if(pNamespace != NULL) 
   pNamespace->Release();
  CoUninitialize();
}

 

 

 

原创粉丝点击