How to add a user to Terminal Services RDP permissions by using WMIon LongHorn server

  • Thread starter Thread starter Sanil
  • Start date Start date
S

Sanil

Guest
Hi,
I am trying to add a user to Terminal services RDP-Tcp
permission / security by using WMI. for that purpose i am using
Win32_TSPermissionSetting class and it's AddAccount() method.
I have tried a lot but ExecMethod fails telling me invalid parameter.

Any help / suggestion will be appreciated

below is the sample code,

#include "stdafx.h"

#define TERMINAL_SERVER_ROOT _T("root\\cimv2\
\TerminalServices")

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HRESULT hr = E_FAIL;

//Initialize COM
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if(FAILED(hr))
{
OutputDebugString(_T("TSEUtil::Failed to CoInitialize
"));
}

// Obtain the initial locator to WMI
IWbemLocator *pLoc = NULL;
hr = CoCreateInstance(CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if(FAILED(hr))
{
OutputDebugString(_T("TSEUtil::CoCreateInstance for
IWbemLocator
Failed"));
}

IWbemServices *pSvc = NULL;
hr = pLoc->ConnectServer( _bstr_t(TERMINAL_SERVER_ROOT), NULL,
NULL,
0, NULL, 0, 0, &pSvc);
if(FAILED(hr))
{
OutputDebugString(_T("TSEUtil::CoCreateInstance for
IWbemServices
Failed"));
}

// Set security levels on the proxy
hr = 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_PKT_PRIVACY, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx

NULL, // client
identity
EOAC_NONE //
proxy capabilities
);

IWbemClassObject *pclsInstObj = NULL;

BSTR MethodName = SysAllocString(L"AddAccount");
BSTR ClassName = SysAllocString(L"Win32_TSPermissionsSetting");

//Get object
IWbemClassObject* pClass = NULL;
hr = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);

if (FAILED (hr))
return FALSE;

VARIANT varTerminalName;
VariantInit(&varTerminalName);
varTerminalName.vt = VT_BSTR;
varTerminalName.bstrVal = L"RDP-Tcp";

hr = pClass->Put(L"TerminalName", 0,&varTerminalName, 0);

if (FAILED (hr))
return FALSE;

//Get method
IWbemClassObject* pInParamsDefinition = NULL;
hr = pClass->GetMethod(MethodName, 0, &pInParamsDefinition, NULL);

if (FAILED (hr))
return FALSE;

//Spawn instance
IWbemClassObject* pClassInstance = NULL;
hr = pInParamsDefinition->SpawnInstance(0, &pClassInstance);

if (FAILED (hr))
return FALSE;

//Set the method parameters
VARIANT varAccountName;
VariantInit(&varAccountName);
varAccountName.vt = VT_BSTR;
varAccountName.bstrVal = L"eng\\sanilt";

hr = pClassInstance->Put(L"AccountName", 0,&varAccountName,
0);

if (FAILED (hr))
return FALSE;

VARIANT var;
VariantInit(&var);
V_VT(&var) = VT_I4;
var.intVal = 2;

hr = pClassInstance->Put(L"PermissionPreSet", 0, &var, 0);

if (FAILED (hr))
return FALSE;

IWbemClassObject* pOutParams = NULL;
hr = pSvc->ExecMethod(ClassName, MethodName, 0,NULL,
pClassInstance, &pOutParams, NULL);

if(FAILED(hr))
{
MessageBox(NULL,_T("Exec Method
Failed"),_T(""),MB_OK);
}

return 0;
}
 
Back
Top