Click here to Skip to main content
15,891,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a c++ code to display all the process with the session ID . but this code is not working perfectly in some Windows xp machines , because of ProcessIdToSessionId() methods minimum support is Windows Vista. Anyone have suggestion about how to do it for Windows XP. Here is my code

What I have tried:

C++
#include <cstdio>
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
    PROCESSENTRY32 entry;
    entry.dwSize = sizeof(PROCESSENTRY32);
    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    std::wstring wsProcessName;
    DWORD dwSessionId,dwPid,dwErr;
    if (Process32First(snapshot, &entry) == TRUE)
    {
        while (Process32Next(snapshot, &entry) == TRUE)
        {
            dwPid = entry.th32ProcessID;
            wsProcessName = entry.szExeFile;
            if(!ProcessIdToSessionId(dwPid,&dwSessionId))
            {
                dwErr = GetLastError();
                std::wcout<<"Error " << wsProcessName.c_str()<<"\t"<<entry.th32ProcessID<<"\t"<<entry.th32ModuleID<<"\t"<<dwErr<<"\n";
            }
            else
                std::wcout<<wsProcessName.c_str()<<"\t"<<entry.th32ProcessID<<"\t"<<dwSessionId<<"\n";
            dwSessionId = NULL;
            dwErr = NULL;
        }
    }
    CloseHandle(snapshot);
    return 0;
}
Posted
Updated 30-Dec-16 0:22am
Comments
Richard MacCutchan 30-Dec-16 4:01am    
If a feature is not supported in Windows XP, then it is not supported in Windows XP. There is no solution.
MarshalS 30-Dec-16 4:28am    
if that API is not supported in windows XP , I asked about there is any other ways to find it.

1 solution

Quote:
because of ProcessIdToSessionId() methods minimum support is Windows Vista
That is from the actual MSDN documentation. Because Windows XP is not supported anymore, it is removed from the documentation.

If you want to support XP (or even older Windows versions) you should use an older SDK documentation. From a 2003 platform SDK:
Quote:
ProcessIdToSessionId
...
Client: Included in Windows XP and Windows 2000 Professional.
Server: Included in Windows Server 2003 and Windows 2000 Server.
Redistributable: Requires Terminal Server 4.0 SP4 on Windows NT 4.0 SP4.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib

When using a not supported function, a program will stop execution immediately with an appropriate error message.

There must be another reason if the function is not working as expected with XP. But again an old SDK might have a clue:
Quote:
Remarks
If the calling process is not running in a Terminal Services environment, the value returned in pSessionId is zero
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900