none
unable to write file in WinCE RRS feed

  • Question

  •  my code below can not write to file.  the writefile function keep returning false.  Please let me know what is wrong with my code  

       DEVDETAIL *pd = (DEVDETAIL*)pPNPBuf;
        HANDLE hDriver;

       GUID InterfaceClassGuid = DEVCLASS_KEYBOARD_GUID;

      DWORD dwBytesRead, dwBytesWritten, dwPos;
      char buff[4096];

        DEVMGR_DEVICE_INFORMATION DeviceInfo;

        DeviceInfo.dwSize = sizeof(DEVMGR_DEVICE_INFORMATION);
        hDriver = FindFirstDevice(DeviceSearchByGuid, &InterfaceClassGuid, &DeviceInfo);

    if (pd->fAttached==TRUE)
         {

              hkbd= CreateFile(DeviceInfo.szDeviceName,
                    GENERIC_WRITE|GENERIC_READ,      
                           0,                     
                           NULL,                 
                           OPEN_EXISTING,          
                           FILE_FLAG_OVERLAPPED,
                           0);         

              if ( hkbd == INVALID_HANDLE_VALUE)
              {
                 printf("Terminal Failure:unable to open file\n");
              }
              else
              {
                bool bErr=WriteFile (hkbd, buff, dwBytesRead, &dwBytesWritten, NULL);

              
         if (FALSE == bErr)
        {  
            printf("Unable to write to file.\n");
        }

              }

    }

           

                  
    Friday, December 28, 2012 4:45 AM

