Microsoft KB Archive/314354

= How To Programmatically Determine Whether Serial Ports Are Redirected to a Terminal Server Client Computer =

Article ID: 314354

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft Win32 Application Programming Interface, when used with:
 * Microsoft Windows XP Professional

-



This article was previously published under Q314354



SUMMARY
The Remote Desktop client that is included with Windows XP permits you to initiate a Terminal Services session under circumstances where the server's serial ports (COM1, COM2, and so on) are redirected to the local client's serial ports. This article explains how a process running within a client session can determine whether the ports for that session are redirected to the client computer.



MORE INFORMATION
To determine whether a COM port has been redirected to the Terminal Services client computer, an application running within the session can query the COM port by using the QueryDosDevice function. If the target for the COM port begins with \Device\RdpDr\;, the port is being redirected to the client through the Terminal Services redirector (RdpDr).

For example, you can use the following code to determine whether COM1 has been redirected: BOOL fRedirected = FALSE; char szTarget[1024]; if (!QueryDosDevice(&quot;COM1&quot;, szTarget, 1024)) printf(&quot;QueryDosDevice failed with error %d\n&quot;, GetLastError); else fRedirected = (0 == strnicmp(szTarget, &quot;\\Device\\RdpDr\\;&quot;, strlen(&quot;\\Device\\RdpDr\\;&quot;))); If an application that is running in a client session with redirected serial ports needs to access a physical serial port on the Terminal Services server (as opposed to the redirected port on the client computer), the program must create a new symbolic link to the server's port by calling the DefineDosDevice function. Then, the application can open a handle to the serial port by using the following syntax to call the CreateFile function:

\\.\

Finally, it can use the handle to access the serial port.

The following code demonstrates how to create a symbolic link (ServerCOM1) to the first physical serial port on the Terminal Services server: // Map ServerCOM1 to the first physical serial port on the TS server. // Before you create the new mapping, verify that it does not already exist.

char szTarget[MAX_PATH]; BOOL fMapped = FALSE;

if (!QueryDosDevice(&quot;ServerCOM1&quot;, szTarget, MAX_PATH)) fMapped = DefineDosDevice(DDD_RAW_TARGET_PATH, &quot;ServerCOM1&quot;,         &quot;\\Device\\Serial0&quot;); else printf (&quot;ServerCOM1 already mapped\n&quot;); The device driver that manages a device registers the device name; the built-in Windows serial driver assigns \Device\Serial0 to the first physical serial port and \Device\Serial1 to the second. Custom vendor-supplied device drivers for add-on serial boards may or may not do the same. Additionally, although COM1 is normally mapped to the first physical serial port, this mapping is not mandatory. An application in a Terminal Services client session cannot know for certain whether this mapping protocol holds.

A client application can remove the symbolic link that it created by calling the DefineDosDevice function, as follows (it should do so after it is finished with the port): fMapped = DefineDosDevice(DDD_RAW_TARGET_PATH | DDD_REMOVE_DEFINITION,     &quot;ServerCOM1&quot;, &quot;\\Device\\Serial0&quot;);

Keywords: kbapi kbcommport kbhowto kbkernbase kbserial kbtermserv KB314354

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.