/* g++ AsusSendTo.cpp -o AsusSendTo.exe -mwindows */
/* usage: AsusSendTo EeePCName cmd.exe /K whoami */
#define UNICODE
#define _UNICODE
#include <string>
#include <windows.h>
#ifndef _T
#define _T(x) L##x
#endif
#define PIPE_TIMEOUT 10000
int main()
{
HANDLE pipe;
std::wstring wsname;
std::wstring exec;
std::wstring pipename;
LPTSTR cmd = GetCommandLine();
while (*cmd != ' ' && *cmd != '\0') ++cmd;
while (*cmd == ' ' && *cmd != '\0') ++cmd;
LPTSTR cmd2 = cmd;
while (*cmd2 != ' ' && *cmd2 != '\0') ++cmd2;
wsname = std::wstring(cmd, cmd2 - cmd);
while (*cmd2 == ' ' && *cmd2 != '\0') ++cmd2;
exec = std::wstring(cmd2);
pipename = _T("\\\\") + wsname + _T("\\pipe\\asuspipe");
while (1)
{
pipe = CreateFile(pipename.c_str(), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, NULL);
if (pipe != INVALID_HANDLE_VALUE)
break;
if (GetLastError() != ERROR_PIPE_BUSY)
{
MessageBox(NULL, _T("Failed to open asuspipe"), _T("ERROR"), MB_ICONERROR | MB_OK);
return 1;
}
if (!WaitNamedPipe(pipename.c_str(), PIPE_TIMEOUT))
{
MessageBox(NULL, _T("Timed out waiting on asuspipe"), _T("ERROR"), MB_ICONERROR | MB_OK);
return 1;
}
}
DWORD pipemode = PIPE_READMODE_MESSAGE;
if (!SetNamedPipeHandleState(pipe, &pipemode, NULL, NULL))
{
MessageBox(NULL, _T("Setting mode on asuspipe failed"), _T("ERROR"), MB_ICONERROR | MB_OK);
return 1;
}
DWORD written = 0;
if (!WriteFile(pipe, exec.c_str(), exec.size() * sizeof(wchar_t), &written, NULL))
{
MessageBox(NULL, _T("Write to asuspipe failed"), _T("ERROR"), MB_ICONERROR | MB_OK);
return 1;
}
CloseHandle(pipe);
return 0;
}