我正在使用COM库“Microsoft Scripting Runtime”(c:\ windows \ System32 \ scrrun.dll)中的Scripting.Dictionary。我正在使用import来获取包装纸,并且这些包装纸正在工作。添加一些项目后,我试图获取键列表,但我被卡住了。
我有办法我可以从Dictionary中获取键的SAFEARRAY
,但是我想使用CComSafeArray
中定义的<atlsafe.h>
类,但是我找不到一个好的结构。当前,构造函数抛出ATL
断言,抱怨vartype
不匹配。诚然,新构建的CComSafeArray的CCCC的vartype
为52428,十六进制形式,这看起来像是未初始化的内存。但是推断的类型也不正确,我期望safearray的vartype是字符串,但是断言代码给出的vartype 12是UI2。很奇怪。
无论如何,这对于熟悉这一点的人来说可能很容易。这是根据SO标准的最小,完整,可验证的示例(MCVE)控制台程序。
#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;
}
最佳答案
更改此:
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;
}
关于c++ - 如何初始化SAFEARRAY从Scripting.Dictionary Keys方法返回的CComSafeArray,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49619401/