• COM 向VBS 脚本传递数组


    在做ocx 控件的时候,函数需要返回一个字符串类型的数组,代码如下:

    VARIANT CreadDllCtrl::GetarcData(void)
    {
        AFX_MANAGE_STATE(AfxGetStaticModuleState());
    
        VARIANT vaResult;
        VariantInit(&vaResult);
        SAFEARRAY * psa;
    
        SAFEARRAYBOUND rgsabound[1];   
        CComVariant cv;
        CComBSTR bstrTmp;
    
        long nIndex;
    
    
    
        rgsabound[0].lLbound = 0;
    
        rgsabound[0].cElements = dxfR.m_tarcData.size();
        //psa = SafeArrayCreate(VT_BSTR, 1, rgsabound);
    
        psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
    
        nIndex = 0;
    
        list<Tag_CADArc> ::iterator it;
        double pointx,pointy,pointz,r,stAngle,edAnge;
        CString strTmp;
        LONG nCount = 0;
        for( it= dxfR.m_tarcData.begin();it!=dxfR.m_tarcData.end();it++)
        {
             CString str;
            //cout<<*it<<"	";
            strTmp.Empty();
            pointx = (*it).tCen.x; 
            pointy = (*it).tCen.y;
            pointz = (*it).tCen.z;
            r = (*it).radius;
            stAngle = (*it).stAngle;
            edAnge = (*it).edAngle;
    
            str.Format(_T("%lf,"),pointx);
            strTmp = str;
            str.Format(_T("%lf,"),pointy);
            strTmp = strTmp + str;
            str.Format(_T("%lf,"),pointz);
            strTmp = strTmp + str;
            str.Format(_T("%lf,"),r);
            strTmp = strTmp + str;
    
            str.Format(_T("%lf,"),stAngle);
            strTmp = strTmp + str;
    
            str.Format(_T("%lf"),edAnge);
            strTmp = strTmp + str;
    
    
            bstrTmp = strTmp.AllocSysString();
            cv = bstrTmp;
    
            //SafeArrayPutElement(psa, &nCount,bstrTmp);
            SafeArrayPutElement(psa, &nCount,&cv);
    
            nCount++;
        }
    
           //vaResult.vt = VT_ARRAY|VT_BSTR;
    
        vaResult.vt = VT_ARRAY|VT_VARIANT;
    
        vaResult.parray = psa;
    
        // TODO: Add your dispatch handler code here
    
        return vaResult;
    }

    本来是用的上面标红的字体的代码,意思就是创建一个VARIANT 类型,这个类型是个BSTR类型的数组;但是在VBS 里面调用该函数的时候,使用变量接收返回的数组,总是

    报,类型不匹配的错误。

    最后发现,要创建的VARINAT 的数组类型应该是VT_ARRAY|VT_VARIANT ,也就是把VT_BSTR 换成VT_VARIANT ,再运行就正确了;

    参考:

    https://blog.csdn.net/Python/article/details/2568849?utm_medium=distribute.pc_relevant_bbs_down.none-task-blog-baidujs-2.nonecase&depth_1-utm_source=distribute.pc_relevant_bbs_down.none-task-blog-baidujs-2.nonecase

    http://www.coder100.com/index/index/content/id/317915

  • 相关阅读:
    【ST开发板评测】Nucleo-F411RE开箱报告
    手把手教你制作Jlink-OB调试器(含原理图、PCB、外壳、固件)
    国产处理器的逆袭机会——RISC-V
    基于uFUN开发板和扩展板的联网校准时钟
    基于uFUN开发板的RGB调色板
    理解ffmpeg中的pts,dts,time_base
    如何终止线程的运行(C/C++)
    关于阻塞和非阻塞,同步和异步的总结
    QT移植无法启动 This application failed to start because it could not find or load the QT platform
    Qt5学习记录:QString与int值互相转换
  • 原文地址:https://www.cnblogs.com/small-lazybee/p/13926962.html
Copyright © 2020-2023  润新知