Files
fastfetch/src/common/processing_windows.c
T

70 lines
2.1 KiB
C
Raw Normal View History

2022-10-08 16:04:55 +08:00
#include "fastfetch.h"
#include "common/processing.h"
#define WIN32_LEAN_AND_MEAN 1
#include <Windows.h>
//We can't use this native version yet because we still use POSIX path ( eg /usr/bin/fish ) at a lot of places.
const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
{
SECURITY_ATTRIBUTES saAttr = {
.nLength = sizeof(SECURITY_ATTRIBUTES),
.lpSecurityDescriptor = NULL,
.bInheritHandle = TRUE,
};
HANDLE hChildStdoutRead, hChildStdoutWrite;
if (!CreatePipe(&hChildStdoutRead, &hChildStdoutWrite, &saAttr, 0))
return "CreatePipe() failed";
if (!SetHandleInformation(hChildStdoutRead, HANDLE_FLAG_INHERIT, 0))
return "SetHandleInformation(hChildStdoutRead) failed";
PROCESS_INFORMATION piProcInfo = {0};
STARTUPINFOA siStartInfo = {
.cb = sizeof(siStartInfo),
.dwFlags = STARTF_USESTDHANDLES,
.hStdOutput = hChildStdoutWrite,
};
2022-11-13 00:04:34 +08:00
BOOL success;
2022-10-08 16:04:55 +08:00
2022-11-13 00:04:34 +08:00
{
FF_STRBUF_AUTO_DESTROY cmdline;
ffStrbufInitF(&cmdline, "\"%s\"", argv[0]);
for(char* const* parg = &argv[1]; *parg; ++parg)
{
ffStrbufAppendC(&cmdline, ' ');
ffStrbufAppendS(&cmdline, *parg);
}
2022-10-08 16:04:55 +08:00
2022-11-13 00:04:34 +08:00
success = CreateProcessA(
NULL, // application name
cmdline.chars, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo // receives PROCESS_INFORMATION
);
}
2022-10-13 20:06:54 +08:00
2022-10-08 16:04:55 +08:00
CloseHandle(hChildStdoutWrite);
if(!success)
{
CloseHandle(hChildStdoutRead);
return "CreateProcessA() failed";
}
2022-10-13 20:06:54 +08:00
char str[1024];
2022-10-08 16:04:55 +08:00
DWORD nRead;
while(ReadFile(hChildStdoutRead, str, sizeof(str), &nRead, NULL) && nRead > 0)
ffStrbufAppendNS(buffer, nRead, str);
CloseHandle(hChildStdoutRead);
return NULL;
}