Hi,

I have to intercept a generated window message (Win32 struct type MSG) sent out of a Win32/MFC DLL. So I searched for intercepting native Events and found methods
QWidget::winEvent( MSG* msg, long* result) from QWidget and
QCoreApplication::winEventFilter( MSG* msg, long* result) from QCoreApplication.

I have to load the DLL dynamically with
Qt Code:
  1. LoadLibrary()
To copy to clipboard, switch view to plain text mode 
and get functions with
Qt Code:
  1. GetProcAddress()
To copy to clipboard, switch view to plain text mode 
so I do not certainly want the DLL to be loaded from QApplication derived class in my app. But the message generated is no window message either because it comes from hardware connected so the message could not fit in any QWidget derived sub-class either.

Because there seem no other possibilities I tried to load DLL from a derived QWidget class and override QWidget::winEvent(MSG* msg, long* result). So my principal parts are like this:

Engine.hpp
Qt Code:
  1. [...]
  2. #ifdef BUILD_ENGINE
  3. #define EXPORT_ENGINE Q_DECL_EXPORT
  4. #else
  5. #define EXPORT_ENGINE
  6. #endif
  7.  
  8. #define WM_TAG WM_USER+11111
  9.  
  10. #include <QtCore>
  11. #include <QWidget>
  12.  
  13. #include <windows.h>
  14. #include <winbase.h>
  15.  
  16. class EXPORT_ENGINE MyWidget : public QWidget
  17. {
  18. Q_OBJECT
  19. public:
  20. typedef bool (*StartScan)(void*, int*, int, int, int, char*);
  21. typedef bool (*SelectReader)(void*, int*, int*, int*, char*, int);
  22. typedef bool (*StopScan)(void*, int);
  23.  
  24. MyWidget( QWidget* parent = 0);
  25.  
  26. bool bind();
  27. bool open();
  28. bool close();
  29.  
  30. virtual bool winEvent( MSG* message, long* result );
  31.  
  32. // signals:
  33. // void tagDetected( const QString& tag );
  34.  
  35.  
  36. private:
  37. static bool StopScanDll( HWND hwParam, int id );
  38. static bool StarScanDll( HWND hwParam, int* id, int type, int com, int baud, QString name);
  39. static bool SelectReaderDll( HWND hwParam, int* id, int* com, int* baud, char* name);
  40. char* mReaderName;
  41. bool mStarted;
  42. QHash< int, int > mBaudRateTable;
  43.  
  44. static HINSTANCE sDll;
  45.  
  46. static int sReaderID;
  47. static int sReaderType;
  48. static int sReaderCom;
  49. static int sReaderBaud;
  50. static QString sReaderName;
  51. static bool sTagHandled;
  52. };
  53.  
  54. #endif // ENGINE_HPP
To copy to clipboard, switch view to plain text mode 

Engine.cpp
Qt Code:
  1. // MyWidget.cpp
  2.  
  3. #include <QtGui>
  4.  
  5. #include "MyWidget.hpp"
  6.  
  7.  
  8. HINSTANCE MyWidget::sDll = 0;
  9. int MyWidget::sReaderID = 0;
  10. int MyWidget::sReaderType = 0;
  11. int MyWidget::sReaderBaud = 0;
  12. int MyWidget::sReaderCom = 0;
  13. QString MyWidget::sReaderName = "";
  14. bool MyWidget::sTagHandled = false;
  15.  
  16. MyWidget::MyWidget( QWidget* parent ) : QWidget( parent )
  17. {
  18.  
  19. sDll = LoadLibrary( L"RFID.dll" );
  20.  
  21. mReaderName = new char[255];
  22. sTagHandled = true;
  23. int baudRateValues[] = {0, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 56000, 57600, 115200 };
  24. for (int i = 0; i < 11; i++ )
  25. {
  26. mBaudRateTable[i] = baudRateValues[i];
  27. }
  28. }
  29.  
  30. bool MyWidget::StopScanDll( HWND hwParent, int id )
  31. {
  32. [...]
  33.  
  34. MyWidget::StopScan FStopScanDll;
  35. FStopScanDll = NULL;
  36. FStopScanDll = (MyWidget::StopScan)GetProcAddress( sDll, "StopScan" );
  37. if ( FStopScanDll == NULL )
  38. return false;
  39. bool b = FStopScanDll( hwParent, id );
  40. return b;
  41. }
  42.  
  43. [...]
  44.  
  45. bool RfidWidget::bind()
  46. {
  47.  
  48. bool b = SelectDll( this->winId(), &sReaderType, &sReaderCom, &sReaderBaud, mReaderName );
  49. sReaderName = mReaderName;
  50. return b;
  51. }
  52.  
  53. [...]
  54.  
  55. bool MyWidget::winEvent( MSG* message, long* result )
  56. {
  57. UINT msg = message->message;
  58. WPARAM wp = message->wParam;
  59. LPARAM lp = message->lParam;
  60.  
  61. switch ( msg )
  62. {
  63. case WM_TAG:
  64. qDebug() << "WM_TAG";
  65.  
  66. emit tagDetected( (char*)lp );
  67. return true;
  68. break;
  69. default:
  70. break;
  71. }
  72.  
  73. return false;
  74. }
To copy to clipboard, switch view to plain text mode 

Problem is, if I define my sub-class with signals (
Qt Code:
  1. QOBJECT
To copy to clipboard, switch view to plain text mode 
macro and signal
Qt Code:
  1. tagDetected( const QString& )
To copy to clipboard, switch view to plain text mode 
I get linker errors from meta object compiling. I suppose this comes from interfering with native Win32 events?

Is there any other possibility to get native events like
Qt Code:
  1. WM_TAG
To copy to clipboard, switch view to plain text mode 
message above?

Thanks for any suggestions,
AlGaN