Skip to content

Commit

Permalink
fix: Fix installer crashes (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
shdwmtr committed Dec 21, 2024
1 parent 0bc733d commit bfafcb7
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions src/pipes/terminal_pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,46 @@
extern "C" {
const int CreateTerminalPipe()
{
HANDLE hPipe = CreateFileA(R"(\\.\pipe\MillenniumStdoutPipe)", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
try {
HANDLE hPipe = CreateFileA(R"(\\.\pipe\MillenniumStdoutPipe)", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if (hPipe == INVALID_HANDLE_VALUE)
{
std::cerr << "Failed to connect to pipe. Error: " << GetLastError() << std::endl;
return 1;
}
if (hPipe == INVALID_HANDLE_VALUE)
{
std::cerr << "Failed to connect to pipe. Error: " << GetLastError() << std::endl;
return 1;
}

int pipeDescriptor = _open_osfhandle((intptr_t)hPipe, _O_WRONLY);
if (pipeDescriptor == -1)
{
std::cerr << "Failed to get pipe file descriptor. Error: " << errno << std::endl;
CloseHandle(hPipe);
return 1;
}
int pipeDescriptor = _open_osfhandle((intptr_t)hPipe, _O_WRONLY);
if (pipeDescriptor == -1)
{
std::cerr << "Failed to get pipe file descriptor. Error: " << errno << std::endl;
CloseHandle(hPipe);
return 1;
}

FILE* pipeFile = _fdopen(pipeDescriptor, "w");
if (!pipeFile)
{
std::cerr << "Failed to open pipe file descriptor as FILE*. Error: " << errno << std::endl;
CloseHandle(hPipe);
return 1;
}
FILE* pipeFile = _fdopen(pipeDescriptor, "w");
if (!pipeFile)
{
std::cerr << "Failed to open pipe file descriptor as FILE*. Error: " << errno << std::endl;
CloseHandle(hPipe);
return 1;
}

if (_dup2(_fileno(pipeFile), _fileno(stdout)) == -1)
{
std::cerr << "Failed to redirect stdout to pipe. Error: " << errno << std::endl;
fclose(pipeFile);
CloseHandle(hPipe);
return 1;
}

if (_dup2(_fileno(pipeFile), _fileno(stdout)) == -1)
{
std::cerr << "Failed to redirect stdout to pipe. Error: " << errno << std::endl;
fclose(pipeFile);
CloseHandle(hPipe);
setvbuf(stdout, NULL, _IONBF, 0);
return 0;
}
catch (std::system_error& ex) {
std::cerr << "Failed to create terminal pipe: " << ex.what() << std::endl;
return 1;
}

setvbuf(stdout, NULL, _IONBF, 0);
return 0;
}
}
#endif

0 comments on commit bfafcb7

Please sign in to comment.