Skip to content

Commit

Permalink
Merge pull request #27 from isanae/get-process-list-2
Browse files Browse the repository at this point in the history
added GetVFSProcessList2() to return all the pids
  • Loading branch information
isanae authored Nov 11, 2019
2 parents 5b93620 + fc44282 commit 6c80490
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
14 changes: 14 additions & 0 deletions include/usvfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ DLLEXPORT void WINAPI GetCurrentVFSName(char *buffer, size_t size);
*/
DLLEXPORT BOOL WINAPI GetVFSProcessList(size_t *count, LPDWORD processIDs);

// retrieve a list of all processes connected to the vfs, stores an array
// of `count` elements in `*buffer`
//
// if this returns TRUE and `count` is not 0, the caller must release the buffer
// with `free(*buffer)`
//
// return values:
// - ERROR_INVALID_PARAMETERS: either `count` or `buffer` is NULL
// - ERROR_TOO_MANY_OPEN_FILES: there seems to be way too many usvfs processes
// running, probably some internal error
// - ERROR_NOT_ENOUGH_MEMORY: malloc() failed
//
DLLEXPORT BOOL WINAPI GetVFSProcessList2(size_t* count, DWORD** buffer);

/**
* spawn a new process that can see the virtual file system. The signature is identical to CreateProcess
*/
Expand Down
29 changes: 29 additions & 0 deletions src/usvfs_dll/usvfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,35 @@ BOOL WINAPI GetVFSProcessList(size_t *count, LPDWORD processIDs)
return TRUE;
}

BOOL WINAPI GetVFSProcessList2(size_t* count, DWORD** buffer)
{
if (!count || !buffer) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}

*count = 0;
*buffer = nullptr;

std::vector<DWORD> pids = context->registeredProcesses();
auto last = std::remove_if(pids.begin(), pids.end(), [](DWORD id) {
return !processStillActive(id);
});

pids.erase(last, pids.end());

if (pids.empty()) {
return TRUE;
}

*count = pids.size();
*buffer = static_cast<DWORD*>(std::calloc(pids.size(), sizeof(DWORD)));

std::copy(pids.begin(), pids.end(), *buffer);

return TRUE;
}

void WINAPI ClearVirtualMappings()
{
context->redirectionTable()->clear();
Expand Down

0 comments on commit 6c80490

Please sign in to comment.