QAxObject pointer is NULL under multiple threads. qaxobjectnull

Source: Internet
Author: User

QAxObject pointer is NULL under multiple threads. qaxobjectnull

You must perform operations on Excel in the project to write data into the Excel file. When the data volume is large, it is time-consuming to Operate Excel.

However, when the following code is executed, a null pointer will occur:

1 QAxObject * excel = new QAxObject ("Excel. application "); 2 excel-> setProperty (" Visible ", false); 3 4 QAxObject * workbooks = excel-> querySubObject (" WorkBooks ");

The workbooks pointer returned in row 4 is a null pointer and cannot be used for downward Excel operations. This is because the QAxObject object is used in a single thread by default,

To solve this problem, an initialization function is required, specifying that the QAxObject pointer can be used under multiple threads.

1 # include <ObjBase. h> 2 3 CoInitializeEx (NULL, COINIT_MULTITHREADED); 4 QAxObject * excel = new QAxObject ("Excel. application "); 5 excel-> setProperty (" Visible ", false); 6 7 QAxObject * workbooks = excel-> querySubObject (" WorkBooks ");

In this way, the above NULL pointer problem can be solved.

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.