이것은 내가 C 규칙을 사용하기 때문에 당신이 C++을 사용한다면 당신이 찾고있는 것을 할 것입니다. 당신은 그것을 사용하기 전에 이것을 청소해야합니다. 나는 방금 그것을 꺼 냈습니다. 또한 파이프 손잡이를 닫아야합니다. 그렇지 않으면 누출 될 것입니다.
// RunCmd.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <strsafe.h>
int RunCmd(_TCHAR * Command,_TCHAR **OutPut);
bool HasTerminated(PROCESS_INFORMATION PI,DWORD *ExitCode);
bool HasData(HANDLE H);
void ErrorExit(LPTSTR lpszFunction);
int _tmain(int argc, _TCHAR* argv[])
{
_TCHAR * Buffer;
_TCHAR CmdLine[] = _TEXT("Outputter.exe");
RunCmd(CmdLine,&Buffer);
wprintf(_TEXT("Buffer from other pgm \n%s"),Buffer);
free(Buffer);
}
int RunCmd(_TCHAR * Command,_TCHAR ** OutPut)
{
_TCHAR * CpBUff = NULL;
STARTUPINFO SI;
memset(&SI,0,sizeof(SI));
*OutPut = NULL;
SECURITY_ATTRIBUTES SA;
SA.nLength = sizeof(SECURITY_ATTRIBUTES);
SA.lpSecurityDescriptor = NULL;
SA.bInheritHandle = true;
HANDLE ReadOutPut, WriteOutPut;
if(!CreatePipe(&ReadOutPut,&WriteOutPut,&SA,0))
{
wprintf(_TEXT("Error"));
}
SI.hStdOutput = WriteOutPut;
SI.cb = sizeof(STARTUPINFO);
SI.dwFlags = STARTF_USESTDHANDLES;
PROCESS_INFORMATION PI;
if (!CreateProcess(NULL,Command,&SA,NULL,true,CREATE_NO_WINDOW,NULL,NULL,&SI,&PI))
{
ErrorExit(TEXT("CreateProcess"));
}
Sleep(500);
DWORD ExitCode;
char Buffer[512];
_TCHAR ConvBuff[512];
int Total =0;
bool Zero;
while (!HasTerminated(PI,&ExitCode) & HasData(ReadOutPut))
{
ZeroMemory(Buffer,512*sizeof(char));
ZeroMemory(ConvBuff,512 * sizeof(_TCHAR));
DWORD NumBytesRead;
ReadFile(ReadOutPut,Buffer,512,&NumBytesRead,NULL);
Zero = Total == 0;
Total += NumBytesRead +1;
*OutPut = ((_TCHAR *) realloc(*OutPut,Total*sizeof(_TCHAR)));
if(Zero)
{
ZeroMemory(*OutPut,Total * sizeof(_TCHAR));
}
size_t ConChar;
mbstowcs_s(&ConChar,ConvBuff,strlen(Buffer)+1,Buffer,511);
StringCchCat(*OutPut,Total,ConvBuff);
}
CloseHandle(PI.hProcess);
CloseHandle(PI.hThread);
return ExitCode;
}
bool HasTerminated(PROCESS_INFORMATION PI,DWORD *ExitCode)
{
return (STILL_ACTIVE == GetExitCodeProcess(PI.hProcess,ExitCode));
}
bool HasData(HANDLE H)
{
char Buffer[25];
DWORD ReadBytes,TotalBytes,TotalLeft;
PeekNamedPipe(H,Buffer,25,&ReadBytes,&TotalBytes,&TotalLeft);
return ReadBytes > 0;
}
void ErrorExit(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL);
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf)/sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}
이미 실행중인 응용 프로그램에 대한 소스를 제어합니까? 또는이 타사 응용 프로그램을 스크래핑하고 싶습니까? – user7116
응용 프로그램을 .NET 어셈블리로 래핑 할 수 있습니까? 의미는 당신이 작성한 코드 내에서 프로세스로 시작하거나, 이런 식으로 끝나면 실패하는 서비스입니다. –
@sixlettervariables : 자체 응용 프로그램이지만 코드를 소유하고 있지 않으므로 변경하기가 어려울 수 있습니다. @ Agent_9191 : 귀하의 의견을 해결하기 위해 질문을 업데이트했습니다. – Justin