From: Rasheed on
[Even i dont know this query is related to this forum or not. If not
please tell me related forum]

Hi all,

I need to check Terminal Services is Enable or not on a remote
machine. I find one link from this Forum. link is as below :

http://www.pcreview.co.uk/forums/thread-1609344.php

and i have get the code as below and executed.

BOOL Is_Terminal_Services ()

{

OSVERSIONINFOEX osvi;

DWORDLONG dwlConditionMask = 0;

// Initialize the OSVERSIONINFOEX structure.

ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));

osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

osvi.wSuiteMask = VER_SUITE_TERMINAL | VER_SUITE_SINGLEUSERTS;

// Initialize the condition mask.

VER_SET_CONDITION( dwlConditionMask, VER_SUITENAME, VER_OR );

// Perform the test.

return VerifyVersionInfo(

&osvi,

VER_SUITENAME,

dwlConditionMask);

}

int _tmain(int argc, _TCHAR* argv[])

{

bool bflag = Is_Terminal_Services () ;

}

But even I am unable to analyze. Because if my Terminal service is
stopped then it returns 0 else it returns 1.

But my Terminal service is not enable in Control Panel -> Add and
Remove Programs -> Add / Remove Windows Components -> Terminal
Services.

So I need the check the above thing(which is Control Panel -> Add and
Remove Programs -> Add / Remove Windows Components -> Terminal
Services. is Enable or Not) whether Terminal Services are enable or
not so what I need to Do. Kindly provide any code snippet will be
great.

From: Christian ASTOR on
Rasheed wrote:

> I need to check Terminal Services is Enable or not on a remote
> machine.

TSEnabled (KB243215), Win32_TerminalServiceSetting,
QueryServiceConfig(), ...
From: Rasheed on
Hi ,

Christian thanks for the reply,

but i have used the wmi class "Win32_TerminalServiceSetting" but
always i am going to geting the "AllowTSConnections" value as true
(1). even after my Terminal service is stop and Runing and Disabling
the services manually.

So can you any one please explain me. how can my terminal services
settings Enable and Disable. becuase when i Disable the service on my
machine, "AllowTSConnections" value sholud be false(0). but my code
returns True. Can you please explain how to get conform that my
service is enable or not.

please find the my snippet:

HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (SUCCEEDED(hr))
{
// Initialize Security
hr = CoInitializeSecurity(
NULL,
-1, // COM negotiates service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
}

//_bstr_t("\\inhyd7074\\ROOT\\CIMV2") ;// WMI namespace
//_bstr_t("FOXBORO_1\\rasheed.shaik"); // User name
//_bstr_t("rasheed"); // User password


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("ROOT\\CIMV2");
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("Connected to %s WMI namespace.\n",
static_cast(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(pLocator != NULL)
pLocator->Release();
if(pNamespace != NULL)
pNamespace->Release();
CoUninitialize();
From: Rasheed on
thanks Christian