|
CreatePipe 를 이용해
rasdial 의 출력내용으로 접속성공 여부를 알아내는것입니다
어디선가 주운걸 수정한것입니다
bool connectVpn( UnicodeString vs, UnicodeString vid , UnicodeString vpw )
{
char strExec[256];
if( vs.Length() && vid.Length() && vpw.Length() )
{
formMain->addListLog( "vpn 접속 : %s , %s , %s" , OV(vs,vid,vpw));
sprintf( strExec , "rasdial %s %s %s" , vs.t_str() , vid.t_str() , vpw.t_str() );
bool flag;
HANDLE hwrite, hread;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = true;
// 어노니머스 파이프 생성
flag = CreatePipe(&hread, &hwrite, &sa, 0);
if (!flag)
{
formMain->addListLog("Fail to open pipe.");
return false ;
}
// 콘솔어플리케이션 프로세스 실행을 위한 준비
STARTUPINFO si;
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = hwrite; // 표준출력(stdout) 리다이렉션
si.hStdError = hwrite; // 표준에러(stderr) 리다이렉션
PROCESS_INFORMATION pi;
// 콘솔어플리케이션 프로세스 실행
flag = CreateProcess(NULL, strExec , NULL, NULL, true, DETACHED_PROCESS,
NULL, NULL, &si, &pi);
if(!flag)
{
formMain->addListLog("Fail to create process.");
return false;
}
CloseHandle(hwrite);//이것을 하지 않으면 프로세스가 block된다
char buffer[512];
DWORD BytesRead;
AnsiString ResultString;
while(ReadFile(hread, buffer, sizeof(buffer)-1, &BytesRead, NULL) && BytesRead)
{
buffer[BytesRead] = '\0';
ResultString = ResultString + buffer;
}
CloseHandle(hread);
if( ResultString.Pos( "명령을 완료했습니다." ) )
{
formMain->addListLog( "접속 성공" );
return true;
}
ShowMessage( ResultString );
formMain->addListLog( "연결할수 없음" );
return false;
}
return true;
}
|