Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi All,
[Application & code back ground] I have an MFC application which is executing on Windows Server 2008 R2 in WOW64 environment. In which on user input it defragments the selected drive on the disk. I initiated the process(.i.e. cmd /c defrag –v c: ) of defragmentation using the CreateProcess() API, along with this to display output of the process on the main screen I created the pipe using CreatePipe() API. I used PeekNamedPipe() & ReadFile() API to get the output and display.
[Problem Area] When the process is launched I am not getting the initial output as below:
Microsoft Disk fragmenter Copyright © 2007 Microsoft Crop.
Invoking defragmentation on (C: )….
I constantly monitor the output of the process while it is under progress but not able to get any thing as output in the pipe. It seem the process is not doing any thing and appears as if the application is not responding. But after certain period of time, once the process is about to completed I get result along with the initial data.

[Sample code]

//Pipe created if(0 == ::CreatePipe(&l_hStdOutRead, &l_hStdOutWrite, &l_SecurityAttribute, (DWORD)NULL)) { //Error code }
//Process created/launched if (0 == ::CreateProcess(NULL, (LPTSTR)f_csProcessName, &l_stSecurityAttributes, NULL, TRUE, CREATE_NO_WINDOW, NULL,NULL, &l_StartupInfo, &l_CmdPI)) { //Error code }
//Read output if (0 == ::PeekNamedPipe(m_hStdOutRead, l_cArrPeekBuffer, (DWORD)NULL, (LPDWORD)NULL, &l_dwAvailable, (LPDWORD)NULL)) { //Return to read again }
if (MFALSE == ::ReadFile(m_hStdOutRead, l_cArrOutput, min(BUFFER_SIZE - 2, l_dwAvailable), &l_dwRead, NULL) || !l_dwRead) { //error code }
//Display data.

Same code work fine on windows xp
If any one is aware of similar problem or worked on the similar issue please let me know the solution.
Posted
Updated 8-Jul-10 18:54pm
v5
Comments
Member 15624430 4-May-22 11:16am    
How to declare this

WaitForSingleObject hproc, 1

Getting error as function or procedure not defined

1 solution

Hi 1 day troubleshooting and found my solution

vb6, xp sp2

PeekNamedPipe does not populate BytesAvail after call


'Code:
BytesAvail = 0
PeekNamedPipe hReadPipe, ByVal 0&, 0, ByVal 0&, BytesAvail, ByVal 0&
If BytesAvail = 0 Then Exit Function
'BytesAvail is allways 0 :thumbsdown:


' Corected with this Code:
BytesAvail = 0
PeekNamedPipe hReadPipe, ByVal 0&, 0, ByVal 0&, BytesAvail, ByVal 0&
WaitForSingleObject hproc, 1 '
If BytesAvail = 0 Then Exit Function
' This is working !!!!!!!!!!!! :) :thumbsup:
' BytesAvail is populated after call to PeekNamedPipe
' Branko Geci *REMOVED MAIL*
 
Share this answer
 
v2
Comments
JF2015 23-Nov-10 5:25am    
Edited: Never post your mail address!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900