CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Feb 2008
    Posts
    138

    Problem in running exe

    Hi all...

    I'm trying to run exe (Ping to be specific) with argument (www.yahoo.com) with the help of createprocess in button click event...
    My prob is tht it's not running and how to redirect the output which usually comes in console window..I need to redirect it to the editBox...

    Following is code snippet..



    case IDOK:
    {
    char buff1[] = "ping.exe";
    char buff2[] = "www.yahoo.com";
    STARTUPINFO sif = {0};
    PROCESS_INFORMATION pif = {0};
    sif.hStdOutput = IDC_EDIT1;
    CreateProcess(buff1,buff2,NULL,NULL,FALSE,CREATE_NO_WINDOW,NULL,NULL,&sif,&pif);
    break;
    }


    Reply will be appreciated..
    Thnks...

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Problem in running exe

    Code:
    sif.hStdOutput = IDC_EDIT1;
    hStdOutput needs a handle. IDC_EDIT is not a handle, it is a resource number.

    And also, if you want to use hStdOutput you need to set a flag.

    hStdOutput :
    Ignored unless dwFlags specifies STARTF_USESTDHANDLES. Specifies a handle that will be used as the standard output handle to the process if STARTF_USESTDHANDLES is specified.

  3. #3
    Join Date
    Feb 2008
    Posts
    138

    Re: Problem in running exe

    Thnks...

    But now i declared a handle also for hStdOutput
    and i changed my code as.


    case IDOK:
    {
    char buff1[] = "ping.exe";
    char buff2[] = " www.yahoo.com";
    STARTUPINFO sif;
    PROCESS_INFORMATION pif;
    memset(&sif, 0, sizeof(sif));
    memset(&pif, 0, sizeof(pif));

    sif.cb = sizeof(sif);
    sif.dwFlags = STARTF_USESTDHANDLES;
    sif.hStdOutput = hChildStdoutWr;

    int bFuncRetn = CreateProcess(NULL,
    buff2,
    NULL,
    NULL,
    TRUE,
    0,
    NULL,
    NULL,
    &sif,
    &pif);

    if (bFuncRetn == 0)
    MessageBox(hwnd , "Creation Failed", "Error", MB_OK);
    else
    {
    CloseHandle(pif.hProcess);
    CloseHandle(pif.hThread);
    return bFuncRetn;
    }


    break;
    }



    But Still "Creation Failed" Mess is coming...

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Problem in running exe

    please use [ code ] [ /code ] around your code.

    you forgot to pass buff1. Also, the command (ping.exe) must contain the full path. CreateProcess DOESN'T search the PATH-environment to look for the exe file.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem in running exe

    1. You seem to like the trial and error method...
    But before using it you must read the documentation about APIs you are using!
    The simple message "Creation Failed" makes no sense because it doesn't show you the reason of failure!
    From MSDN article "CreateProcess":
    Return Values
    If the function succeeds, the return value is nonzero.

    If the function fails, the return value is zero. To get extended error information, call GetLastError.
    So, why don't you call GetLastError to know the reason of failure?

    2. You should post the code that can be compiled, otherwise no one could help you to find all your bugs/problems with this code.
    BTW, what is hChildStdoutWr? where and how is it defined?

    3. You have to use Code tags around your code snippets. Otherwise it is hard to read understand the code. Please, check it out: Announcement: Before you post....
    Victor Nijegorodov

  6. #6
    Join Date
    Feb 2008
    Posts
    138

    Re: Problem in running exe

    Thnks Skizmo..

    Now it works..Atleast it's opening the process...I was not giving full path of exe..But only "Ping" works in c#..

    Neverthless can you tell me in this case how to redirect the standard output to my editBox???

  7. #7
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Problem in running exe

    You can to redirect stdout to a pipe; capture all the text in a buffer; then update your edit box.

    Viggy

  8. #8
    Join Date
    Oct 2007
    Location
    Fredericksburg, VA
    Posts
    41

    Re: Problem in running exe

    See what I'm doing, encountering slightly different issues, trying to do the same thing. Maybe we can help each other out here.

    http://www.codeguru.com/forum/showthread.php?t=446229
    Last edited by bderagon; February 15th, 2008 at 04:18 PM.

  9. #9
    Join Date
    Oct 2007
    Location
    Fredericksburg, VA
    Posts
    41

    Re: Problem in running exe

    Did you get it work?

  10. #10
    Join Date
    Feb 2008
    Posts
    138

    Re: Problem in running exe

    Hi bderagon...

    Thanks for reply but i still didn't manage to understand createpipe...I took handle for hStdOutput but how to use it to redirect the text in edit box??

    And also i need to display the text in real time..I mean just like ping do..

  11. #11
    Join Date
    Feb 2008
    Posts
    138

    Re: Problem in running exe

    Problem almost solved...Manage to run proces...But the big point is that can not redirect console output to my listBox...

    Here is my code..

    Code:
    #include "Stdafx.h"
    #include "windows.h"
    #include "resource.h"
    #include "stdio.h"
    #define BUFSIZE 4096
    
    
    HWND WindowInterface=NULL;
    HINSTANCE WindowInterfaceInstance=NULL;
    int CALLBACK WindowInterfaceProc(HWND hwnd,UINT mes,WPARAM wParam,LPARAM lParam);
    
    HANDLE hReadPipe, hWritePipe;
    //HANDLE hChildStdinRd, hChildStdinWr,hChildStdoutRd, hChildStdoutWr,hInputFile, hStdout;
    
    //VOID WriteToPipe(VOID); 
    //VOID ReadFromPipe(VOID); 
    
    
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
     	
    	WindowInterfaceInstance = hInstance;
    		
    	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, WindowInterfaceProc);
    
    	return 0;
    }
    
    int CALLBACK WindowInterfaceProc(HWND hwnd, UINT mes, WPARAM wParam, LPARAM lParam)
    {
    	switch(mes)
    	{
    		case WM_COMMAND:
    		{
    			switch(LOWORD(wParam))
    			{
    		 		case IDOK:
    				{
    					char buff1[] = "c:\\windows\\system32\\ping.exe ";
    					char buff2[] = " 192.168.0.96 -t ";
    					//char buff3[] = " > c:\\ashish.txt";
    					
    					char szBuffer[MAX_PATH];
    					sprintf(szBuffer, "%s %s", buff1, buff2);
    
    
    					STARTUPINFO siStartupInfo = {0};
    					PROCESS_INFORMATION piProcessInfo = {0};
    					memset(&siStartupInfo, 0, sizeof(siStartupInfo));
    					memset(&piProcessInfo, 0, sizeof(piProcessInfo));
    					
    					siStartupInfo.cb = sizeof(siStartupInfo);
    					siStartupInfo.dwFlags = STARTF_USESTDHANDLES;
    					siStartupInfo.dwFlags = STARTF_USESHOWWINDOW;
    					siStartupInfo.wShowWindow = true;
    					siStartupInfo.hStdOutput = hWritePipe;
    					
    
    					SECURITY_ATTRIBUTES saAttr;
    					saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
    					saAttr.bInheritHandle = true;
    					saAttr.lpSecurityDescriptor = NULL;
    
    
    					BOOL cp = CreatePipe( &hReadPipe, &hWritePipe, &saAttr, 0 );
    
    						
    					cp = CreateProcess( NULL, szBuffer, NULL, NULL, FALSE, 0, NULL, NULL, &siStartupInfo, &piProcessInfo );
    				
    					if(cp)
    					{
    								
    						 //siStartupInfo.hStdOutput = GetDlgItemText(hWritePipe,IDC_LIST1);
    					}
    
    				/*	else
    					{
    						CloseHandle( hWritePipe );
    						DWORD NumberOfBytesRead;
    						TCHAR Buffer[ 500 ];
    						while(::ReadFile( hReadPipe, Buffer, sizeof(Buffer)/sizeof(Buffer[ 0 ])-1,&NumberOfBytesRead, NULL ) )
    						{
    							if (NumberOfBytesRead)
    							{
    								Buffer[ NumberOfBytesRead ] = TCHAR( 0 );
    								strPing += Buffer;
    							}
    							else
    							{
    								break;
    							}
    						};
    
    						CloseHandle( hReadPipe );
    						WaitForSingleObject( piProcessInfo.hProcess, INFINITE );
    						cp = TRUE;
    						break;*/
    					}
    					break;
    				
    				case IDCANCEL:
    				{
    					MessageBox(NULL, "EXITING" , "Test", MB_OK);
    					ExitProcess(0);
    					return TRUE;
    					break;
    				}
    
    				/*case IDC_EDIT1:
    				{
    					
    				}*/
    
    			}
    		}
    
    		return TRUE;
    
    		case WM_CLOSE:
    			{
    				MessageBox(NULL, "Hello1" , "Test", MB_OK);
    				ExitProcess(0);
    				return TRUE;
    			}
    
    		case WM_DESTROY:
    		{
    			MessageBox(NULL, "Hello2", "Test", MB_OK);
    			ExitProcess(0);
    			return TRUE;
    		}
    	}
    
    	return FALSE;
    }
    Last edited by techie.ashish; February 16th, 2008 at 06:18 AM.

  12. #12
    Join Date
    Feb 2008
    Posts
    138

    Re: Problem in running exe

    What to write in if block??

  13. #13
    Join Date
    Oct 2007
    Location
    Fredericksburg, VA
    Posts
    41

    Re: Problem in running exe

    (critical code change) First code I changed here, is your startup info, your pipe flag was being overwritten by your show window flag. You need to use the |= operator, or better yet, declare them both on the same line as I did here for you.

    Code:
    	siStartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;

    or, as I was saying, alternative is:

    Code:
    	siStartupInfo.dwFlags = STARTF_USESTDHANDLES;
    	siStartupInfo.dwFlags |= STARTF_USESHOWWINDOW;
    (non-critical code change) Secondly, I would call my handles to the pipes local to the block I was using them in, just good practice as it'll save you some memory; unless you have reason to use the handles outside of this code block.
    (non-critical code change) Third, I would add some kind of error checking to the pipe creation, as a good practice measure.

    Next, you have the code you need commented out here, ecept, it goes in your if block, not your else block:
    Here, I wrote it for you with some minor changes:

    Code:
    	if(cp)
    	{
    		WaitForSingleObject( piProcessInfo.hProcess, INFINITE );
    Put the wait for single object line up there, so we know its finished writing before we close our write pipe.
    Code:
    		CloseHandle(hWritePipe);
    		TCHAR Buffer[5000];
    		DWORD NumberOfBytesRead;
    		while(ReadFile(hReadPipe, Buffer, sizeof(Buffer), &NumberOfBytesRead, NULL))
    		{
    			Buffer[NumberOfBytesRead] = TCHAR(0);
    			strPing+= Buffer;
    		}
    Somewhere in your code, for that above section to work, declare strPing as a CString.
    The output of our pipe wasn't to a control, pipes don't work like that, it was to a temporary buffer we had to read from first and input into a string.
    Now your info is stored inside a string, and we can move it from a CString, to a listbox.

    I didn't use a list box, I used a multi-line edit-box, but you can see what I did here.
    Your next section of code will look something like this:

    Code:
    CEdit *c_txtPing = reinterpret_cast<CEdit *>(GetDlgItem(txtPing));
    c_txtPing->SetWindowText(strPing);
    If you decide to use the default IDC name, just use that in place of GetDlgItem(txtPing) so it'd look like GetDlgItem(IDC_TEXT1) or something to that effect.

    Also it makes it easier to keep track of your progress in the future if you keep your problem to one thread, .
    Last edited by bderagon; February 16th, 2008 at 04:13 PM.

  14. #14
    Join Date
    Feb 2008
    Posts
    138

    Re: Problem in running exe

    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..

  15. #15
    Join Date
    Feb 2008
    Posts
    138

    Re: Problem in running exe

    hi all..

    Problem solved..Manage to read data asynchronously..But the only problem is tht whenever i redirect data to my editbox my cancel button remains out of focus..I mean i can't cancel my ping command in mid of process..
    Do i need to create Thread for tht?? And if yes how i do tht..
    Here is my code

    Code:
    #include "Stdafx.h"
    #include "windows.h"
    #include "resource.h"
    #include "stdio.h"
    #define BUFSIZE 4096
    
    
    HWND WindowInterface=NULL;
    HINSTANCE WindowInterfaceInstance=NULL;
    int CALLBACK WindowInterfaceProc(HWND hwnd,UINT mes,WPARAM wParam,LPARAM lParam);
    HWND hPWindow;
    BOOL m_bStopped = FALSE;
    
    
    void AppendText(char *Msg)
    {
    	static HWND Edit = NULL;
    	if(Edit == NULL)
    		Edit = GetDlgItem(hPWindow, IDC_EDIT1);
    
    	SetFocus(Edit);
    	SendMessage(Edit,EM_REPLACESEL,(WPARAM)0,(LPARAM) Msg);
    }
    
    BOOL MyFunction(HWND hwnd)
    {
    	HANDLE					PipeReadHandle;
    	HANDLE					PipeWriteHandle;
    	PROCESS_INFORMATION		ProcessInfo;
    	SECURITY_ATTRIBUTES		SecurityAttributes;
    	STARTUPINFOA			StartupInfo;
    	BOOL					Success;
    
    	ZeroMemory( &StartupInfo,			sizeof( StartupInfo ));
    	ZeroMemory( &ProcessInfo,			sizeof( ProcessInfo ));
    	ZeroMemory( &SecurityAttributes,	sizeof( SecurityAttributes ));
    
    	SecurityAttributes.nLength              = sizeof(SECURITY_ATTRIBUTES);
    	SecurityAttributes.bInheritHandle       = TRUE;
    	SecurityAttributes.lpSecurityDescriptor = NULL;
    
    	Success = CreatePipe(&PipeReadHandle, &PipeWriteHandle, &SecurityAttributes, 0);
    	if(!Success)
    	{
    		MessageBox(NULL, "Error creating pipe", "Test", MB_OK);
    		return FALSE;
    	}	
    
    	StartupInfo.cb           = sizeof(STARTUPINFO);
    	StartupInfo.dwFlags      = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    	StartupInfo.wShowWindow  = SW_HIDE;
    	StartupInfo.hStdOutput   = PipeWriteHandle;
    	StartupInfo.hStdError    = PipeWriteHandle;
    
    	char szBuffer[MAX_PATH];
    	TCHAR Info[MAX_PATH];
    	GetWindowsDirectory(Info , MAX_PATH);
    	sprintf(szBuffer, Info);
    	lstrcat(szBuffer, "\\system32\\ping 192.168.0.96");
    
    	Success = CreateProcess( 
    							NULL,
    							szBuffer,
    							NULL,
    							NULL,
    							TRUE,
    							0,
    							NULL,
    							NULL,
    							&StartupInfo,
    							&ProcessInfo);
    
    	if(!Success)
    	{
    		DWORD err = GetLastError();
    		MessageBox(NULL, "Error creating process", "Test", MB_OK);
    		return FALSE;
    	}
    
    	DWORD	BytesLeftThisMessage = 0;
    	DWORD	NumBytesRead;
    	CHAR	PipeData[2*MAX_PATH]; 
    	DWORD	TotalBytesAvailable = 0;
    
    	while(1)
    	{ 
    		NumBytesRead = 0;
    
    		Success = PeekNamedPipe(PipeReadHandle,				// handle to pipe to copy from 
    								PipeData,					// pointer to data buffer 
    								1,							// size, in bytes, of data buffer 
    								&NumBytesRead,				// pointer to number of bytes read 
    								&TotalBytesAvailable,		// pointer to total number of bytes available
    								&BytesLeftThisMessage		// pointer to unread bytes in this message 
    								);
    		
    		if(!Success)
    		{
    			MessageBox(NULL, "PeekNamedPipe fialed", "Test", MB_OK);
    			break;
    		}
    
    		if(NumBytesRead)
    		{
    			Success = ReadFile(
    								PipeReadHandle,		// handle to pipe to copy from 
    								PipeData,			// address of buffer that receives data
    								(sizeof(PipeData) - 1),	// number of bytes to read
    								&NumBytesRead,		// address of number of bytes read
    								NULL);				// address of structure for data for overlapped I/O
    							
    
    			if(!Success)
    			{
    				MessageBox(NULL, "ReadFile fialed", "Test", MB_OK);				
    				break;
    			}
    
    			PipeData[NumBytesRead] = '\0';			
    			AppendText(PipeData);	
    		}
    		else
    		{
    			if(WaitForSingleObject(ProcessInfo.hProcess, 0) == WAIT_OBJECT_0)
    				break;
    		}
    
    		if(m_bStopped)
    		{
    			TerminateProcess(ProcessInfo.hProcess, 0);			
    			break;
    		}
    		
    		Sleep(2);
    	}
    	
    	return(Success);
    }
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
     	
    	WindowInterfaceInstance = hInstance;
    		
    	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)WindowInterfaceProc);
    	
    	return(0);
    }
    
    int CALLBACK WindowInterfaceProc(HWND hwnd, UINT mes, WPARAM wParam, LPARAM lParam)
    {
    	switch(mes)
    	{
    		case WM_COMMAND:
    		{
    			switch(LOWORD(wParam))
    			{
    		 		case IDOK:
    				{
    					hPWindow = hwnd;
    					MyFunction(hwnd);
    					break;
    				}
    				
    				case IDCANCEL:
    				{
    					m_bStopped = TRUE;
    					Sleep(20);
    					ExitProcess(0);
    					return TRUE;
    					break;
    				}
    
    			}
    		}
    
    		return TRUE;
    
    		case WM_CLOSE:
    			{
    				ExitProcess(0);
    				return TRUE;
    			}
    
    		case WM_DESTROY:
    		{
    			ExitProcess(0);
    			return TRUE;
    		}
    	}
    
    	return FALSE;
    }

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured