Results 1 to 3 of 3

Thread: USB:Pipe not Opening!

  1. #1
    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; 
    }

  2. #2
    Join Date
    Oct 2005
    Posts
    75

    Re: USB:Pipe not Opening!

    Thread Moved.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: USB:Pipe not Opening!

    For :
    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:

    HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\CurrentVersion\
    NetworkCards\<number>

    Registry Hive B:

    HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum\Root\
    Legacy_<adapter name>

    Registry Hive C:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\
    <adapter name>
    Registry Hive D:

    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 .

Similar Threads

  1. Replies: 1
    Last Post: 11-11-2011, 04:46 AM
  2. Hybrid Silent Pipe alternative for Gigabyte GA-X58A-UD
    By Vrushabha in forum Hardware Peripherals
    Replies: 6
    Last Post: 09-08-2011, 10:22 PM
  3. dsquery pipe to dsmove
    By HenrikK in forum Active Directory
    Replies: 4
    Last Post: 23-02-2010, 11:06 PM
  4. How to use pipe between programs in C?
    By Harpreet Gaur in forum Software Development
    Replies: 5
    Last Post: 08-01-2010, 10:13 AM
  5. [Named Pipe] Redirection of STDIN and STDOUT
    By Shanbaag in forum Software Development
    Replies: 2
    Last Post: 24-04-2009, 03:53 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,111,035.99007 seconds with 17 queries