#include<iostream>
#include<windows.h>
#include<thread>
using namespace std;
void msg(int a);
HANDLE hread;
HANDLE hwrite;
HANDLE hread1;
HANDLE hwrite1;
void msg(int a)
{
cout << "func called";
char buffer[2] = { 0 };
DWORD bytesRead;
DWORD bytesWrite;
while (true)
{
if (ReadFile(hread, buffer, 1, &bytesRead, NULL) == NULL)
break;
cout << buffer;
}
}
void input(int a)
{
}
int main()
{
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (CreatePipe(&hread, &hwrite, &sa, 0))
{
cout << "创建成功" << endl;
}
SECURITY_ATTRIBUTES sb;
sb.nLength = sizeof(SECURITY_ATTRIBUTES);
sb.lpSecurityDescriptor = NULL;
sb.bInheritHandle = TRUE;
if (CreatePipe(&hread1, &hwrite1, &sb, 0))
{
cout << "创建成功" << endl;
}
cout << "服务器正在启动" << endl;
STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdOutput = hwrite;
si.hStdError = hwrite;
si.hStdInput = hread1;
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
string temp = "cmd.exe";
LPSTR command = const_cast<char*>(temp.c_str());
if (!CreateProcess(NULL, command
, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
{
cout << "进程创建错误" << endl;
}
thread mdgloop(msg, 1);
mdgloop.detach();
char buffer[] = { "help" };
DWORD bytesRead;
DWORD bytesWrite = 0;
while (true)
{
WriteFile(hwrite1, buffer, strlen(buffer), &bytesWrite, NULL);//如果添加延迟或单次执行则无法写入
}
}
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。