Invalid parameter

This is a discussion on Invalid parameter within the Windows Programming forums, part of the Platform Specific Boards category; HI! Im trying to comunicating width my bluetooth usb divice. After register the GUID, (information on inf file), At this ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    Portugal
    Posts
    25

    Smile Invalid parameter

    HI!
    Im trying to comunicating width my bluetooth usb divice.
    After register the GUID, (information on inf file),
    At this time, i can receive in my winproc a DBT_DEVICEARRIVAL: message.
    In lParam the DBT_DEVTYP_DEVICEINTERFACE
    width devInterface->dbcc_classguid== my register GUID IVT_BLUETOOTH.
    Now, im trying to get all informatiom from device, and all works god,
    just at :

    DeviceHandle=CreateFile(detailData->DevicePath,0,FILE_SHARE_READ| FILE_SHARE_WRITE,
    &SecurityAttributes,OPEN_EXISTING,FILE_ATTRIBUTE_N ORMAL,NULL);
    DisplayLastError("CreateFile: ");
    Attributes.Size = sizeof(Attributes);

    after that, i have a error message 'Invalid parameter'.
    I think the invalid parameter is DeviceHandle, but i dont now why.


    HidD_GetAttributes(DeviceHandle,&Attributes);
    DisplayLastError("HidD_GetAttributes: ");
    sprintf(Buf,"0x%x",Attributes.VendorID);
    _editVendorID.SetText(Buf);
    sprintf(Buf,"0x%x",Attributes.ProductID);
    _editProductID.SetText(Buf);
    HidD_GetManufacturerString(DeviceHandle, Buffer, 126);
    DisplayLastError("HidD_GetManufacturerString: ");
    sprintf(Buf,"%ws",Buffer);
    _editManufacturer.SetText(Buf);
    HidD_GetProductString(DeviceHandle,Buffer,126);
    DisplayLastError("HidD_GetProductString: ");
    sprintf(Buf,"%ws",Buffer);
    _editName.SetText(Buf);

    Can you show me the way?
    thanks in advance.
    Last edited by joseCarlos; 11-17-2011 at 03:30 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    So test it... In fact you should always test the handle when opening files (and lots of other times too!)
    Code:
    fHandle = CreateFile(...
    
    if (fHandle == INVALID_HANDLE_VALUE)
      { messagebox(...
         // deal with error
       }
    It is very likely your file is not opening correctly...

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    Portugal
    Posts
    25
    But i get the HANDLER != INVALID_HANDLE_VALUE
    there is no error on CreateFile
    I have error on HidDGetAttribute(...) and HidD_GetManufacturerString
    HidD_GetProductString

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That's right... the error shows up later, the first time you try to actually access the opened file, because the file handle is invalid.

    CreateFile() returns an error, not a handle, if it fails to open the file (or device) then your code just carries on with DeviceHandle == INVALID_HANDLE_VALUE which causes an access violation when you try to actually use the handle in your later function calls.

    The thing is you don't know if there's an error or not... because you aren't testing the return value of CreateFile() before crashing along into your other code.
    Last edited by CommonTater; 11-17-2011 at 11:05 AM.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    Portugal
    Posts
    25
    I am testing the error and there is no error,
    that is my problem

    the same function work with no problem if i open my usb logitec mouse
    Code:
     ...           DeviceHandle=CreateFile(detailData->DevicePath,0,FILE_SHARE_READ	 | FILE_SHARE_WRITE,
                                        &SecurityAttributes,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
                   DisplayLastError("CreateFile: ");
    ...
    void CUsbhidiocDlg::DisplayLastError(const char * Operation)
    {
        //Display a message and the last error in the log List Box.
    
        LPVOID lpMsgBuf;
        USHORT Index = 0;
        std::string	strLastError = "";
        FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER |
            FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            GetLastError(),
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
            (LPTSTR) &lpMsgBuf,
            0,
            NULL
        );
    
        //Display the last error.
    
        strLastError = Operation ;
        strLastError+= (LPCTSTR)lpMsgBuf;
    
         //Trim CR/LF from the error message.
    
        strLastError[strLastError.size()-2]=0;
        _listResult.AddString(strLastError.c_str());
        _listResult.SelectPos(-1);
    //	ScrollToBottomOfListBox(Index);
        LocalFree(lpMsgBuf);
    }

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No you are not checking for errors.

    Yes, I see your DisplayLastError() function...

    The fact remains that you are NOT testing the returned value from CreateFile... you do not know if the handle is valid or not.

    This correctly tests the handle returned by CreateFile()...
    Code:
    fHandle = CreateFile(...
    
    if (fHandle == INVALID_HANDLE_VALUE)
      { messagebox(...
         // deal with error
       }
    This does not...
    Code:
    DeviceHandle=CreateFile(...
    DisplayLastError("CreateFile: ");
    An invalid file handle with a reported error of 0 can and will slip right past that.

    To make what you have work you need to do this...
    Code:
    DeviceHandle=CreateFile(...
    
    if (DeviceHandle == INVALID_HANDLE_VALUE)
      { 
         DisplayLastError("CreateFile: ");
         // fix the error or exit()
      }
    Read the MSDN specification for the return value of CreateFile()...
    Quote Originally Posted by MSDN
    If the function succeeds, the return value is an open handle to the specified file, device, named pipe, or mail slot.

    If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
    Last edited by CommonTater; 11-17-2011 at 11:34 AM.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    Portugal
    Posts
    25
    you're right, but GetLastError() does not report any error, but I'll try DeviceHandler. thanks

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by joseCarlos View Post
    you're right, but GetLastError() does not report any error, but I'll try DeviceHandler. thanks
    That's what I've been trying to tell you... it is possible to have an invalid file handle without any reported error.

    It is also possible that your error reporting mechanism might show an error message with a valid file handle.

    The only way to know for sure is to test the actual handle ... i.e. the return value of CreateFile()
    Last edited by CommonTater; 11-17-2011 at 11:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-22-2011, 02:44 PM
  2. Replies: 6
    Last Post: 01-08-2008, 09:25 AM
  3. Invalid name?
    By rabbit in forum C++ Programming
    Replies: 4
    Last Post: 03-01-2006, 07:02 PM
  4. Why is this invalid?
    By sand_man in forum C Programming
    Replies: 1
    Last Post: 10-20-2004, 10:51 PM
  5. Replies: 5
    Last Post: 10-14-2004, 09:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21