Results 1 to 9 of 9

Thread: Using Qlibrary on a function that uses arguments

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Using Qlibrary on a function that uses arguments

    Does someone has any experience or has any sample code on how using Qlibrary on a function that has arguments ?

    The DLL source code is showing: (Note: I have the DLL only in a compiled form - so I cannot change it prototypes)

    short int __stdcall About(unsigned char Device, unsigned char *Version,unsigned char *Manufact) {
    ...
    }
    So I've tried to do something like :

    Qt Code:
    1. typedef short int (*About)(unsigned char, unsigned char *, unsigned char *);
    2.  
    3. unsigned char m_version[10];
    4. unsigned char m_manufacturer[10];
    5.  
    6. Qlibrary m_dll("MyLibrary");
    7. About about = (About) m_dll.resolve("About");
    8. if (about) {
    9. about(0, m_version, m_manufacturer);
    10. }
    To copy to clipboard, switch view to plain text mode 

    But the content in m_version and in m_manufacturer is not as expected.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using Qlibrary on a function that uses arguments

    Maybe the function works differently from what you expect from it?

  3. #3
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using Qlibrary on a function that uses arguments

    Well, I found out that some code executed after the call of the DLL function behaves differently than if the same code is executed before the DLL function call.

    If I do something like:

    Qt Code:
    1. ...
    2. GetInfo getInfo = (GetInfo) m_dll->resolve("GetInfo");
    3. if (getInfo) {
    4. QString str;
    5. str = QString("%1 %2 %3 %4 %5 %6").arg(1).arg("aa").arg(3).arg("bb").arg(5).arg(6);
    6. m_ui->setSoftwareVersion(str);
    7. getInfo(m_interfaceNum, 0, tmp_version, tmp_feature, tmp_manufacturer,tmp_controller);
    8. ...
    To copy to clipboard, switch view to plain text mode 

    it will work...
    ... but if I do something like:

    Qt Code:
    1. ...
    2. GetInfo getInfo = (GetInfo) m_dll->resolve("GetInfo");
    3. if (getInfo) {
    4. QString str;
    5. getInfo(m_interfaceNum, 0, tmp_version, tmp_feature, tmp_manufacturer,tmp_controller);
    6. str = QString("%1 %2 %3 %4 %5 %6").arg(1).arg("aa").arg(3).arg("bb").arg(5).arg(6);
    7. m_ui->setSoftwareVersion(str);
    8. ...
    To copy to clipboard, switch view to plain text mode 

    it wont work. (str is filled differently and the line m->ui... brings QT to crash)
    If you observe how str is filled in the second case, you will note that the last argument (6) is not filled in correctly. Also in the second case using a fix string in the line m_ui->setSoftwareVersion("my fixed string"); will also make the app to crash, while it does not if the same line is executed before the DLL function call.

    I have attached a small project to demonstrate this issue.
    The mentionned code is located in can_zcan4usbfx.cpp.
    Please note that the DLL provided in my project can be dowloaded directly at http://www.zanthic.com/can4usbfx.htm

    Do you have an idea what this could be ?
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using Qlibrary on a function that uses arguments

    Are you sure that tmp_feature, tmp_manufacturer and tmp_controller have correct sizes? Maybe GetInfo returns Unicode strings?

  5. #5
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using Qlibrary on a function that uses arguments

    Yes, I am sure that the size is correct, although I already made a test giving larger size to these variables, and GetInfo returns correct informations.

    Qt Code:
    1. // ****************************************************************************
    2. short int __stdcall GetInfo(unsigned char InterfaceNum,unsigned char DeviceNum,unsigned char *Version,unsigned char *Feature
    3. ,unsigned char *Manufact,unsigned char *CANCont)
    4. // Interface Number 0-9 for for than one interface connected (CAN-4-USB only)
    5. // DeviceNum is which CAN controller within an interface if more than one
    6. // Verson will get filled with two bytes for major and minor version
    7. // Feature is an 8 bit flag byte to show which features are availble
    8. // Manufact will be a string up to 20 characters of the board manufacturer name
    9. // CANCont is a byte array with the first character containing the number of CAN
    10. // controllers within this board. The next bytes are a value to show what type of
    11. // CAN controller this is
    12. {
    13. unsigned char USBOutBuf[4];
    14. unsigned char USBInBuf[65];
    15. short int Result,pntr=4;
    16. unsigned char c;
    17.  
    18. Result=OpenDriver(InterfaceNum);
    19. if (Result<=0) return (Result);
    20.  
    21. USBOutBuf[0] = DeviceNum;
    22. USBOutBuf[1] = GETSTATSREQ; // command
    23.  
    24. Result = USBRawCommand(InterfaceNum, USBOutBuf, 2, USBInBuf);
    25. if (Result<0) return(Result); // return error
    26. if (USBInBuf[0]==GETSTATSRESP)
    27. {
    28. *Version++=USBInBuf[1];
    29. *Version++=USBInBuf[2];
    30. *Feature=USBInBuf[3];
    31. while(USBInBuf[pntr]!=0)
    32. *Manufact++=USBInBuf[pntr++]; // load name till 0
    33.  
    34. *Manufact++=USBInBuf[pntr++]; // load 0
    35.  
    36. c=USBInBuf[pntr++]; // number of controllers
    37. *CANCont++=c; // save to buffer
    38. while (c--) // loop through the number of controllers
    39. *CANCont++=USBInBuf[pntr++]; // save their type to the buffer
    40.  
    41. return(ACK);
    42. }
    43. else
    44. return (ERRIMPROPERRESPONSE);
    45. }
    To copy to clipboard, switch view to plain text mode 

    After the DLL call Qt is all screwed up.
    I am not sure why this should influence the filling of str after the DLL call ?

    Anyone has an idea ?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using Qlibrary on a function that uses arguments

    Is the application multithreaded?

  7. #7
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using Qlibrary on a function that uses arguments

    Yes, the function in which the DLL is accessed runs within a thread.
    But I don't understand how this can affect how filling a simple QString can behave differently before and after the DLL function is called.
    I have attached a demo project, including the DLL, in a previous post, that can be used to reproduce the strange behavior I am observing, so that you can have a look if you want.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using Qlibrary on a function that uses arguments

    Maybe the function is not reentrant?

  9. #9
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using Qlibrary on a function that uses arguments

    I've got the answer from the Trolltech support.

    I negliged the windows calling convention (cf article at http://www.unixwiz.net/techtips/win32-callconv.html) with the consequence of corrupts memory all over the place.

    Finally I had to change my typedef from:

    Qt Code:
    1. typedef short int (*GetInfo)(unsigned char,unsigned char,unsigned char *,unsigned char *,unsigned char *,unsigned char *);
    To copy to clipboard, switch view to plain text mode 
    into:

    Qt Code:
    1. typedef short int (__stdcall *GetInfo)(unsigned char,unsigned char,unsigned char *,unsigned char *,unsigned char *,unsigned char *);
    To copy to clipboard, switch view to plain text mode 

    That solved everything.

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 14:22
  2. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  3. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52

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.