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("COM1", szTarget, 1024))
printf("QueryDosDevice() failed with error %d\n", GetLastError());
else
fRedirected = (0 == strnicmp(szTarget, "\\Device\\RdpDr\\;",
strlen("\\Device\\RdpDr\\;")));
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("ServerCOM1", szTarget, MAX_PATH))
fMapped = DefineDosDevice(DDD_RAW_TARGET_PATH, "ServerCOM1",
"\\Device\\Serial0");
else
printf ("ServerCOM1 already mapped\n");
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,
"ServerCOM1", "\\Device\\Serial0");