how to read the values in safearray

Discussion in 'SolidWorks' started by syshieley, Jul 26, 2006.

  1. syshieley

    syshieley Guest

    Dear all

    i want to access every body in the opened part, there is an example in
    SolieWorks API for it and i was carrying on based on it.
    But a problem for me, after using pPartDoc->GetBodies2(swAllBodies,
    FALSE, &vBodies) to get the bodies data which is vBodies and in
    variant. i cannot read every body information from it. based on the
    example in API, i did almost in the same way:

    pPartDoc->GetBodies2(swAllBodies, FALSE, &vBodies);
    SAFEARRAY* psaBody = V_ARRAY(&vBodies);
    LPDISPATCH* pBodyDispArray = NULL;
    long nBodyHighIndex = -1;
    long nBodyCount = -1;

    hr = SafeArrayAccessData(psaBody, (void **) &pBodyDispArray);
    ASSERT(S_OK == hr);
    ASSERT(NULL != pBodyDispArray);
    hr = SafeArrayGetUBound(psaBody, 1, &nBodyHighIndex);
    ASSERT(S_OK == hr);
    nBodyCount = nBodyHighIndex + 1;
    for (i = 0; i < nBodyCount; i++)
    {
    LPBODY2 pSWBody;
    pSWBody = pBodyDispArray;
    ASSERT(pSWBody);
    hr = pSWBody->Select(VARIANT_TRUE, 0, &bRet);
    ASSERT(bRet);
    pSWBody->Release();
    }
    hr = SafeArrayUnaccessData(psaBody);
    hr = SafeArrayDestroy(psaBody);


    there is error duding compiling, it is said it is wrong to do pSWBody =
    pBodyDispArray;
    because error C2440: '=' : cannot convert from 'struct IDispatch *' to
    'struct IBody2 *'
    i am really confused, and cannot find a way to read each item in
    safearray and evaluate it to other object.

    thanks for anyhelp you provide....

    cheers
    SY
     
    syshieley, Jul 26, 2006
    #1
  2. syshieley

    Heikki Leivo Guest

    Have you tried _type casting_?

    For example

    pSWBody = (IBody2*) pBodyDispArray;


    -h-
     
    Heikki Leivo, Jul 26, 2006
    #2
  3. syshieley

    syshieley Guest

    Heikki Leivo 寫é“:

    great!!! it works, thanks so much
    it wasts me so long time, i would turn here for help earlier

    thanks so much
     
    syshieley, Jul 26, 2006
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.