English help on website



SafeArrayの返り値について(Ver9.24対応版)
目次COMの呼び出し− SafeArrayの返り値について
COMオブジェクトが返す値として、いわゆるSafeArrayという種類の値で、配列をまるごと1つの返り値として返すことがあります。
例えば"Scripting.Dictionary"オブジェクトの"Keys"や"Items"がそれにあたります。
秀丸マクロではSafeArrayをそのまま表現することができないため、SafeArrayを受け取ったときは自動的にオブジェクトに変換しています。(V8.98以降)
オブジェクトは、"Count"で要素数、"Item"で要素の内容を取得できるオブジェクトになっています。

サンプル
    newfile;
    
    #objDic = createobject("Scripting.Dictionary");
    callmethod #objDic, "add", "Key1", "Value1";
    callmethod #objDic, "add", "Key2", 2;
    callmethod #objDic, "add", 3, "Value3";
    
    #objKeys = member(#objDic, "Keys"); //ここでオブジェクトに変換される
    #c = getpropnum(#objKeys, "Count"); //"Count"で要素数
    #i = 0;
    insert "Keys:"+str(#c)+"\n";
    while(#i<#c){
        $key = getpropstr(#objKeys, "Item", #i); //"Item"で要素
        if(getresultex(10)!=false){
            insert "[\""+$key+"\"]=";
            $a = getpropstr(#objDic, "Item", $key);
            if(getresultex(10)!=false){
                insert "\""+$a+"\"";
            } else {
                #a = getpropnum(#objDic, "Item", $key);
                if(getresultex(10)!=false){
                    insert str(#a);
                }
            }
            insert "\n";
            #i=#i+1;
            continue;
        }
        #key = getpropnum(#objKeys, "Item", #i); //"Item"で要素
        if(getresultex(10)!=false){
            insert "["+str(#key)+"]=";
            $a = getpropstr(#objDic, "Item", #key);
            if(getresultex(10)!=false){
                insert "\""+$a+"\"";
            } else {
                #a = getpropnum(#objDic, "Item", #key);
                if(getresultex(10)!=false){
                    insert str(#a);
                }
            }
            insert "\n";
            #i=#i+1;
            continue;
        }
        #i=#i+1;
    }
    endmacro;