PowerToys/tools/HandlesExperiment/FileLocksmithLib/IPC.cpp

125 lines
1.9 KiB
C++
Raw Normal View History

2022-09-13 19:45:07 +08:00
#include "pch.h"
#include "IPC.h"
#define FILE_LOCKSMITH_IPC_PIPE_NAME L"\\\\.\\pipe\\FileLocksmith-cdc0e7ad-e8c6-47fd-a63d-c1c90d73b2e3"
constexpr DWORD DefaultPipeBufferSize = 8192;
constexpr DWORD DefaultPipeTimeoutMillis = 200;
namespace ipc
{
Writer::Writer()
{
2022-09-14 00:20:07 +08:00
start();
}
Writer::~Writer()
{
finish();
2022-09-13 19:45:07 +08:00
}
HRESULT Writer::start()
{
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&m_read_pipe, &m_write_pipe, &sa, 0))
{
return HRESULT_FROM_WIN32(GetLastError());
}
2022-09-13 21:03:07 +08:00
return S_OK;
2022-09-13 19:45:07 +08:00
}
HRESULT Writer::add_path(LPCWSTR path)
{
int length = lstrlenW(path);
DWORD written;
if (!WriteFile(m_write_pipe, path, length * sizeof(WCHAR), &written, NULL))
{
return HRESULT_FROM_WIN32(GetLastError());
}
if (written != length * sizeof(WCHAR))
{
return E_FAIL;
}
WCHAR line_break = L'\n';
if (!WriteFile(m_write_pipe, &line_break, sizeof(WCHAR), &written, NULL))
{
return HRESULT_FROM_WIN32(GetLastError());
}
if (written != sizeof(WCHAR))
{
return E_FAIL;
}
return S_OK;
}
void Writer::finish()
{
2022-09-14 00:20:07 +08:00
add_path(L"");
2022-09-13 19:45:07 +08:00
if (m_write_pipe)
{
CloseHandle(m_write_pipe);
m_write_pipe = NULL;
}
2022-09-14 00:20:07 +08:00
if (m_read_pipe)
{
CloseHandle(m_read_pipe);
m_read_pipe = NULL;
}
2022-09-13 19:45:07 +08:00
}
HANDLE Writer::get_read_handle()
{
2022-09-14 00:20:07 +08:00
HANDLE result = m_read_pipe;
m_read_pipe = NULL;
return result;
2022-09-13 19:45:07 +08:00
}
std::vector<std::wstring> read_paths_from_stdin()
{
std::vector<std::wstring> result;
std::wstring line;
2022-09-14 16:45:14 +08:00
bool finished = false;
while (!finished)
2022-09-13 19:45:07 +08:00
{
2022-09-14 16:45:14 +08:00
WCHAR ch;
// We have to read data like this
if (!std::cin.read(reinterpret_cast<char*>(&ch), 2))
2022-09-14 00:20:07 +08:00
{
2022-09-14 16:45:14 +08:00
finished = true;
}
else if (ch == L'\n')
{
if (line.empty())
{
finished = true;
}
else
{
result.push_back(line);
line = {};
}
}
else
{
line += ch;
2022-09-14 00:20:07 +08:00
}
2022-09-13 19:45:07 +08:00
}
return result;
}
}