|
#include <windows.h>
#include <stdio.h>
#undef GetFreeSpace
#import "C:\WINDOWS\System32\scrrun.dll"
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
int main(int argc, char* argv[])
{
CoInitialize(NULL);
Scripting::IDictionaryPtr pDic;
try {
TESTHR(pDic.CreateInstance("Scripting.Dictionary"));
_variant_t Key,Value;
// 追加
Key = "A";
Value = "日本語";
pDic->Add( &Key, &Value );
// 追加
Key = "B";
Value = "表示";
pDic->Add( &Key, &Value );
// 追加
Key = "XXX";
Value = "面倒なので、文字列のみ";
pDic->Add( &Key, &Value );
// 置き換え
Key = "B";
Value = "【表示】";
pDic->PutItem( &Key, &Value );
_bstr_t str;
printf( "要素数 : %d\n", pDic->GetCount() );
// Key による参照
Key = "A";
str = pDic->GetItem( &Key );
printf( "GetItem で直接表示 : %s\n", (LPTSTR)str );
_variant_t ar;
ar = pDic->Keys();
SAFEARRAY *psa;
long lLBound, lUBound, cElements;
psa = ar.parray;
// 配列の下位および上位の境界を取得
SafeArrayGetLBound( psa, 1, &lLBound );
SafeArrayGetUBound( psa, 1, &lUBound );
// 要素数を計算
cElements = lUBound - lLBound + 1;
// 列挙
printf( "----------------------\n");
for ( long cCnt = lLBound; cCnt <= lUBound; cCnt++ ) {
VARIANT vVal;
SafeArrayGetElement( psa, &cCnt, &vVal );
Key = vVal;
str = pDic->GetItem( &Key );
printf( "%s\n", (LPTSTR)str );
}
}
catch (_com_error &e)
{
}
pDic.Release();
CoUninitialize();
return 0;
}
| |