Results 1 to 2 of 2

Thread: Win32 MSG - winEvent()

  1. #1
    Join Date
    May 2006
    Location
    Stuttgart, Germany
    Posts
    22
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Cool Win32 MSG - winEvent()

    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

  2. #2
    Join Date
    May 2006
    Location
    Stuttgart, Germany
    Posts
    22
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Win32 MSG - winEvent()

    Hi,

    ok, managed to get rid of meta object linker errors by adding dependency of Qt GUI lib in project file. But one problem remains: Are there any possibilities to get win API messages without generating a widget?

    Thanks for any hints,
    AlGaN

Similar Threads

  1. win32 platform macros?
    By gfunk in forum Qt Programming
    Replies: 1
    Last Post: 5th September 2007, 20:02
  2. MSG not defined (winEvent)
    By December in forum Qt Programming
    Replies: 6
    Last Post: 19th February 2007, 16:24
  3. Win32 qmake: building a static library
    By Amanda in forum Qt Programming
    Replies: 5
    Last Post: 8th November 2006, 19:32
  4. kdelibs for win32...
    By CuCullin in forum KDE Forum
    Replies: 1
    Last Post: 3rd April 2006, 07:26
  5. QDialog in the taskbar (Win32)
    By Arthur in forum Qt Programming
    Replies: 4
    Last Post: 28th February 2006, 16:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.