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.