Qt with VC + + to carry out the verification mechanism of the plug-in (traverse vtable, to ensure that the number of virtual functions consistent, you can also use the Q_invokable macro definition)

Source: Internet
Author: User

Due to the recent company to develop a C + + plug-in mechanism-based, there is a major problem is C + + binary compatibility issues.
Once the class uses the virtual function, as long as the random changes and additions and deletions to check the virtual function of the head file, it will cause the program to jump when running, because this time EXE and DLL inside the vtable model is inconsistent

Just the program is developed using QT, so there are two ways to ensure that the header file is consistent in order to load the program

1. Using the MOC mechanism of QT
The MOC mechanism of QT has a Q_invokable macro definition that allows MOC to generate information about the class member functions,
Then use the method inside the qobject to get the corresponding function parameter and function order.
Used to guarantee the order of virtual functions, parameters are consistent
See the Qobject documentation for specific information
Http://doc.qt.io/qt-5/qobject.html
Http://doc.qt.io/qt-5/qmetaobject.html
Http://doc.qt.io/qt-5/qmetamethod.html
Get the specified function information from the Qobject or MetaObject and get the Metamethod

2. Traverse the vtable to ensure that the number of virtual functions is consistent. How to get vtable on the web there are many articles, the following is mainly for VS2015 compiler code

The following code is the main thing to do:
1. Ensure that the incoming program has a virtual function
2. Guaranteed t no pointer type
3. Get vtable
4. Traverse vtable has been encountering 0

template <classT>intvtableLength(T &object){    static_assert(std::is_polymorphic<T>::value,                  "at least has one virtual function");    static_assert(!std::is_pointer<T>::value,                  "type not allow pointer");    int* vptr = (int*)&object;    int* vtable = (int*)*vptr;    intlen = 0;    for(inti = 0; vtable[i] != 0; i++)    {        len++;    }    returnlen;}

Https://www.cnblogs.com/linyilong3/p/6750516.html

Qt with VC + + to carry out the verification mechanism of the plug-in (traverse vtable, to ensure that the number of virtual functions consistent, you can also use the Q_invokable macro definition)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.