Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

CNiExcelCellRange::GetColumnWidth() crashed if CNiExcelApplication ExcelApp(false, CNiComInitialize::Multithreaded );

I have an application that written as client and server. The server is a regular DLL that is written in C++ in which the function "TestSpreadSheet()" has been exposed for client to use. The client is TestStand application in which there is an action to call function "TestSpreadSheet() in DLL".

 

Here is my function in DLL:

TestSpreadSheet()

{

CNiExcelApplication ExcelApp(false, CNiComInitialize::Multithreaded );  // CNiComInitialize::Apartment mode seems not working if called from TestStand

CNiExcelWorkbook WorkBook = ExcelApp.OpenWorkbook("Limit.xsl");

CNiExcelWorksheet WorkSheet = WorkBook.GetWorksheet("Sheet1");

CNiExcelCellRange Row1 = WorkSheet.GetRange("1:1");

Num = Row1.GetColumnWidth();  //Crash!!!

...

}

When the function is called by the action in TestStand the server crashed at “Row1.GetColumnWidth();”.

Measurement Studio has an example at C:\Program Files\National Instruments\MeasurementStudioVS2003\VCNET\Examples\Office\ExcellReport. The major difference between ExcellReport and my DLL code is that CNiExcellApplication is defined as mode of CNiComInitialize::Apartment and there is no problem using CNiExcelCellRange::GetColumnWidth() in this case. However it causes crash in if CNiExcellApplication is instanced as CNiComInitialize::Multithreaded mode.

 

0 Kudos
Message 1 of 4
(6,751 Views)
First of all, there's a discrepancy between your code saying what is working versus what is not working.  In your code snippit, you say that Multithreaded works, but in your explanation, you say Apartment works.  I'm assuming that it's Apartment that works.

The reason for this behavior is due to the nature of ActiveX.  ActiveX is best called within a single threaded apartment.  On the C++ side, it's best to initialize as Apartment.  Note that within TestStand, you can ensure that certain module calls are done within a single threaded apartment (making a sequence call and specifying Run in a New Thread with Advanced settings for STA).

Is there any reason why you do not simply use one or the other since it appears to be working?
Thanks,

Andy McRorie
NI R&D
0 Kudos
Message 2 of 4
(6,738 Views)

Andy,

Thank you for the reply.

 

Yes, in my function I instance the CNiExcelApplication as "Multithreaded". The reason is that this function will be called from TestStand and it doesn't work if I instance CNiExcelApplication as "Apartment".

 

TestStand doesn't provide convenient way to read .xls file so that I have a couple of functions in DLL to retrieve the data from .xls and pass the data back to TestStand.

 

In my DLL all CNiExcelApplication, CNiExcelWorkbook, CNiExcelWorksheet and CNiExcelCellRange member functions I used work fine in "Multithreaded" mode. Only one function causes crash which is "CNiExcelCellRange::GetColumnWidth()". This function works fine in "Apartment" mode.

 

 

Kathy

 

 

 

0 Kudos
Message 3 of 4
(6,736 Views)
Hey Kathy,

First of all, if you are trying to read properties (for instance, limits for steps) from an Excel file, TestStand does have a mechanism for that- the Property Loader.

If you are wanting something customized or that does not read specific properties, you are correct in that TestStand does not provide this capability as you would typically create a code module that performs the file I/O and return specific values to TestStand.

Continuing with the issue that you're running into, we need a bit more information.  To clarify, when you use Apartment, what functions do not work?  And what do you mean when you say they don't work?  How do they crash?  Does it only crash when called from TestStand?  Have you tried calling the DLL from another environment than TestStand?  Did you try putting the code module in a separate sequence and specifying Single Threaded Apartment?
Thanks,

Andy McRorie
NI R&D
0 Kudos
Message 4 of 4
(6,716 Views)