Writing to parallel port and USB port

Hi,

I am developing an application that sends raw data to a printer via serial,
parallel and USB ports. For serial port I would open a handle to the COM
port using CreateFile and read and write to it. Is the same method of
communication applicable for parallel port?

For USB, I understand that a handle to the USB port cannot be obtained.
Instead a handle to the device has to be obtained. However, I am not clear
on how this is to be done. Kindly help me out.

Thanks,
Reema

Hi Reema,

for parallel port u may use LPT1 ie, for device object in driver
\Device\Parallel0

u can use the same CreateFile function to do this…

For usb device, u will have to enumerate the device using the specific
class guid, and using SetupDi Functions…

The sample code shows how to do that…

HANDLE GetDeviceViaInterface( GUID* pGuid, DWORD instance)
{
// Get handle to relevant device information set
HDEVINFO info = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT |
DIGCF_INTERFACEDEVICE);
if(info==INVALID_HANDLE_VALUE)
{
printf(“No HDEVINFO available for this GUID\n”);
return NULL;
}

// Get interface data for the requested instance
SP_INTERFACE_DEVICE_DATA ifdata;
ifdata.cbSize = sizeof(ifdata);
if(!SetupDiEnumDeviceInterfaces(info, NULL, pGuid, instance, &ifdata))
{
printf(“No SP_INTERFACE_DEVICE_DATA available for this GUID
instance\n”);
SetupDiDestroyDeviceInfoList(info);
return NULL;
}

// Get size of symbolic link name
DWORD ReqLen;
SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL);
PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail =
(PSP_INTERFACE_DEVICE_DETAIL_DATA)(new char[ReqLen]);
if( ifDetail==NULL)
{
SetupDiDestroyDeviceInfoList(info);
return NULL;
}

// Get symbolic link name
ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if( !SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen,
NULL, NULL))
{
SetupDiDestroyDeviceInfoList(info);
delete ifDetail;
return NULL;
}

// printf(“Symbolic link is %s\n”,ifDetail->DevicePath);

// Open file
HANDLE rv = CreateFile( ifDetail->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

delete ifDetail;
SetupDiDestroyDeviceInfoList(info);
return rv;
}

U may open a handle to the device by calling the above function

UsbDevHdle = GetDeviceViaInterface((LPGUID)&GUID_UR_USB_DEVICE,0);
if( UsbDevHdle==NULL)
{
printf(“success opening usb device\n”);
return 1;
}
else
printf(“failure opening usb device\n”);

“GUID_UR_USB_DEVICE” this should be what u had used in the device driver…
as an interface…

Hope this helps…

Shiva Prasad
xxxxx@techie.com