使用 wtsapi32.dll 提供的 函数
//---------------------------------------------------------------------------
char* __fastcall Tfm_LoginList::GetWTSString(DWORD sID,WTS_INFO_CLASS wtsInfo)
{
LPTSTR p = NULL;
DWORD bufCount;
char* Result = NULL;
if(WTSQuerySessionInformation(0,sID,wtsInfo,&p,&bufCount))
{
Result = new char[bufCount];
strcpy(Result,p);
}
else
Result = "<Unknown>";
WTSFreeMemory(p);
return Result;
}
// 将获得的客户端显示在一个 listview 中
void __fastcall Tfm_LoginList::btnRefClick(TObject *Sender)
{
PWTS_SESSION_INFO pSession, pWTS_Info;
DWORD sID,SessionCount;
char uName[50]; //用户名称
String wName;
int i,sState;
DWORD bufCount,ProcessCount;
TCHAR *p=NULL;
HANDLE hServer = 0;
TListItem *pItem;
ListView1->Clear();
if(WTSEnumerateSessions(hServer,0,1,&pSession,&SessionCount))
{
pWTS_Info=pSession;
for( i = 0; i<SessionCount; i++)
{
sID = pSession->SessionId;
pItem = ListView1->Items->Add();
pItem->Caption = sID;
pItem->SubItems->Add(GetWTSString(sID,WTSUserName));
pItem->SubItems->Add(GetWTSString(sID,WTSClientName)) ;
WTS_CLIENT_ADDRESS *ip = NULL;
if(WTSQuerySessionInformation(hServer,sID,WTSClientAddress,(LPTSTR *)&ip,&bufCount))
{
char* cAddress = new char[bufCount];
memset( cAddress,0,bufCount);
sprintf(cAddress, "%d.%d.%d.%d",ip->Address[2],ip->Address[3],ip->Address[4],ip->Address[5]);
pItem->SubItems->Add(cAddress);
WTSFreeMemory(ip);
delete cAddress;
}
pSession++;
}
}
}
//断开选中的 客户端连接
//---------------------------------------------------------------------------
void __fastcall Tfm_LoginList::btnlogoffClick(TObject *Sender)
{
if(ListView1->Items->Count > 0)
{
int pos = ListView1->ItemIndex;
TListItem* pItem = ListView1->Items->Item[pos];
String seid = pItem->Caption;
if(WTSLogoffSession(0,seid.ToInt(),true))
{
pItem->Delete();
}
}
}
//---------------------------------------------------------------------------