VC6中获取远程桌面连接的客户端ip地址
在VC6中如果要编译一下代码需要将附件中的文件拷贝到项目目录下。
#include "sal.h"
#include "wtsapi32.h"
#pragma comment(lib, "wtsapi32.lib")
#pragma comment(lib, "kernel32.lib")
#ifdef __cplusplus
extern "C" {
#endif
BOOL WINAPI ProcessIdToSessionId(__in DWORD dwProcessId,__out DWORD *pSessionId);
#ifdef __cplusplus
}
#endif
#define SM_REMOTESESSION 0x1000
bool GetClientIP(char *clientip)
{
bool fActiveSession = false;
DWORD dwSessionID = -1; // 0 is 1st console session created on XP, 1 is 1st console session on Vista
LPTSTR pData = NULL;
DWORD cbReturned = 0;
WTS_CLIENT_ADDRESS clientaddress;
if(GetSystemMetrics(SM_REMOTESESSION) == 0) return false;
ProcessIdToSessionId(GetCurrentProcessId(), &dwSessionID);
if( WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessionID, WTSClientAddress, &pData, &cbReturned) && (cbReturned == sizeof(WTS_CLIENT_ADDRESS)) )
{ // if we get WTSActive we're in the active session, otherwise we assume we're not in the active session (WTSDisconnected)
//fActiveSession = (*((INT *)pData) == WTSActive) ? true : false;
memcpy(&clientaddress,pData,sizeof(WTS_CLIENT_ADDRESS));
memset(clientip,0,sizeof(clientip));
sprintf(clientip,"%d.%d.%d.%d",
clientaddress.Address[2],
clientaddress.Address[3],
clientaddress.Address[4],
clientaddress.Address[5]);
WTSFreeMemory(pData);
return true;
}
WTSFreeMemory(pData);
return false;
}