WTSVirtualChannelRead stops reading data from virtual channel and Marshal.GetLastWin32Error() return

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
On Win2K3 / Win2K8 Terminal Server, C# Pinvoke of WTSVirtualChannelRead stops reading data from virtual channel and Marshal.GetLastWin32Error() returns ERROR_INVALID_HANDLE. I tried GC.KeepAlive() as well as SafeHandle, but neither helped.
Code pieces for reference:<br/>
=========================
<p style="line-height:normal; margin-bottom:0pt <span style="line-height:115%; font-family:Consolas; font-size:9.5pt [DllImport(<span style="color:#a31515 "Wtsapi32.dll", SetLastError =<span style="color:blue true)]
<p style="line-height:normal; margin-bottom:0pt <span style="color:blue; font-family:Consolas; font-size:9.5pt static<span style="font-family:Consolas; font-size:9.5pt <span style="color:blue void ReadThread(<span style="color:#2b91af Object
TSCommunicationObjRef)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt {
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue byte[] data = <span style="color:blue new<span style="color:blue byte[1608];
<span style="color:green //1608 is as per limit by Terminal Services Virtual Channel
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue int bytesReaded = -1;
<p style="line-height:normal; margin-bottom:0pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
TSCommunication TSComObj = TSCommunicationObjRef <span style="color:blue
as TSCommunication;
<p style="line-height:normal; margin-bottom:0pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue try
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
{
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue while (TSComObj.m_VirtualChannelHandle != <span style="color:#2b91af
IntPtr.Zero)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
{
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue while (WtsApi32.WTSVirtualChannelRead(TSComObj.m_VirtualChannelHandle, 2000, data, data.Length,<span style="color:blue ref bytesReaded))
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
{
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue if (bytesReaded == 0)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
{
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue int nWin32LastErr = -1;
<p style="line-height:normal; margin-bottom:0pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
nWin32LastErr = Marshal.GetLastWin32Error();
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
}
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue else
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
{
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green // receive the read data and process
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span> }
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
}<span>
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
}
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue catch(...){} <span style="color:green //different kind of catch blocks
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue finally
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
{
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af GC.KeepAlive(TSComObj);
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
}
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
}
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt } <hr class="sig Pradeep

View the full article
 
Back
Top