Search code examples
visual-c++pipedevice-driverioctl

ERROR_INVALID_HANDLE


I am trying to use the following functions defined in mpusbapi.h

HANDLE(*MPUSBOpen)(DWORD instance,     //Input
                   PCHAR pVID_PID,     //Input identifier
                   PCHAR pEP,          //Input pipe
                   DWORD dwDir,        //Input
                   DWORD dwReserved);  //Input <Future Use>

DWORD(*MPUSBWrite)(HANDLE handle,          //Input
                   PVOID pData,            //Input
                   DWORD dwLen,            //Input
                   PDWORD pLength,         //Output
                   DWORD dwMilliseconds);  //Input

When I use these in my test.cpp, it looks like;

HANDLE LACOutpipe;
pipeName="\\MCHP_EP";
PCHAR VidPid="vid_04d8&pid_fc5f";
BYTE bufData[3];
DWORD buflen=sizeof(bufData);
DWORD bufProcessed;

LACOutpipe=MPUSBOpen(0,        //only one device connected, dont need to check for multiple
                     VidPid,   //this is the device driver vid and pid
                     pipeName, //the pipe to write to?
                     MP_WRITE, //MP_WRITE is just 1
                     0);       //not supported yet?
cout<<LACOutpipe<<endl;

//now use LACOutpipe handle to write

cout<<MPUSBWRITE(LACOutpipe,   //the handle to write to
               bufData,        //BYTE array with data to be sent
               bufLen,         //length of bufData
               &bufProcessed,  //bytes processed
               10000)          //10 second timeout
               <<endl;

cout<<GetLastError()<<endl;

The console output is:

FFFFFFFF
0
6
Press any key to continue...

MPUSBWrite returning 0 means that the write function failed. Error code 6 corresponds to ERROR_INVALID_HANDLE: The handle is invalid.

Anyone know why? I have a hunch it is the pipeName but not sure how to check/fix this.


Solution

  • O.K. figured this out.

    Incorrect declaration:

    PCHAR VidPid= "vid_04d8&pid_fc5f";
    DWORD pipeName="\\MCHP_EP1";
    

    Correct declaration

    char VidPid[]="vid_04d8&pid_fc5f";
    char pipename[]="\\MCHP_EP1";
    

    Note that for the Firgelli LAC Board, there is only endpoint 1 available for IN and OUT pipes.

    I hope this solves some of the frustration problems that arise for anyone out there that has spent as much time as me trying to work around their lean documentation and no Visual C++ examples for the Firgelli LAC Board.