|
-
February 16th, 2008, 09:06 AM
#1
While Writing Exe get hang...
Hi all...
Making a prog which call ping with specific argument... I'm creating pipe to read and write the data... I'm successfully reading the content content but when i try to write the data in my EDITBOX it get hang...
What could be the prob???
Also do i need to take some othere control to print my putput???
When i use "MessageBox(hwnd,Buffer,"a",MB_OK);"
It gives output without any trouble... But i don't need message box..
I want to redirect in EDITBOX or some other control..
Here is my code..
Any reply will be appreciated
thanks..
Code:
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] = "";
si.cb = sizeof( si );
si.dwFlags = 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.dwFlags |= STARTF_USESTDHANDLES;
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);
}
while(::ReadFile( hReadPipe, Buffer, sizeof(Buffer)/sizeof(Buffer[ 0 ])-1,&NumberOfBytesRead, NULL ) )
{
if (NumberOfBytesRead)
{
Buffer[ NumberOfBytesRead ] = TCHAR( 0 );
SetFocus(GetDlgItem(hwnd, IDC_EDIT1));
SetDlgItemText(hwnd, IDC_EDIT1, Buffer);
//MessageBox(hwnd,Buffer,"a",MB_OK);
}
else
{
break;
}
}
// Close write pipe
if(hWritePipe)
{
CloseHandle( hWritePipe );
}
if(hReadPipe)
{
CloseHandle( hReadPipe );
}
WaitForSingleObject( pi.hProcess, INFINITE );
cp = TRUE;
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);
}
-
February 16th, 2008, 02:20 PM
#2
Re: While Writing Exe get hang...
ReadFile is for synchronous reading and can be blocked if pipe is waiting to be written to. Your code is stuck in ReadFile.
Use ReadFileEx to make it asynchronous. See MSDN for details.
Another approach is to read results after process is done.
In the snippet below, I did not add any code to check errors. You should check if buffer is long enough to accommodate string.
You can also consider using different cursor to let user know that app is working.
The best approach would be to create worker thread to handle reading; application will not appear frozen.
Code:
#define PIPE_BUF_LEN 500
BOOL PipeIt(HWND hwnd)
{
int iProcID = -1;
STARTUPINFO si = {0};
SECURITY_ATTRIBUTES saAttr = {0};
HANDLE hReadPipe = NULL, hWritePipe = NULL;
PROCESS_INFORMATION pi = {0};
DWORD NumberOfBytesRead = 0;
TCHAR Buffer[PIPE_BUF_LEN] = "";
SetDlgItemText(hwnd, IDC_EDIT1, "Please wait, working...");
InvalidateRect(hwnd, NULL, TRUE);
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
BOOL bRes = CreatePipe(&hReadPipe, &hWritePipe, &saAttr, 0);
if (!bRes)
{
MessageBox(NULL, "Failed to create pipe.", "Error", MB_OK);
return(FALSE);
}
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdOutput = hWritePipe;
bRes = CreateProcess(NULL, (LPTSTR)(LPCTSTR)"ping 192.168.0.96", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
if (!bRes)
{
MessageBox(NULL, "Failed to create process.", "Error", MB_OK);
return(FALSE);
}
WaitForSingleObject(pi.hProcess, INFINITE);
bRes = ReadFile(hReadPipe, Buffer, PIPE_BUF_LEN, &NumberOfBytesRead, NULL);
if(!bRes)
{
return FALSE;
}
Buffer[NumberOfBytesRead] = TCHAR(0);
SetDlgItemText(hwnd, IDC_EDIT1, Buffer);
CloseHandle(hWritePipe);
CloseHandle(hReadPipe);
return(bRes);
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, (DLGPROC)DialogProc);
return 0;
}
Since dialog is modal and you do not create another window, GetMessage loop is not needed.
There are only 10 types of people in the world:
Those who understand binary and those who do not.
-
February 16th, 2008, 03:36 PM
#3
Re: While Writing Exe get hang...
Your sample doesn't work when Buffer is a WCHAR * (that is, when Unicode is defined and TCHAR == WCHAR). However, it works perfectly fine when the buffer is a char buffer even though the project uses Unicode.
Here's how it worked for me...
Code:
#include <windows.h>
#include "resource.h"
#define PIPE_BUF_LEN 65536
BOOL PipeIt(HWND hwnd)
{
STARTUPINFO si;
SECURITY_ATTRIBUTES saAttr;
HANDLE hReadPipe = NULL, hWritePipe = NULL;
PROCESS_INFORMATION pi;
DWORD NumberOfBytesRead = 0;
char Buffer[PIPE_BUF_LEN];
ZeroMemory (&pi, sizeof (pi));
ZeroMemory (&si, sizeof (si));
ZeroMemory (&saAttr, sizeof (saAttr));
ZeroMemory (Buffer, PIPE_BUF_LEN);
SetDlgItemText(hwnd, IDC_EDIT1, TEXT("Please wait, working..."));
saAttr.nLength = sizeof (SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
BOOL bRes = CreatePipe(&hReadPipe, &hWritePipe, &saAttr, 0);
if (!bRes)
{
MessageBox(hwnd, TEXT("Failed to create pipe."), TEXT("Error"), MB_OK);
return(FALSE);
}
GetStartupInfo (&si);
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdOutput = hWritePipe;
TCHAR buffer[256];
lstrcpy (buffer,TEXT("ping 192.168.1.1"));
bRes = CreateProcess(NULL, buffer, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
if (!bRes)
{
MessageBox(hwnd, TEXT("Failed to create process."), TEXT("Error"), MB_OK);
return(FALSE);
}
WaitForSingleObject(pi.hProcess, INFINITE);
bRes = ReadFile(hReadPipe, Buffer, PIPE_BUF_LEN, &NumberOfBytesRead, NULL);
if(!bRes)
{
return FALSE;
}
Buffer[NumberOfBytesRead] = 0;
SetDlgItemTextA(hwnd, IDC_EDIT1, Buffer);
CloseHandle(hWritePipe);
CloseHandle(hReadPipe);
return(bRes);
}
LRESULT CALLBACK MainWindowProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, (DLGPROC)MainWindowProc);
return 0;
}
-
February 16th, 2008, 04:08 PM
#4
Re: While Writing Exe get hang...
also see my response under your other thread.
added link just for future reference in case someone finds this in a search later 
http://www.codeguru.com/forum/showthread.php?t=446185
Last edited by bderagon; February 16th, 2008 at 04:10 PM.
-
February 18th, 2008, 04:03 AM
#5
Re: While Writing Exe get hang...
Thks all...
Manage to redirect the data..But in synchronously manner...For asynchronous how do i call ReadFileEx??
Gone through MSDN but couldn't understand "lpOverlapped" and "lpCompletionRoutine"...
What changes do i need to put in my code..
I believe this would be my last questio regarding this prob..
Hope to see reply soon..
-
February 19th, 2008, 09:59 AM
#6
Re: While Writing Exe get hang...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|