Friday, November 10, 2006

WTSQuerySessionInformation

The WTSQuerySessionInformation function retrieves session information for the specified session on the specified terminal server.

With the function below you can find out the ip-Address of the client-computer if they logged in on a terminal server!
Under it, there is a function to get the client machine name!

Take a look to
http://www.microsoft.com/Businesssolutions.....
http://msdn.microsoft.com/library/...

#define.WTSClientIP(14)
#define.WTS_CURRENT_SESSION(-1)
#define.WTS_CURRENT_SERVER_HANDLE(0)
#define.structSize(256)
client server static str getRDPIPAddress()
{
DLL _winApiDLL = new DLL('WTSAPI32');
DLLFunction _getRDPName = new DLLFunction(_winApiDLL, 'WTSQuerySessionInformationA');
Binary dwPointer;
Binary structPointer;
Binary structAccess;
int spRet;
int dwret;
str rdpIP = "";
;
structpointer = new Binary(4); // will receive the pointer to result struc
dwPointer = new Binary(4); // wiil receive the length of reult struc


_getRDPName.returns(ExtTypes::DWORD);
_getRDPName.arg(ExtTypes::DWORD,
ExtTypes::DWORD,
ExtTypes::DWORD,
ExtTypes::POINTER,
ExtTypes::POINTER);


if(_getRDPName.call(#WTS_CURRENT_SERVER_HANDLE, #WTS_CURRENT_SESSION,
#WTSClientIP, structpointer, dwpointer) != 0)
{
spRet = structpointer.dWord(0);
dwRet = dwPointer.dWord(0);
structAccess = new Binary(0);
structAccess.attach(spRet,dwRet);

rdpIP = strfmt("%1", structAccess.byte(6));
rdpIP = rdpip + "." + strfmt("%1", structAccess.byte(7));
rdpIP = rdpip + "." + strfmt("%1", structAccess.byte(8));
rdpIP = rdpip + "." + strfmt("%1", structAccess.byte(9));
}
WinAPI::WTSFreeMemory(spRet);
return rdpIP;
}

client static void WTSFreeMemory(int _buffer)
{
DLL wtsapi32DLL = new DLL('wtsapi32.dll');
DLLFunction wtsfm = new DLLFunction(wtsapi32DLL, 'WTSFreeMemory');
;
wtsfm.arg(ExtTypes::DWord);
wtsfm.call(_buffer);
}

To get the client machine name:

#define.WTSClientName(10)
client server static str getRDPComputerName()
{
/*
Franz Aigner 20050922

*/
DLL _winApiDLL = new DLL('WTSAPI32');
DLLFunction _getRDPName = new DLLFunction(_winApiDLL, 'WTSQuerySessionInformationA');
Binary dwPointer;
Binary structPointer;
Binary structAccess;
int spRet;
int dwret;
str rdpName = "";
;
structpointer = new Binary(4); // will receive the pointer to result struc
dwPointer = new Binary(4); // wiil receive the length of reult struc


_getRDPName.returns(ExtTypes::DWORD);
_getRDPName.arg(ExtTypes::DWORD,
ExtTypes::DWORD,
ExtTypes::DWORD,
ExtTypes::POINTER,
ExtTypes::POINTER);

if(_getRDPName.call(#WTS_CURRENT_SERVER_HANDLE, #WTS_CURRENT_SESSION,
#WTSClientName, structpointer, dwpointer) != 0)
{
spRet = structpointer.dWord(0);
dwRet = dwPointer.dWord(0);
structAccess = new Binary(0);
structAccess.attach(spRet,dwRet);
rdpName = structAccess.string(0);
}
WinAPI::WTSFreeMemory(spRet);
return rdpName;
}

1 comment:

Anonymous said...

Thank you - you are superb.