Answers

  • Using "GetLastError()" printf the error value to help debugging easier.

    Eg .

    printf("Unable to write to file ERROR code returned = %d \r\n",GetLastError());

    --- Misbah


    Senior Design Engineer T.E.S Electroni Solutions (Bangalore-India) www.tes-dst.com email-misbah.khan@tes-dst.com

    • Proposed as answer by Misbah Khan Friday, December 28, 2012 2:31 PM
    • Marked as answer by Liang1 Sunday, December 30, 2012 10:17 PM
    Friday, December 28, 2012 5:09 AM
  • It can be that the driver prevents writefile operation, writefile is not implemented or writefile is not exported in the driver.

    Quick test on WEC7 VPC image tells me that COM2 succeeds but with WAV1 I got the same error code. When I look into the wav driver I can see that the WriteFile is not implemented.

    I used this test code:

    int _tmain(int argc, _TCHAR* argv[])
    {
    	HANDLE hDriver = NULL;
    
    
    	hDriver = CreateFile(L"COM2:", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
    	
    	if(hDriver != INVALID_HANDLE_VALUE)
    	{
    		
    		char buff[4096];
    		DWORD dwBytesWritten = 0;
    		DWORD dwBytesToWrite = sizeof(buff);
    
    		BOOL bRet = WriteFile(hDriver, buff, dwBytesToWrite, &dwBytesWritten, NULL);
    		if(bRet)
    		{
    			RETAILMSG(1,(TEXT("%d bytes written successfully\r\n"), dwBytesWritten));
    		}
    		else
    		{
    			RETAILMSG(1,(TEXT("Failed to write file with %d\r\n"), GetLastError()));
    		}
    	}
    	else
    	{
    		RETAILMSG(1,(TEXT("Failed to create file with %d\r\n"), GetLastError()));
    	}
    	return 0;
    }

    Also: you pass 0 bytes to write in your writefile?

    WriteFile (hkbd, buff, dwBytesRead, &dwBytesWritten, NULL); Where dwBytesRead in not initialized.. 

    Also if I look into http://msdn.microsoft.com/en-us/library/ee490417.aspx

    it tells me that FILE_FLAG_OVERLAPPED is not supported; This seems strange cause WEC7 should support overlapped IO...


    -- Good luck, Erwin Zwart--- Check out my blog: http://GuruCE.com/blog https://twitter.com/GuruCE_Embedded GuruCE Microsoft Embedded Partner http://GuruCE.com Consultancy, training and development services.

    • Proposed as answer by Erwin Zwart Saturday, December 29, 2012 2:05 PM
    • Marked as answer by Liang1 Sunday, December 30, 2012 10:17 PM
    Friday, December 28, 2012 3:53 PM

All replies

  • Using "GetLastError()" printf the error value to help debugging easier.

    Eg .

    printf("Unable to write to file ERROR code returned = %d \r\n",GetLastError());

    --- Misbah


    Senior Design Engineer T.E.S Electroni Solutions (Bangalore-India) www.tes-dst.com email-misbah.khan@tes-dst.com

    • Proposed as answer by Misbah Khan Friday, December 28, 2012 2:31 PM
    • Marked as answer by Liang1 Sunday, December 30, 2012 10:17 PM
    Friday, December 28, 2012 5:09 AM
  • Thank you for the reply.  It just returned Error code 50.  I am confused I thought writeFile is part of WINCE package

    50 (0x32)

    The request is not supported.

    Friday, December 28, 2012 2:01 PM
  • It can be that the driver prevents writefile operation, writefile is not implemented or writefile is not exported in the driver.

    Quick test on WEC7 VPC image tells me that COM2 succeeds but with WAV1 I got the same error code. When I look into the wav driver I can see that the WriteFile is not implemented.

    I used this test code:

    int _tmain(int argc, _TCHAR* argv[])
    {
    	HANDLE hDriver = NULL;
    
    
    	hDriver = CreateFile(L"COM2:", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
    	
    	if(hDriver != INVALID_HANDLE_VALUE)
    	{
    		
    		char buff[4096];
    		DWORD dwBytesWritten = 0;
    		DWORD dwBytesToWrite = sizeof(buff);
    
    		BOOL bRet = WriteFile(hDriver, buff, dwBytesToWrite, &dwBytesWritten, NULL);
    		if(bRet)
    		{
    			RETAILMSG(1,(TEXT("%d bytes written successfully\r\n"), dwBytesWritten));
    		}
    		else
    		{
    			RETAILMSG(1,(TEXT("Failed to write file with %d\r\n"), GetLastError()));
    		}
    	}
    	else
    	{
    		RETAILMSG(1,(TEXT("Failed to create file with %d\r\n"), GetLastError()));
    	}
    	return 0;
    }

    Also: you pass 0 bytes to write in your writefile?

    WriteFile (hkbd, buff, dwBytesRead, &dwBytesWritten, NULL); Where dwBytesRead in not initialized.. 

    Also if I look into http://msdn.microsoft.com/en-us/library/ee490417.aspx

    it tells me that FILE_FLAG_OVERLAPPED is not supported; This seems strange cause WEC7 should support overlapped IO...


    -- Good luck, Erwin Zwart--- Check out my blog: http://GuruCE.com/blog https://twitter.com/GuruCE_Embedded GuruCE Microsoft Embedded Partner http://GuruCE.com Consultancy, training and development services.

    • Proposed as answer by Erwin Zwart Saturday, December 29, 2012 2:05 PM
    • Marked as answer by Liang1 Sunday, December 30, 2012 10:17 PM
    Friday, December 28, 2012 3:53 PM
  • Hi Erwin,

    Thank you for your help! I have tried the example that you provided above, the writeFile works perfectly.  Probably my kbdhid driver does not support WriteFile.   could you tell if there is any alternative way to sent the buffer down to the usb keyboard device.

    Friday, December 28, 2012 6:47 PM
  • Not that I know of, but it also depends on the implementation of the keyboard driver I guess. 

    -- Good luck, Erwin Zwart--- Check out my blog: http://GuruCE.com/blog https://twitter.com/GuruCE_Embedded GuruCE Microsoft Embedded Partner http://GuruCE.com Consultancy, training and development services.

    • Proposed as answer by Erwin Zwart Saturday, December 29, 2012 2:05 PM
    Saturday, December 29, 2012 2:05 PM
  • Hi Erwin,

    I have two additional questions to ask you.   I am very new to hid driver,   I would like to know the kind of USB driver I should use to write packet from mother board to USB hid keyboard device, is a control driver?  from USB hid keyboard device to mother board,  do I use kbdhid client driver?

    thank you!

    Saturday, December 29, 2012 7:23 PM
  • I would suggest to mark one of the answers we gave to you as the answer if it helped. For the additional 2 questions create a new post here so that everybody can help. Make sure you explain what you are trying to do.

    Is your device actual an keyboard which you are trying to control or do you just want to send some packages over USB? Do you want to 'control' the keyboard?



    -- Good luck, Erwin Zwart--- Check out my blog: http://GuruCE.com/blog https://twitter.com/GuruCE_Embedded GuruCE Microsoft Embedded Partner http://GuruCE.com Consultancy, training and development services.

    Sunday, December 30, 2012 9:49 AM
  •  I will open a new question.  The objective of my project is to use the function WriteFile sends the packet to the keyboard device.  The packet contains the commands that tell the USB HID Keyboard LEDs to turn on or off.   I need to know what type driver do I need control driver or khdhid driver. 
    Sunday, December 30, 2012 10:42 PM