Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i have 2 projects:
1. client - which is a DLL that is integrated to the remote desktop product.
client can send using VirtualChannelWrite, inside CHANNEL_ENTRY_POINTS struct.

it also receives data through a delegate that we register on.

2. Server - which inits the channel, using: WTSVirtualChannelOpen
then, there's a
while (true)
{
   BOOL code = WTSVirtualChannelRead(... PULONG pBytesRead);

   // 0 means error
   if (code == 0)
   {
       int lastError = GetLastError();
       // crash or do anything
   }
   else if (pBytesRead > 0)         // this never happens after closing and opening mstsc again (when the server app is still active)
   {
      // do something, received data
   }

}


when i open the mstsc, and then lunch the server, everything works fine.
data is sent and received from client to server.

when i close the remote desktop window (press on X), the server is still running (inside the loop).
but when i open the mstsc again, then even when i send data, the receiving side is not getting it.

i assume that, because when you terminate the client session, it is said that the virtual channel is closed.

BUT, the server side has no idea about it.
i keep getting the same code from the call (0), and same lasterror - 667, which is 667 also when i get a correct data.

i'm in the dark here,
how can i know when a new session started? (when i connected to the mstsc again), i'd like to free the virtual channel handle at the server side, and then reconnect using WTSVirtualChannelOpen.

please help :|

Ariel.
Posted
Updated 31-Oct-11 12:45pm
v2

You need to add some code for the case where pBytesRead is zero. Also you should only call GetLastError() in the error case. You are calling it in every iteration of your loop so in most cases the value you receive is meaningless, as error codes are only posted to the last error when the previous API call fails. See here[^] for more details on return values.
 
Share this answer
 
Comments
arielb 31-Oct-11 18:30pm    
The GetLastError() was actually for debug cases, it will go away.

And which code should i add?
i get the same results when the channel is active, and no data was sent yet,
and when the virtual channel is closed, and i reopen mstsc (The client does issue a connect, with no errors), but the server's channel is already gone.
same pBytesRead=0, and no error returns.

I just want somehow to understand that the client disconnected, or even better, connected, to issue an open call.
is that possible?

thanks :)
Richard MacCutchan 31-Oct-11 19:18pm    
Look at the documentation. If the return value is zero then you must check GetLastError() to see why it failed. In all other cases the value of GetLastError() is not valid. You need to do things in the correct order to check the status of your API call. And if you have received an error status you should break out of your loop and take recovery or reconnect action as determined by the error code.
arielb 1-Nov-11 2:30am    
Again, the return value and the GetLastError returns the same values, when the channel is up and working, and when i reopened mstsc while server is up (and data is not received anymore by either side)

so i cant use it
According to the documentation the function returns 0 if it fails. Only if you get non-zero should you check the number of bytes read. You cannot assume that you will get zero for number of bytes *only* on error condition.

So, first check 'code' for 0, then check GetLastError().

if 'code' is non-zero, then check the number of bytes you received.
 
Share this answer
 
Comments
arielb 31-Oct-11 18:43pm    
Its not what i've asked...
even if the channel is not active (i guess so atleast), i get no error when i send at the client side, and read at the server side.
the data just wont transmit (again, after closing the mstsc window).

i need to find out how to understand at the server that the client closed the window (if i'd get errors, it would be good, but thats not the case
Chuck O'Toole 31-Oct-11 18:50pm    
ah, I see you updated your question to include more of the code, including the parts I couldn't see when I posted this. So sorry for the unhelpful answer.
Allright, noone seemed to give me a good answer..

anyway, i've done a workaround:
at the while loop (at the server side), i've added the following pesudo code:

C++
// this is the handle i got from the open erlier: HANDLE hdl
if (counter >= SOME_NUMBER_OF_LOOPS)
{
   counter=0;
   WTSVirtualChannelClose(&hdl);
   hdl = WTSVirtualChannelOpen(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, "something"); 
   
   // if client is not connected, just sleep for a bit.
   if (hdl == NULL)
   {
      Sleep(1000)
   }
}


the open gives me null if the client is not connected.
 
Share this answer
 

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