I'm trying to get all the running processes on my PC using WTSEnumerateProcesses

255 views Asked by At

I'm trying to use the WTSEnumerateProcesses() function in a project to get all the processes. Here is my code

#include <iostream>
#include <Windows.h>
#include <WtsApi32.h>
#include <sddl.h>
 
#pragma comment(lib, "Wtsapi32.lib")

using namespace std;

int main() {
    cout << "C++ DLL Injector";
    WTS_PROCESS_INFO *processes = NULL;
    LPTSTR sid;
    DWORD count = 0;
 
    if (WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, NULL, 1, &processes, &count)) {
        for (DWORD i = 0; i < count; i++) {
            if (!ConvertSidToStringSid(processes[i].pUserSid, &sid)) {
                std::wcout <<
                    processes[i].pProcessName <<
                    " " <<
                    processes[i].ProcessId <<
                    " " <<
                    processes[i].SessionId <<
                    " " <<
                    std::endl;
            } else {
                std::wcout <<
                    processes[i].pProcessName <<
                    " " <<
                    processes[i].ProcessId <<
                    " " <<
                    processes[i].SessionId <<
                    " " <<
                    sid <<
                    " " <<
                    std::endl;
            }
        }
    } else {
        return GetLastError();
    }
 
    WTSFreeMemory(processes);
 
    return NO_ERROR;
}

But when i run, it shows me this error

C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g C:\Users\gabri\Downloads\Code\C++\Main.cpp -o C:\Users\gabri\Downloads\Code\C++\Main.exe
C:\Users\gabri\Downloads\Code\C++\Main.cpp: In function 'int main()':
C:\Users\gabri\Downloads\Code\C++\Main.cpp:16:58: warning: passing NULL to non-pointer argument 2 of 'WINBOOL WTSEnumerateProcessesA(HANDLE, DWORD, DWORD, _WTS_PROCESS_INFOA**, DWORD*)' [-Wconversion-null]
   16 |     if (WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, NULL, 1, &processes, &count)) {
      |                                                          ^~~~
In file included from C:\Users\gabri\Downloads\Code\C++\Main.cpp:3:
C:/msys64/mingw64/include/WtsApi32.h:168:62: note:   declared here
  168 |   WINBOOL WINAPI WTSEnumerateProcessesA(HANDLE hServer,DWORD Reserved,DWORD Version,PWTS_PROCESS_INFOA *ppProcessInfo,DWORD *pCount);
      |                                                        ~~~~~~^~~~~~~~
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\gabri\AppData\Local\Temp\ccZGWcxq.o: in function `main':
C:\Users\gabri\Downloads\Code\C++/Main.cpp:16: undefined reference to `WTSEnumerateProcessesA'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\gabri\Downloads\Code\C++/Main.cpp:44: undefined reference to `WTSFreeMemory'
collect2.exe: error: ld returned 1 exit status

I don't know why this error appears, I copied this code from a website.

Obs: Im using GCC and G++ to compile it (i dont know if this helps, im a begginer with C++)

0

There are 0 answers