#include
#include
#include
using namespace std;
int main(){
DWORD processId = GetCurrentProcessId();// id
DWORD sessionId;
if(ProcessIdToSessionId(processId,&sessionId)){
_tprintf(TEXT("Process '%u' runs in Terminal Services session '%u'"),processId,sessionId);// tchar.h
}
else{
_tprintf(TEXT("Unable to get Terminal Services session ID for process '%u'"),processId);
}
}
d:\programming\C++>cl getSessionId.cpp
x86 Microsoft (R) C/C++ 18.00.21005.1 (C) Microsoft
Corporation。 。
getSessionId.cpp
c:\program files (x86)\microsoft sdks\windows\v7.1a\include\sal_supp.h(57) : war
ning C4005: “__useHeader”:
F:\360Apps\vs2013\VC\include\sal.h(2886) : “__useHeader”
c:\program files (x86)\microsoft sdks\windows\v7.1a\include\specstrings_supp.h(7
7) : warning C4005: “__on_failure”:
F:\360Apps\vs2013\VC\include\sal.h(2896) : “__on_failure”
F:\360Apps\vs2013\VC\include\xlocale(337) : warning C4530: C++
, 。 /EHsc
Microsoft (R) Incremental Linker Version 12.00.21005.1
Copyright (C) Microsoft Corporation. All rights reserved.
/out:getSessionId.exe
getSessionId.obj
d:\programming\C++>getSessionId
Process '5056' runs in Terminal Services session '1'
d:\programming\C++>getSessionId
Process '6572' runs in Terminal Services session '1'
d:\programming\C++>getSessionId
Process '8324' runs in Terminal Services session '1'
d:\programming\C++>getSessionId
Process '8516' runs in Terminal Services session '1'
d:\programming\C++>getSessionId
Process '7748' runs in Terminal Services session '1'