S
sanju
Hi all,
I'm developing a sample USB application on the host side, but it is
failing to open the pipe and write; It is getting the handle and
device path everything properly. I verified with the rwbulk.exe which
is provided by the microsoft as sample with the Win DDK. Even that is
getting the same device path and the pipe name and opening it nicely,
but my application is returning error code 2(ERROR_FILE_NOT_FOUND) in
the createfile for the pipe.
Can anyone help me in identifying why it is failing?
I'm using the LoopBack DSF sample as my device. and that is configured
to use bulkusb.sys.
#include "UsbDev.h"
#include <conio.h>
#include "setupapi.h"
#include "bulkusr.h"
#include "usbio.cpp"
#include <tchar.h>
char myPointer[200]={0};
HANDLE USBDeviceOpen(LPGUID );
HANDLE open_file (PCHAR);
HANDLE UsbRead ();
HANDLE UsbWrite ();
int ResetPipe(HANDLE);
void test(PUCHAR, ULONG *);
char inPipe[32] = "PIPE00";
char outPipe[32] = "PIPE01";
char DeviceName[256] = "";
int main()
{
HANDLE hOut = INVALID_HANDLE_VALUE;
INT getInput;
CHAR Exit = 0;
#if 1
hOut = USBDeviceOpen((LPGUID) &GUID_CLASS_I82930_BULK);
if(hOut) {
printf("Device Opened SuccessFully\n");
}
else {
printf("Calling Exit.........\n");
exit(0);
}
#endif
do {
printf("\n0. Exit \n");
printf("1.Write \n ");
printf("\nSelect the operation you want to perform:
");
scanf("%d", &getInput);
switch(getInput) {
case 0:
Exit = 1;
break;
case 1:
printf("\nwrite has not implemented\n");
UsbWrite();
break;
default:
printf("\nInvalid input\n");
break;
}
}while(Exit != 1);
CloseHandle(hOut);
return 0;
}
HANDLE USBDeviceOpen(LPGUID pguid)
{
HDEVINFO
hardwareDeviceInfo;
PSP_DEVICE_INTERFACE_DETAIL_DATA
functionClassDeviceData = NULL;
SP_DEVICE_INTERFACE_DATA deviceInfoData;
ULONG
predictedLength = 0;
ULONG
requiredLength = 0;
HANDLE
hOut =INVALID_HANDLE_VALUE;
CHAR
*devName = NULL;
ULONG
ErrCode = 0;
//LPCTSTR dev;
hardwareDeviceInfo = SetupDiGetClassDevs(pguid,
NULL, // Define no
enumerator (global)
NULL, // Define no
(DIGCF_PRESENT | // Only
Devices present
DIGCF_DEVICEINTERFACE)); // Function class devices.
if (hardwareDeviceInfo == INVALID_HANDLE_VALUE)
{
printf("Invalid handle from SetupDiGetClassDevs \n");
// return INVALID_HANDLE_VALUE;
}
deviceInfoData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);
if (!SetupDiEnumDeviceInterfaces(hardwareDeviceInfo,
NULL, // We don't care about
specific PDOs
pguid,
0,
&deviceInfoData))
{
// printf("SetupDiEnumDeviceInterfaces Failed \n");
printf("Device not found %d\n",GetLastError());
getch();
return 0;
}
if (SetupDiGetDeviceInterfaceDetail(hardwareDeviceInfo,
&deviceInfoData,
NULL, // probing so no
output buffer yet
0, // probing so output
buffer length of zero
&requiredLength,
NULL)) // not interested in
the specific dev-node
{
printf("SetupDiGetDeviceInterfaceDetail Failed \n");
// return INVALID_HANDLE_VALUE;
}
if (ERROR_INSUFFICIENT_BUFFER != GetLastError())
{
printf("ERROR_INSUFFICIENT_BUFFER \n");
// return INVALID_HANDLE_VALUE;
}
predictedLength = requiredLength;
// sizeof (SP_FNCLASS_DEVICE_DATA) + 512;
functionClassDeviceData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc
(predictedLength);
if (NULL == functionClassDeviceData)
{
printf("Allocation Failed \n");
// return INVALID_HANDLE_VALUE;
}
functionClassDeviceData->cbSize = sizeof
(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(hardwareDeviceInfo,
&deviceInfoData,
functionClassDeviceData,
predictedLength,
&requiredLength,
NULL))
{
free(functionClassDeviceData);
printf("SetupDiGetDeviceInterfaceDetail Failed \n");
// return INVALID_HANDLE_VALUE;
}
wcstombs(myPointer, functionClassDeviceData->DevicePath,
wcslen
( functionClassDeviceData->DevicePath)+1);
printf("Attempting to open %s\n", myPointer);
hOut = CreateFile(functionClassDeviceData->DevicePath,
GENERIC_READ |
GENERIC_WRITE,
FILE_SHARE_READ |
FILE_SHARE_WRITE,
NULL, // No
SECURITY_ATTRIBUTES structure
OPEN_EXISTING,// No special
create flags
0, // No special
attributes
NULL); // No template
file
if (INVALID_HANDLE_VALUE == hOut)
{
printf("FAILED to open \n");
}
SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
free(functionClassDeviceData);
return hOut;
}
HANDLE
open_file (
PCHAR filename
)
{
int success = 1;
HANDLE h;
char tempBuff[256] = "";
LPCWSTR p;
USBDeviceOpen((LPGUID) &GUID_CLASS_I82930_BULK);
//strcpy(tempBuff, (CHAR)functionClassDeviceData->DevicePath);
strcpy(tempBuff,myPointer);
printf("Attempting to open %s\n", tempBuff);
strcat(tempBuff, "\\");
if ((strlen(DeviceName) + strlen(filename)) > 255)
{
printf("Failed to open handle - possibly long filename
\n");
return INVALID_HANDLE_VALUE;
}
strcat(tempBuff, filename);
printf("tempBuff = (%s)\n", tempBuff);
//p=(LPCWSTR)tempBuff;
h = CreateFile((LPCWSTR)tempBuff,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL);
if (h == INVALID_HANDLE_VALUE)
{
printf("Failed to open %d\n",GetLastError());
success = 0;
}
else
{
printf("Opened successfully. \n");
}
return h;
}
HANDLE
UsbWrite (VOID)
{
PCHAR poutBuf = NULL;
PCHAR pinBuf = NULL;
HANDLE hRead = INVALID_HANDLE_VALUE;
HANDLE hWrite = INVALID_HANDLE_VALUE;
DWORD nBytesRead = 0;
DWORD nBytesWrite = 0;
ULONG success;
poutBuf = (PCHAR)malloc(32 * sizeof(UCHAR));
pinBuf = (PCHAR)malloc(32 * sizeof(UCHAR));
if((NULL != poutBuf) &&(NULL != pinBuf)) {
hWrite = open_file(outPipe);
//hRead = open_file(inPipe);
strcpy(poutBuf, "1234abcd");
success = WriteFile(hWrite,
poutBuf,
(32 * sizeof(UCHAR)),
&nBytesWrite,
NULL);
if (success)
{
printf("Write Succcess\n");
}
else {
printf("WriteFile failed\n");
}
}
else {
printf("memory allocation for poutBuf and PinBuf is
failed\n");
free(poutBuf);
free(pinBuf);
return 0;
}
CloseHandle(hWrite);
CloseHandle(hRead);
free(poutBuf);
free(pinBuf);
return 0;
}
I'm developing a sample USB application on the host side, but it is
failing to open the pipe and write; It is getting the handle and
device path everything properly. I verified with the rwbulk.exe which
is provided by the microsoft as sample with the Win DDK. Even that is
getting the same device path and the pipe name and opening it nicely,
but my application is returning error code 2(ERROR_FILE_NOT_FOUND) in
the createfile for the pipe.
Can anyone help me in identifying why it is failing?
I'm using the LoopBack DSF sample as my device. and that is configured
to use bulkusb.sys.
#include "UsbDev.h"
#include <conio.h>
#include "setupapi.h"
#include "bulkusr.h"
#include "usbio.cpp"
#include <tchar.h>
char myPointer[200]={0};
HANDLE USBDeviceOpen(LPGUID );
HANDLE open_file (PCHAR);
HANDLE UsbRead ();
HANDLE UsbWrite ();
int ResetPipe(HANDLE);
void test(PUCHAR, ULONG *);
char inPipe[32] = "PIPE00";
char outPipe[32] = "PIPE01";
char DeviceName[256] = "";
int main()
{
HANDLE hOut = INVALID_HANDLE_VALUE;
INT getInput;
CHAR Exit = 0;
#if 1
hOut = USBDeviceOpen((LPGUID) &GUID_CLASS_I82930_BULK);
if(hOut) {
printf("Device Opened SuccessFully\n");
}
else {
printf("Calling Exit.........\n");
exit(0);
}
#endif
do {
printf("\n0. Exit \n");
printf("1.Write \n ");
printf("\nSelect the operation you want to perform:
");
scanf("%d", &getInput);
switch(getInput) {
case 0:
Exit = 1;
break;
case 1:
printf("\nwrite has not implemented\n");
UsbWrite();
break;
default:
printf("\nInvalid input\n");
break;
}
}while(Exit != 1);
CloseHandle(hOut);
return 0;
}
HANDLE USBDeviceOpen(LPGUID pguid)
{
HDEVINFO
hardwareDeviceInfo;
PSP_DEVICE_INTERFACE_DETAIL_DATA
functionClassDeviceData = NULL;
SP_DEVICE_INTERFACE_DATA deviceInfoData;
ULONG
predictedLength = 0;
ULONG
requiredLength = 0;
HANDLE
hOut =INVALID_HANDLE_VALUE;
CHAR
*devName = NULL;
ULONG
ErrCode = 0;
//LPCTSTR dev;
hardwareDeviceInfo = SetupDiGetClassDevs(pguid,
NULL, // Define no
enumerator (global)
NULL, // Define no
(DIGCF_PRESENT | // Only
Devices present
DIGCF_DEVICEINTERFACE)); // Function class devices.
if (hardwareDeviceInfo == INVALID_HANDLE_VALUE)
{
printf("Invalid handle from SetupDiGetClassDevs \n");
// return INVALID_HANDLE_VALUE;
}
deviceInfoData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);
if (!SetupDiEnumDeviceInterfaces(hardwareDeviceInfo,
NULL, // We don't care about
specific PDOs
pguid,
0,
&deviceInfoData))
{
// printf("SetupDiEnumDeviceInterfaces Failed \n");
printf("Device not found %d\n",GetLastError());
getch();
return 0;
}
if (SetupDiGetDeviceInterfaceDetail(hardwareDeviceInfo,
&deviceInfoData,
NULL, // probing so no
output buffer yet
0, // probing so output
buffer length of zero
&requiredLength,
NULL)) // not interested in
the specific dev-node
{
printf("SetupDiGetDeviceInterfaceDetail Failed \n");
// return INVALID_HANDLE_VALUE;
}
if (ERROR_INSUFFICIENT_BUFFER != GetLastError())
{
printf("ERROR_INSUFFICIENT_BUFFER \n");
// return INVALID_HANDLE_VALUE;
}
predictedLength = requiredLength;
// sizeof (SP_FNCLASS_DEVICE_DATA) + 512;
functionClassDeviceData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc
(predictedLength);
if (NULL == functionClassDeviceData)
{
printf("Allocation Failed \n");
// return INVALID_HANDLE_VALUE;
}
functionClassDeviceData->cbSize = sizeof
(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(hardwareDeviceInfo,
&deviceInfoData,
functionClassDeviceData,
predictedLength,
&requiredLength,
NULL))
{
free(functionClassDeviceData);
printf("SetupDiGetDeviceInterfaceDetail Failed \n");
// return INVALID_HANDLE_VALUE;
}
wcstombs(myPointer, functionClassDeviceData->DevicePath,
wcslen
( functionClassDeviceData->DevicePath)+1);
printf("Attempting to open %s\n", myPointer);
hOut = CreateFile(functionClassDeviceData->DevicePath,
GENERIC_READ |
GENERIC_WRITE,
FILE_SHARE_READ |
FILE_SHARE_WRITE,
NULL, // No
SECURITY_ATTRIBUTES structure
OPEN_EXISTING,// No special
create flags
0, // No special
attributes
NULL); // No template
file
if (INVALID_HANDLE_VALUE == hOut)
{
printf("FAILED to open \n");
}
SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
free(functionClassDeviceData);
return hOut;
}
HANDLE
open_file (
PCHAR filename
)
{
int success = 1;
HANDLE h;
char tempBuff[256] = "";
LPCWSTR p;
USBDeviceOpen((LPGUID) &GUID_CLASS_I82930_BULK);
//strcpy(tempBuff, (CHAR)functionClassDeviceData->DevicePath);
strcpy(tempBuff,myPointer);
printf("Attempting to open %s\n", tempBuff);
strcat(tempBuff, "\\");
if ((strlen(DeviceName) + strlen(filename)) > 255)
{
printf("Failed to open handle - possibly long filename
\n");
return INVALID_HANDLE_VALUE;
}
strcat(tempBuff, filename);
printf("tempBuff = (%s)\n", tempBuff);
//p=(LPCWSTR)tempBuff;
h = CreateFile((LPCWSTR)tempBuff,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL);
if (h == INVALID_HANDLE_VALUE)
{
printf("Failed to open %d\n",GetLastError());
success = 0;
}
else
{
printf("Opened successfully. \n");
}
return h;
}
HANDLE
UsbWrite (VOID)
{
PCHAR poutBuf = NULL;
PCHAR pinBuf = NULL;
HANDLE hRead = INVALID_HANDLE_VALUE;
HANDLE hWrite = INVALID_HANDLE_VALUE;
DWORD nBytesRead = 0;
DWORD nBytesWrite = 0;
ULONG success;
poutBuf = (PCHAR)malloc(32 * sizeof(UCHAR));
pinBuf = (PCHAR)malloc(32 * sizeof(UCHAR));
if((NULL != poutBuf) &&(NULL != pinBuf)) {
hWrite = open_file(outPipe);
//hRead = open_file(inPipe);
strcpy(poutBuf, "1234abcd");
success = WriteFile(hWrite,
poutBuf,
(32 * sizeof(UCHAR)),
&nBytesWrite,
NULL);
if (success)
{
printf("Write Succcess\n");
}
else {
printf("WriteFile failed\n");
}
}
else {
printf("memory allocation for poutBuf and PinBuf is
failed\n");
free(poutBuf);
free(pinBuf);
return 0;
}
CloseHandle(hWrite);
CloseHandle(hRead);
free(poutBuf);
free(pinBuf);
return 0;
}