Skip to content

Instantly share code, notes, and snippets.

@shuax
Created October 26, 2020 07:22
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 shuax/9e28800717799db4c68380909cb4ba6b to your computer and use it in GitHub Desktop.
Save shuax/9e28800717799db4c68380909cb4ba6b to your computer and use it in GitHub Desktop.
#define UNICODE
#include <windows.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <thread>
#pragma comment(lib, "shell32.lib")
std::wstring Win32ErrorToString(int ErrorCode)
{
static wchar_t Message[1024];
// If this program was multithreaded, we'd want to use
// FORMAT_MESSAGE_ALLOCATE_BUFFER instead of a static buffer here.
// (And of course, free the buffer when we were done with it)
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL, ErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
Message, 1024, NULL);
return Message;
}
std::wstring Win32LastErrorToString()
{
return Win32ErrorToString(GetLastError());
}
std::wstring QuoteSpaceIfNeeded(const std::wstring &str)
{
if (str.find(L' ') == std::wstring::npos)
return std::move(str);
std::wstring escaped(L"\"");
for (auto c : str)
{
if (c == L'"')
escaped += L'"';
escaped += c;
}
escaped += L'"';
return std::move(escaped);
}
std::wstring JoinArgsString(std::vector<std::wstring> lines, const std::wstring &delimiter)
{
std::wstring text;
bool first = true;
for (auto &line : lines)
{
if (!first)
text += delimiter;
else
first = false;
text += QuoteSpaceIfNeeded(line);
}
return text;
}
void Read(HANDLE handle)
{
// char buffer[1024];
// while(1)
// {
// DWORD bread = 0;
// ReadFile(handle, buffer, 1024, &bread, NULL);
// printf("Read %d\n[%s]\n", bread, buffer);
// // Sleep(1);
// }
}
void Write(HANDLE handle)
{
char buffer[1024];
while(1)
{
DWORD bread = 0;
ReadFile(handle, buffer, 1024, &bread, NULL);
printf("Write %d\n[%s]\n", bread, buffer);
Sleep(1);
}
}
int main()
{
PVOID OldValue = nullptr;
Wow64DisableWow64FsRedirection(&OldValue);
std::vector <std::wstring> args;
args.push_back(L"C:\\Windows\\System32\\OpenSSH\\ssh.exe");
int argc;
LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
for (int i = 1; i < argc; i++)
{
args.push_back(argv[i]);
}
LocalFree(argv);
auto command = JoinArgsString(args, L" ");
auto command_c = wcsdup(command.c_str());
HANDLE newstdin, write_stdin;
HANDLE read_stdout, newstdout;
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
if (!CreatePipe(&newstdin, &write_stdin, &sa, 0)){
fprintf(stderr, "Create Pipe Failed\n");
return 0;
}
if (!CreatePipe(&read_stdout, &newstdout, &sa, 0)){
CloseHandle(newstdin);
CloseHandle(write_stdin);
fprintf(stderr, "Create Pipe Failed\n");
return 0;
}
// SetHandleInformation(write_stdin, HANDLE_FLAG_INHERIT, 0);
std::thread t1(Read, write_stdin);
std::thread t2(Write, read_stdout);
t1.detach();
t2.detach();
STARTUPINFO si{};
PROCESS_INFORMATION pi{};
// GetStartupInfo(&si);
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = newstdout;
si.hStdError = newstdout;
// si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
// si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
// si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
if(CreateProcess(NULL, command_c,
NULL, NULL, TRUE, 0, NULL, NULL,
&si, &pi))
{
WaitForSingleObject(pi.hProcess, INFINITE);
// Sleep(1000);
}
else
{
printf("%ws", Win32LastErrorToString().c_str());
}
free(command_c);
}
@shuax
Copy link
Author

shuax commented Oct 26, 2020

ssh似乎不能重定向输入输出流,尝试失败

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment