This article is a problem encountered in the actual development process. I have received help from my colleagues and would like to thank you.
In the secondary development process of SolidWorks, the asynchronous mode is used, and the following code has a problem in use (where the problem may occur, I have watched it)
BOOL CSWFun: GetProfileLength (CComPtr <ILoop2> pLoop, double & dLength) {long edgeCount = 0; pLoop-> GetEdgeCount (& edgeCount); if (edgeCount> 0) {double dLenthTemp = 0.0; VARIANT_BOOL isClosed = VARIANT_FALSE; incluisperiodic = VARIANT_FALSE; incluvbret = VARIANT_FALSE; IEdge ** pEdgeArr = NULL; pEdgeArr = new IEdge * [edgeCount]; zeroMemory (pEdgeArr, edgeCount * sizeof (IEdge *); pLoop-> IGetEdges (pEdgeArr); for (int I = 0; I <edgeCount; ++ I) {CComPtr <IEdge> pEdge = pEdgeArr [I]; // The possible pointer is NULL if (pEdge = NULL) {continue;} CComPtr <ICurve> pCurve; pEdge-> IGetCurve (& pCurve); if (NULL = pCurve) {continue;} double dStart = 0.0; double dEnd = 0.0; pCurve-> GetEndParams (& dStart, & dEnd, & isClosed, & isPeriodic, & vbRet ); // if (vbRet! = VARIANT_FALSE) {double dTemp = 0.0; pCurve-> getleng2( dStart, dEnd, & dTemp); if (dTemp> 0) {dLenthTemp + = dTemp ;}}} delete [] pEdgeArr; pEdgeArr = NULL; dLength = dLenthTemp;} return TRUE ;}
After the code is changed to the following, it will be normal
BOOL CSWFun::GetProfileLength(CComPtr<ILoop2>pLoop, double &dLength){long edgeCount = 0;pLoop->GetEdgeCount(&edgeCount);if (edgeCount > 0){double dLenthTemp = 0.0;VARIANT_BOOL isClosed = VARIANT_FALSE;VARIANT_BOOL isPeriodic = VARIANT_FALSE;VARIANT_BOOL vbRet = VARIANT_FALSE;CComVariant vEdgeArray;pLoop->GetEdges(&vEdgeArray);if (V_VT(&vEdgeArray) == VT_EMPTY){return FALSE;}SAFEARRAY *pSaveEdgeArray = V_ARRAY(&vEdgeArray);LPDISPATCH *pEdgeDispArray = NULL;SafeArrayAccessData(pSaveEdgeArray, (void**)&pEdgeDispArray);if (pEdgeDispArray == NULL){return FALSE;}for (int i=0; i<edgeCount; ++i){CComQIPtr<IEdge> pEdge = pEdgeDispArray[i];if (pEdge == NULL){continue;}CComPtr<ICurve> pCurve;pEdge->IGetCurve(&pCurve);if (NULL == pCurve){continue;}double dStart = 0.0;double dEnd = 0.0;pCurve->GetEndParams(&dStart, &dEnd, &isClosed, &isPeriodic, &vbRet);if (vbRet != VARIANT_FALSE){double dTemp = 0.0;pCurve->GetLength2(dStart, dEnd, &dTemp);if (dTemp > 0){dLenthTemp += dTemp;}}}dLength = dLenthTemp;}return TRUE;}
Invalid interface functions are mainly functions that get or set data starting with 'I. The corresponding value is IGetEdges. It is normal to change it to GetEdges. The COM interface is recommended for all add-in DLL projects. for executable (.exe) implementations, you can use the COM interface unless the method or property passes an array. in this case, use the equivalent
Dispatch method that uses a VARIANT to encapsulate the array. Therefore, during development, if it is in asynchronous mode, you need to obtain array-type data to avoid using functions starting with "I.