views:

199

answers:

1

Hi!

I'm trying rewrite xCmd which can start a process on a remote machine. Basically it installs itself as a service on the target computer and then starts the requested process. Everything is working fine, but i noticed an error. xCmd is communicating through pipes, and it uses WriteFile(). My problem is, that if i run my API (or the originally, both produce this error), then for the first time it will start the requested process, but if i start it one more time, then it will "freeze out" at this line:

WriteFile( hCommandPipe, &msg, sizeof(msg), &dwTemp, NULL );

WriteFile didn't returns any errorcode, the program just stops here. I can't even close cmd window. I can only close this, when i close on the target computer the service. Can anyone help me to solve this? It's really annoying and i have no idea :(

Here is the function which isn't working properly:

 BOOL ExecuteRemoteCommand()
 {
 DWORD dwTemp = 0;
 xCmdMessage msg;
 xCmdResponse response;

 ::ZeroMemory( &msg, sizeof(msg) );
 ::ZeroMemory( &response, sizeof(response) );

 FillMessage( &msg );

 // Send message to service
 WriteFile( hCommandPipe, &msg, sizeof(msg), &dwTemp, NULL );

 // Connects to remote pipes (stdout, stdin, stderr)
 if ( ConnectToRemotePipes( 5, 1000 ) )
 {
   StdOutput( _T("Ok\n\n") );

    // Waiting for response from service
    ReadFile( hCommandPipe, &response, sizeof(response), &dwTemp, NULL );
 }
 else
    StdOutput( _T("Failed\n\n") );

 if ( response.dwErrorCode == 0 ) 
    _tprintf( _T("\nRemote command returned %d(0x%X)\n"), 
              response.dwReturnCode, 
              response.dwReturnCode );
 else
    _tprintf( _T("\nRemote command failed to start. Returned error code is %d(0x%X)\n"), 
              response.dwErrorCode, 
              response.dwErrorCode );

 return TRUE;
}

Thanks in advance!

kampi

+2  A: 

I think that means that the other end of the pipe connection is not reading data out of the pipe and the pipe buffer is full.

It may also indicate that the other end has closed their pipe handle. For anonymous pipes that would cause an error when you try to write, but I don't remember if that's also true with named pipes.

If the pipe buffer is full when an application uses the WriteFile function to write to a pipe, the write operation may not finish immediately. The write operation will be completed when a read operation (using the ReadFile function) makes more system buffer space available for the pipe.

MSDN

The write operation will block until the data is read from the pipe so that the additional buffer quota can be released.

MSDN

Tim Sylvester
Thanks, for your reply, but i still doesn't understand :( Why does it work for the first run, and why does it freeze for the second time? I think i should 0 out a buffer or a variable but i don't know which one :(
kampi
@kampi I think the problem is on the other end of the pipe. Can you add the code where you're calling `ReadFile` on the pipe? It looks like either you're not reading all the data from the first request or the pipe is broken during the first request. Also, are you using named or anonymous pipes? Have you taken a look at the example programs? http://msdn.microsoft.com/en-us/library/aa365799%28VS.85%29.aspx
Tim Sylvester
@Tim you're right! The problem was at the other end of the pipe. The problem was with the CreateProcess function. It's bInheritHandles paramater was set to TRUE and that caused the problem. I set it to FALSE and now it's working fine. I didn't thought that that could be the problem. Thanks for your help!
kampi
@kampi Ah, handle inheritance is tricky, and causes several subtle problems. Do you know *why* it fixed the problem? Was the new process using the inherited handle somehow? It's nice to explain the fix a bit on these questions, so that when someone has the same problem and finds this page via Google, etc., they can see what the problem was and how to fix it.
Tim Sylvester
@Tim, unfortunately i don't understand why this fixed my problem. I didn't find any code which would use these handles. I still don't understand the full working of xCmd, but i'm understanding more and more day by day :) If i knew why it fixed my problem, trust me i explained it. I think this some xCmd specific problem. Thanks for your help!
kampi