0

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++)

Mikel F
  • 3,567
  • 1
  • 21
  • 33
spookyss
  • 27
  • 6
  • It is a DWORD (aka unsigned int), just pass 0. – Hans Passant Mar 07 '23 at 01:05
  • The DWORD/NULL issue is a warning that should have no effect. The errors appear to be path problems during the link stage. – Mikel F Mar 07 '23 at 01:10
  • And for what it is worth, that is not a run-time error. It fails when you attempt to build the executable, not when you run it. – Mikel F Mar 07 '23 at 01:12
  • 1
    You need to link Wtsapi32.lib. At least that's what Microsoft names the correct library. The mingw32 distro I have installed at the moment doesn't seem to include the correct library, so you'll probably also need to use its tools to create the correct library first (but I haven't done that with a recent iteration of mingw32, and the way I seem to recall it working at one time doesn't seem to work now, so either it's changed or my memory's wrong--more likely the latter). – Jerry Coffin Mar 07 '23 at 01:54
  • @HansPassant I tried this also, but still not working. – spookyss Mar 07 '23 at 15:33
  • @JerryCoffin I took a look at the files, and that's right. The problem is that I don't know where I download the .lib file. – spookyss Mar 07 '23 at 15:43
  • @spookyss: Looking at things, it appears that current iterations of mingw mostly have you link directly against the dll itself, so for a typical Windows installation, you'd do something like: `g++.exe -fdiagnostics-color=always -g C:\Users\gabri\Downloads\Code\C++\Main.cpp -o C:\Users\gabri\Downloads\Code\C++\Main.exe c:\Windows\System32\wtsapi32.dll` – Jerry Coffin Mar 07 '23 at 15:52
  • I did this, but it shows this```warning: passing NULL to non-pointer argument 2 of 'WINBOOL WTSEnumerateProcessesA(HANDLE, DWORD, DWORD, _WTS_PROCESS_INFOA**, DWORD*)' [-Wconversion-null]``` – spookyss Mar 07 '23 at 17:28
  • 1
    @spookyss: You should normally only pass NULL where a pointer is expected. In this case, it's expecting a DWORD, and the documentation says to pass 0, so do that... – Jerry Coffin Mar 08 '23 at 05:25

0 Answers0