Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , ,

USB:Pipe not Opening!

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 26-05-2009
Member
 
Join Date: May 2009
Posts: 4
USB:Pipe not Opening!

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.

Code:
#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; 
}

Reply With Quote
  #2  
Old 26-05-2009
Rameez.J's Avatar
Super Moderator
 
Join Date: Oct 2005
Posts: 109
Re: USB:Pipe not Opening!

Thread Moved.
Reply With Quote
  #3  
Old 27-05-2009
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,118
Re: USB:Pipe not Opening!

For :
Quote:
Error 2 ERROR_FILE_NOT_FOUND
This problem may be caused by an invalid network adapter entry in the registry.

To remove this invalid network adapter registry entry, perform the following steps:

1. Run Registry Editor (Regedt32.exe) and go to the following keys in the registry: Registry Hive A:

Quote:
HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\Cu rrentVersion\
NetworkCards\<number>

Registry Hive B:

Quote:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum\R oot\
Legacy_<adapter name>

Registry Hive C:

Quote:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servic es\
<adapter name>
Registry Hive D:

Quote:
HKEY_LOCAL_MACHINE\SOFTWARE\<adapter name>

2. Select the registry key for the entry you wish to remove, and from the Edit menu, click Delete. This will remove the registry key.

Source : Microsoft , Hope this help you .
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads for: "USB:Pipe not Opening!"
Thread Thread Starter Forum Replies Last Post
dsquery pipe to dsmove dpav Active Directory 10 3 Weeks Ago 10:06 PM
How to use pipe between programs in C? Harpreet Gaur Software Development 5 08-01-2010 09:13 AM
[Named Pipe] Redirection of STDIN and STDOUT Shanbaag Software Development 2 24-04-2009 03:53 PM
Named Pipe lsass.exe Marbles Windows Security 7 28-05-2007 04:31 AM
MSI P35 Platinum - Circus Pipe dr.nil Motherboard Processor & RAM 0 04-05-2007 01:59 AM


All times are GMT +5.5. The time now is 04:19 AM.