Jump to content
  • Advertisement
Sign in to follow this  
AshishChauhan

Problem in ReadFile

This topic is 3749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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... // 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; }

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!