Techniques for Generating Fake COM Ports

I wrote this last July on an internal blog.  Now it seems that it should be made available outside as well.  It is only meant for testing COM port mappings with Citrix but the ideas could be applied elsewhere with regards to building fake device names.

Original Post:

Since I am doing testing with PortICA and COM ports, I needed a way of constructing COM ports to test the full range of Client ComPort Mapping (CCM) support.  CCM can support up to 32 COM ports from the client and normally this would be difficult to create.

With some games within symbolic links, it actually is not that bad.

I only started working on this on Tuesday so please excuse the roughness of how it is laid out.

//
// FAKECOM.cpp : Incredibly simple program to generate fake COM ports for the sake of testing.
//               This program will create all the COM ports from COM1 to COM32 in the current session.
//               All of these COM ports will point to the standard serial driver with the first port (\Device\Serial0)
//
#include “stdafx.h”

#define MAX_COM_PORT_NUMBER         32
#define MAX_COM_PORT_NAME_LENGTH    10
#define MAX_DEVICE_NAME_LENGTH      500
#define SERIAL_DRIVER_PATH          L”\\Device\\Serial0″

WCHAR g_ComPortName[MAX_COM_PORT_NAME_LENGTH];
WCHAR g_DeviceName[MAX_DEVICE_NAME_LENGTH];

void RemoveDeviceDefine(LPCWSTR ComPortName);

int wmain(int argc, WCHAR * argv[])
{
int i;
BOOL bRet;
DWORD Size;

for(i=1; i <= MAX_COM_PORT_NUMBER; i++)
{
swprintf(g_ComPortName, MAX_COM_PORT_NAME_LENGTH, L”COM%u”, i);

// make sure to remove all other entries first
RemoveDeviceDefine(g_ComPortName);

bRet = DefineDosDeviceW(DDD_RAW_TARGET_PATH, g_ComPortName, SERIAL_DRIVER_PATH);

if(bRet == FALSE)
{
wprintf(L”DefineDosDeviceW failed index(%u) rc(%x)\n”, i, GetLastError());
}
else
{
wprintf(L”Device %s defined\n”, g_ComPortName);
}
}

// now go prove that it is set correctly
for(i=1; i <= MAX_COM_PORT_NUMBER; i++)
{
swprintf(g_ComPortName, MAX_COM_PORT_NAME_LENGTH, L”COM%u”, i);

Size = QueryDosDeviceW(g_ComPortName, g_DeviceName, MAX_DEVICE_NAME_LENGTH);

if(Size == 0)
{
wprintf(L”QueryDosDeviceW failed index(%u) rc(%x)\n”, i, GetLastError());
}
else
{
wprintf(L”Device %s defined as %s\n”, g_ComPortName, g_DeviceName);
}
}

return 0;
}

void RemoveDeviceDefine(LPCWSTR ComPortName)
{
DWORD Size = 0;
BOOL bRet;

do
{
bRet = DefineDosDeviceW(DDD_REMOVE_DEFINITION, ComPortName, NULL);

// check to see if there is anything left to remove
if(bRet)
{
Size = QueryDosDeviceW(ComPortName, g_DeviceName, MAX_DEVICE_NAME_LENGTH);
}

} while((bRet != FALSE) && (Size != 0));

return;
}

Enjoy!

About

Live near Brisbane, Australia. Software developer currently focused on iOS and Android. Avid Google Local Guide

Tagged with: , , ,
Posted in COM Port Redirection, PortICA
Archives
Categories
Follow Red Circle Blog on WordPress.com
%d bloggers like this: