Skip to content

Commit 6c80490

Browse files
authored
Merge pull request #27 from isanae/get-process-list-2
added GetVFSProcessList2() to return all the pids
2 parents 5b93620 + fc44282 commit 6c80490

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

include/usvfs.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ DLLEXPORT void WINAPI GetCurrentVFSName(char *buffer, size_t size);
106106
*/
107107
DLLEXPORT BOOL WINAPI GetVFSProcessList(size_t *count, LPDWORD processIDs);
108108

109+
// retrieve a list of all processes connected to the vfs, stores an array
110+
// of `count` elements in `*buffer`
111+
//
112+
// if this returns TRUE and `count` is not 0, the caller must release the buffer
113+
// with `free(*buffer)`
114+
//
115+
// return values:
116+
// - ERROR_INVALID_PARAMETERS: either `count` or `buffer` is NULL
117+
// - ERROR_TOO_MANY_OPEN_FILES: there seems to be way too many usvfs processes
118+
// running, probably some internal error
119+
// - ERROR_NOT_ENOUGH_MEMORY: malloc() failed
120+
//
121+
DLLEXPORT BOOL WINAPI GetVFSProcessList2(size_t* count, DWORD** buffer);
122+
109123
/**
110124
* spawn a new process that can see the virtual file system. The signature is identical to CreateProcess
111125
*/

src/usvfs_dll/usvfs.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,35 @@ BOOL WINAPI GetVFSProcessList(size_t *count, LPDWORD processIDs)
532532
return TRUE;
533533
}
534534

535+
BOOL WINAPI GetVFSProcessList2(size_t* count, DWORD** buffer)
536+
{
537+
if (!count || !buffer) {
538+
SetLastError(ERROR_INVALID_PARAMETER);
539+
return FALSE;
540+
}
541+
542+
*count = 0;
543+
*buffer = nullptr;
544+
545+
std::vector<DWORD> pids = context->registeredProcesses();
546+
auto last = std::remove_if(pids.begin(), pids.end(), [](DWORD id) {
547+
return !processStillActive(id);
548+
});
549+
550+
pids.erase(last, pids.end());
551+
552+
if (pids.empty()) {
553+
return TRUE;
554+
}
555+
556+
*count = pids.size();
557+
*buffer = static_cast<DWORD*>(std::calloc(pids.size(), sizeof(DWORD)));
558+
559+
std::copy(pids.begin(), pids.end(), *buffer);
560+
561+
return TRUE;
562+
}
563+
535564
void WINAPI ClearVirtualMappings()
536565
{
537566
context->redirectionTable()->clear();

0 commit comments

Comments
 (0)