Skip to content

Instantly share code, notes, and snippets.

@chrisnas
Created September 17, 2022 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisnas/98de6677ffb582e374699667a5fc5f95 to your computer and use it in GitHub Desktop.
Save chrisnas/98de6677ffb582e374699667a5fc5f95 to your computer and use it in GitHub Desktop.
int BasicConnection(DWORD pid)
{
wchar_t pszPipeName[256];
// build the pipe name as described in the protocol
int nCharactersWritten = -1;
nCharactersWritten = wsprintf(
pszPipeName,
L"\\\\.\\pipe\\dotnet-diagnostic-%d",
pid
);
// check that CLR has created the diagnostics named pipe
if (!::WaitNamedPipe(pszPipeName, 200))
{
auto error = ::GetLastError();
std::cout << "Diagnostics named pipe is not available for process #" << pid << " (" << error << ")" << "\n";
return -1;
}
// connect to the named pipe
HANDLE hPipe;
hPipe = ::CreateFile(
pszPipeName, // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
if (hPipe == INVALID_HANDLE_VALUE)
{
std::cout << "Impossible to connect to " << pszPipeName << "\n";
return -2;
}
// ... send a command...
// don't forget to close the named pipe
::CloseHandle(hPipe);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment