我正在使用COM库“Microsoft Scripting Runtime”(c:\ windows \ System32 \ scrrun.dll)中的Scripting.Dictionary。我正在使用import来获取我的包装器,这些都有效。添加一些项目后,我正在尝试获取密钥列表,但我被卡住了。
我有办法。我可以从词典中获取SAFEARRAY
个键,但我想使用CComSafeArray
中定义的<atlsafe.h>
类,但我无法找到一个好的构造。目前,构造函数抛出ATL
断言抱怨vartype
不匹配。不可否认,新构建的CComSafeArray的CCCC为vartype
为52428(十六进制),看起来可能与未初始化的内存相似。但推断的类型看起来也不正确,我期望safearray的vartype是字符串,但断言代码给出了vartype 12,即UI2。很奇怪。
无论如何,对于熟悉这一点的人来说,这可能很容易。这是一个控制台程序Minimal,Complete,Verifiable,Example(MCVE),符合SO标准。
#include "stdafx.h"
#include <atlbase.h>
#include <atlsafe.h>
#import "c:\windows\System32\scrrun.dll" raw_interfaces_only,raw_native_types, named_guids, rename("DeleteFile", "_DeleteFile"), rename("MoveFile","_MoveFile"), rename("CopyFile", "_CopyFile"), rename("GetFreeSpace", "_GetFreeSpace")
int main()
{
HRESULT hr = S_OK;
CoInitialize(0);
CComQIPtr<Scripting::IDictionary> dictColours;
hr = dictColours.CoCreateInstance(__uuidof(Scripting::Dictionary));
if (!SUCCEEDED(hr)) { return hr; }
CComVariant varZero(0); //dummy value, only interested in keys
hr = dictColours->Add(&variant_t(L"red"), &varZero);
if (!SUCCEEDED(hr)) { return hr; }
hr = dictColours->Add(&variant_t(L"green"), &varZero);
if (!SUCCEEDED(hr)) { return hr; }
hr = dictColours->Add(&variant_t(L"blue"), &varZero);
if (!SUCCEEDED(hr)) { return hr; }
long lColourCount(0);
hr = dictColours->get_Count(&lColourCount);
if (!SUCCEEDED(hr)) { return hr; }
CComVariant varColoursKeys;
hr = dictColours->Keys(&varColoursKeys);
if (!SUCCEEDED(hr)) { return hr; }
SAFEARRAY keys(*(varColoursKeys.parray));
//fine up to this point
CComSafeArray<VARIANT> varColours;
/* The following code throws an error for the next (line) live, vt=52428 or hex CCCC which looks like uninitialised memory
whilst GetType() returns 12 which is UI2 (and somehow I expected string type 8!)
VARTYPE vt;
HRESULT hRes = ::ATL::AtlSafeArrayGetActualVartype(const_cast<LPSAFEARRAY>(psaSrc), &vt);
ATLENSURE_SUCCEEDED(hRes);
ATLENSURE_THROW(vt == GetType(), E_INVALIDARG);
*/
varColours = (keys);
CoUninitialize();
return 0;
}
答案 0 :(得分:2)
改变这个:
SAFEARRAY keys(*(varColoursKeys.parray));
对此:
SAFEARRAY* keys = varColoursKeys.parray;
(可选,首先确保varColoursKeys.vt
具有VT_ARRAY
标志)
然后,您可以向keys
查询其vt
:
hr = ::ATL::AtlSafeArrayGetActualVartype(keys, &vt);
请注意,varColours = keys
将生成数组数据的副本。如果这不是您真正想要的,那么您应该Detach()
来自CComVariant
的数组(或直接使用VARIANT
而不是CComVariant
)和Attach()
它到CComSafeArray
(然后你可以使用CComSafeArray::GetType()
方法)。
此外,在调用CoUninitialize()
之前,您需要确保所有COM包装都超出范围并释放资源。
试试这个:
#include "stdafx.h"
#include <atlbase.h>
#include <atlsafe.h>
#import "c:\windows\System32\scrrun.dll" raw_interfaces_only,raw_native_types, named_guids, rename("DeleteFile", "_DeleteFile"), rename("MoveFile","_MoveFile"), rename("CopyFile", "_CopyFile"), rename("GetFreeSpace", "_GetFreeSpace")
HRESULT DoIt()
{
CComQIPtr<Scripting::IDictionary> dictColours;
hr = dictColours.CoCreateInstance(__uuidof(Scripting::Dictionary));
if (FAILED(hr)) { return hr; }
CComVariant varZero(0); //dummy value, only interested in keys
hr = dictColours->Add(&variant_t(L"red"), &varZero);
if (FAILED(hr)) { return hr; }
hr = dictColours->Add(&variant_t(L"green"), &varZero);
if (FAILED(hr)) { return hr; }
hr = dictColours->Add(&variant_t(L"blue"), &varZero);
if (FAILED(hr)) { return hr; }
long lColourCount(0);
hr = dictColours->get_Count(&lColourCount);
if (FAILED(hr)) { return hr; }
VARIANT varColoursKeys;
hr = dictColours->Keys(&varColoursKeys);
if (FAILED(hr)) { return hr; }
CComSafeArray<VARIANT> varColours;
varColours.Attach(varColoursKeys.parray);
// use varColours as needed...
VARTYPE vt = varColours.GetType();
LONG lLower = varColours.GetLowerBound();
LONG lUpper = varColours.GetUpperBound();
for (LONG i = lLower; i <= lUpper; ++i)
{
VARIANT &v = varColours.GetAt(i);
// use v.bstrVal as needed ...
}
//...
return S_OK;
}
int main()
{
HRESULT hr = CoInitialize(0);
if (FAILED(hr)) { return hr; }
hr = DoIt();
CoUninitialize();
return hr;
}