Sign in to follow this  
AshishChauhan

Problem in ReadFile

Recommended Posts

AshishChauhan    122
Hi everyone.. I'm new in VC++, I'm trying to make program which call "ping,exe" with specific argument "192.168.1.1", I'm creating pipe and succefully getting data in it..An di also manage to print my data in editBox with the help of ReadFile.. My problem is that data is coming in synchronous manner i.e it appears suddenly..I want my editBox behave like console.. Data should come in asynchronous manner. I know i've use ReadFileEx. But don't know how to implement.. Here is my code.. Any reply will be appreciated.. thanks.. Also take a look of while loop also..whenever i use to print data my program get hanged... [CODE] // Redirection_Ping.cpp : Defines the entry point for the application. #include "Stdafx.h" #include "windows.h" #include "resource.h" #include "stdio.h" #define BUFSIZE 4096 HWND WindowInterface=NULL; HINSTANCE WindowInterfaceInstance=NULL; BOOL ExitApplication = FALSE; int CALLBACK WindowInterfaceProc(HWND hwnd,UINT mes,WPARAM wParam,LPARAM lParam); BOOL MyFunction(HWND hwnd) { STARTUPINFO si = { 0 }; SECURITY_ATTRIBUTES saAttr = { 0 }; HANDLE hReadPipe = NULL, hWritePipe = NULL; PROCESS_INFORMATION pi = {0}; DWORD NumberOfBytesRead = 0; TCHAR Buffer[500] = ""; OVERLAPPED overlapped = { 0 }; SetDlgItemText(hwnd, IDC_EDIT1, TEXT("Please wait, working...")); si.cb = sizeof( si ); si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); saAttr.bInheritHandle = TRUE; saAttr.lpSecurityDescriptor = NULL; BOOL cp = CreatePipe( &hReadPipe, &hWritePipe, &saAttr, 0 ); if ( !cp) { MessageBox(NULL, "Failed to create pipe.", "Error", MB_OK); return(FALSE); } si.hStdOutput = hWritePipe; cp = CreateProcess( NULL, (LPTSTR)(LPCTSTR)"ping 192.168.0.96", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi ); if (!cp) { MessageBox(NULL, "Failed to create process.", "Error", MB_OK); return(FALSE); } WaitForSingleObject(pi.hProcess, INFINITE); cp = ReadFile( hReadPipe, Buffer, sizeof(Buffer), &NumberOfBytesRead, NULL); Buffer[NumberOfBytesRead] = TCHAR(0); SetDlgItemText(hwnd, IDC_EDIT1, Buffer); CloseHandle(hWritePipe); CloseHandle(hReadPipe); /* while(cp && Buffer[0] && NumberOfBytesRead && !ExitApplication) { Buffer[NumberOfBytesRead] = 0; SetDlgItemText(hwnd, IDC_EDIT1, Buffer); Sleep(5); Buffer[0] = 0; NumberOfBytesRead = 0; cp = ReadFile( hReadPipe, Buffer, 10, &NumberOfBytesRead, NULL); } CloseHandle(hWritePipe); CloseHandle(hReadPipe);*/ return(cp); } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WindowInterfaceInstance = hInstance; MSG msg; DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)WindowInterfaceProc); int bRet; while((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) { TranslateMessage(&msg); DispatchMessage(&msg); } return(FALSE); } int CALLBACK WindowInterfaceProc(HWND hwnd, UINT mes, WPARAM wParam, LPARAM lParam) { switch(mes) { case WM_COMMAND: { switch(LOWORD(wParam)) { case IDOK: { MyFunction(hwnd); break; } case IDCANCEL: { //MessageBox(NULL, "EXITING" , "Test", MB_OK); ExitApplication = TRUE; ExitProcess(0); return TRUE; break; } /*case IDC_EDIT1: { }*/ } } return TRUE; case WM_CLOSE: { ExitProcess(0); return TRUE; } case WM_DESTROY: { ExitProcess(0); return TRUE; } } return FALSE; } [/CODE]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this