Bug in WinUsbWrapper.c

Found a bug in EventGhost? Report it here.

Bug in WinUsbWrapper.c

Postby JohnieD » Sun Nov 20, 2011 3:27 pm

Hello,

Inspired by EG I am writing my own (small) piece of code in pure C/WinAPI for CyberLink Remote Controller and I have just found bug in EG's WinUsbWrapper.c.

Code: Select all
   while (TRUE)
   {
      WinUsb_ReadPipe(usbHandle, inPipeId, buffer, params->chunkSize, &length, &overlapped);
      if (WaitForMultipleObjects(2, handles, FALSE, INFINITE)!=WAIT_OBJECT_0) goto exit;
      if (!params->suppressRepeat || memcmp(buffer, oldBuffer, params->chunkSize))
         SendMessage(params->notifyWnd, params->msg, length, (LPARAM) buffer);
      tmpBuffer = oldBuffer;
      oldBuffer = buffer;
      buffer = tmpBuffer;
      ResetEvent(overlapped.hEvent);
   }

As you can see, the code performs overlapped read (WinUsb_ReadPipe()) and then sends pointer to the buffer and its length to "higher level". The problem is that the variable "length" sometimes contains wrong value. According to MSDN:
For an overlapped operation (and if LengthTransferred is a non-NULL value), the value received in LengthTransferred after WinUsb_ReadPipe returns is meaningless until the overlapped operation has completed. To retrieve the actual number of bytes read from the pipe, call WinUsb_GetOverlappedResult.
So in order to fix the problem call to WinUsb_GetOverlappedResult() must be added after WaitForMultipleObjects():
Code: Select all
   while (TRUE)
   {
      WinUsb_ReadPipe(usbHandle, inPipeId, buffer, params->chunkSize, &length, &overlapped);
      if (WaitForMultipleObjects(2, handles, FALSE, INFINITE)!=WAIT_OBJECT_0) goto exit;
      WinUsb_GetOverlappedResult(usbHandle, &overlapped, &length, FALSE);
      if (!params->suppressRepeat || memcmp(buffer, oldBuffer, params->chunkSize))
         SendMessage(params->notifyWnd, params->msg, length, (LPARAM) buffer);
      tmpBuffer = oldBuffer;
      oldBuffer = buffer;
      buffer = tmpBuffer;
      ResetEvent(overlapped.hEvent);
   }

Now "length" contains proper value.

However there is also problem in Python code (<EG ROOT DIR>\plugins\CyberlinkUniversalRemote\__init__.py). It seems that the author assumed that all RC codes have have fixed length (4 bytes) what seems not to be true. This causes strange EventGhost's behaviour, eg. DVDMenu key sometimes works and sometimes doesn't. It is easy to explain: WinUsb_GetOverlappedResult() returns 3 as the length but when one attempts to read 4 bytes from the buffer then 04h 00h 04h 00h or 04h 00h 04h 02h is read. I hope it helps. :D
JohnieD
 
Posts: 1
Joined: Sun Nov 20, 2011 2:54 pm

Re: Bug in WinUsbWrapper.c

Postby Pako » Sun Dec 18, 2011 10:35 am

Thank you for reporting and resolving the issue.
However, I do not know what can I do.
I do not understand your explanation, which relates to a bug in CyberlinkUniversalRemote plugin.
I do not know what I can do there.
I am not the owner CyberlinkUniversalRemote and therefore I can not try it.

Pako
User avatar
Pako
Plugin Developer
 
Posts: 1283
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic


Return to Bug Reports

Who is online

Users browsing this forum: No registered users and 2 guests