Anonymous
Как вывести неанглийский символ с помощью анонимных каналов?
Сообщение
Anonymous » 03 дек 2024, 05:08
Я написал этот код, который отправляет команду (строку, здесь меня не волнует unincode) через канал в cmd, а с помощью другого канала я получаю вывод:
Код: Выделить всё
string command = " cd C:\\Users\\user\\Desktop\\ && dir \n";
std::wstring convert(const char* str, int size) {
int wideCharLength = MultiByteToWideChar(CP_ACP, 0, str, size, nullptr, 0);
std::wstring wstr(wideCharLength, L'\0');
MultiByteToWideChar(CP_ACP, 0, str, size, &wstr[0], wideCharLength);
return wstr;
}
void cmd() {
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
HANDLE hCommandReadPipe, hCommandWritePipe;
HANDLE hOutputReadPipe, hOutputWritePipe;
CreatePipe(&hCommandReadPipe, &hCommandWritePipe, &sa, 0);
CreatePipe(&hOutputReadPipe, &hOutputWritePipe, &sa, 0);
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.hStdInput = hCommandReadPipe;
si.hStdError = hOutputWritePipe;
si.hStdOutput = hOutputWritePipe;
si.dwFlags |= STARTF_USESTDHANDLES;
CHAR cmdPath[MAX_PATH];
ExpandEnvironmentStringsA("%SystemRoot%\\System32\\cmd.exe", cmdPath, MAX_PATH);
CreateProcessA(cmdPath, NULL, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
CloseHandle(hCommandReadPipe);
CloseHandle(hOutputWritePipe);
while (true) {
DWORD bytesWritten;
WriteFile(hCommandWritePipe, command.c_str(), command.length(), &bytesWritten, NULL);
command.erase();
string temp_buff(4096, '\0');
DWORD bytesRead = 0;
BOOL readResult = ReadFile(hOutputReadPipe, &temp_buff[0], temp_buff.size(), &bytesRead, NULL);
if (readResult && bytesRead > 0) {
temp_buff.resize(bytesRead);
wstring wbuff = convert((char*)temp_buff.c_str(), bytesRead);
wcout
Подробнее здесь: [url]https://stackoverflow.com/questions/79246060/how-to-output-non-english-character-with-anonymous-pipes[/url]
1733191698
Anonymous
Я написал этот код, который отправляет команду (строку, здесь меня не волнует unincode) через канал в cmd, а с помощью другого канала я получаю вывод: [code]string command = " cd C:\\Users\\user\\Desktop\\ && dir \n"; std::wstring convert(const char* str, int size) { int wideCharLength = MultiByteToWideChar(CP_ACP, 0, str, size, nullptr, 0); std::wstring wstr(wideCharLength, L'\0'); MultiByteToWideChar(CP_ACP, 0, str, size, &wstr[0], wideCharLength); return wstr; } void cmd() { SECURITY_ATTRIBUTES sa; sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE; HANDLE hCommandReadPipe, hCommandWritePipe; HANDLE hOutputReadPipe, hOutputWritePipe; CreatePipe(&hCommandReadPipe, &hCommandWritePipe, &sa, 0); CreatePipe(&hOutputReadPipe, &hOutputWritePipe, &sa, 0); STARTUPINFOA si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); si.hStdInput = hCommandReadPipe; si.hStdError = hOutputWritePipe; si.hStdOutput = hOutputWritePipe; si.dwFlags |= STARTF_USESTDHANDLES; CHAR cmdPath[MAX_PATH]; ExpandEnvironmentStringsA("%SystemRoot%\\System32\\cmd.exe", cmdPath, MAX_PATH); CreateProcessA(cmdPath, NULL, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi); CloseHandle(hCommandReadPipe); CloseHandle(hOutputWritePipe); while (true) { DWORD bytesWritten; WriteFile(hCommandWritePipe, command.c_str(), command.length(), &bytesWritten, NULL); command.erase(); string temp_buff(4096, '\0'); DWORD bytesRead = 0; BOOL readResult = ReadFile(hOutputReadPipe, &temp_buff[0], temp_buff.size(), &bytesRead, NULL); if (readResult && bytesRead > 0) { temp_buff.resize(bytesRead); wstring wbuff = convert((char*)temp_buff.c_str(), bytesRead); wcout Подробнее здесь: [url]https://stackoverflow.com/questions/79246060/how-to-output-non-english-character-with-anonymous-pipes[/url]