64,685
社区成员
发帖
与我相关
我的任务
分享
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
//这个管道用于主程序向子进程传递命令
if (!CreatePipe(&hMainRead,&hMainWrite,&sa,0))
{
printf("CreatePipe Failed!\n");
return 0;
}
//这个管道用于子进程向主程序传递结果
if (!CreatePipe(&hCmdRead,&hCmdWrite,&sa,0))
{
printf("CreatePipe Failed!\n");
return 0;
}
ZeroMemory(&si,sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError = hCmdWrite;
si.hStdOutput = hCmdWrite;
si.hStdInput = hMainRead;
si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));
if (!CreateProcess(NULL,szCmd,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi))
{
printf("CreateProcess Failed!\n");
return 0;
}
// CloseHandle(hMainRead);
// CloseHandle(hCmdWrite);
while (TRUE)
{
memset(szReadBuff,0,MAX_SIZE);
ReadFile(hCmdRead,szReadBuff,MAX_SIZE,&dwReadBuff,NULL);
// timeout
if (strstr(szReadBuff,szTimeOut) != NULL)
{
printf(szCmd);
printf("can't connect!\n");
memset(szDeadIP,0,128);
memcpy(szDeadIP,szShhIP,128);
break;
}
// store key
if (strstr(szReadBuff,szStoreKey) != NULL)
{
WriteFile(hMainWrite,"n\n",3,&dwWriteBuff,NULL);
//第一次返回的是这个信息,writefile后可读取到子进程新的返回信息
continue;
}
// access denied
if (strstr(szReadBuff,szNeedPass) != NULL)
{
WriteFile(hMainWrite,"exit\n",6,&dwWriteBuff,NULL);
//第二次读取后返回的信息needpass,输入pass后,readfile就读取不到信息了
continue;
}
}
CloseHandle(hMainWrite);
CloseHandle(hCmdRead);
CloseHandle(hMainRead);
CloseHandle(hCmdWrite);
TerminateProcess(pi.hThread,0);