gpt4 book ai didi

c++ - 如何使用 CoCreateInstance 实例化 Scripting.Dictionary?

转载 作者:行者123 更新时间:2023-11-30 03:38:46 29 4
gpt4 key购买 nike

我一直在阅读 COM 和 VBScript,现在我很好奇是否可以使用 CoCreateInstance 函数从 C++ 创建一个 Scripting.Dictionary 对象。

我想要完成的是这样的:

脚本.vbs

' reminder: run with "cscript script.vbs"
Option Explicit

Dim myDictionary

Set myDictionary = CreateObject("Scripting.Dictionary")

myDictionary.Add "Foo", "Bar"

MsgBox myDictionary.Item("Foo")

我实际上不明白实例化 COM 对象的过程是如何工作的,但据我了解,它看起来应该是这样的:

ma​​in.cpp:

/* reminder: use Unicode */
#include <Windows.h>
#include <stdio.h>

/**
* Takes a guid and dumps it out to stdin as a string.
* @param guid guid to dump to stdin.
*/
void DumpGUID(GUID guid)
{

char dumpData[81];
sprintf_s (
&dumpData[0],
81,
"{"
"\"Data1\": \"%u\","
"\"Data2\": \"%u\","
"\"Data3\": \"%u\","
"\"Data4\": \"%u\""
"}",
guid.Data1,
guid.Data2,
guid.Data3,
guid.Data4
);
puts(dumpData);
}

int main(int argc, char **argv)
{
GUID guid;
const wchar_t *ScriptingDictionaryGUID = L"EE09B103-97E0-11CF-978F-00A02463E06F";

void *myDictionary = NULL;

/* anonymous scope: set guid to 0 so I can tell if it has changed */
{
memset(&guid, 0, sizeof(guid));
}

DumpGUID(guid);

/* anonymous scope: convert my string to guid */
{
HRESULT whyNoConvert = CLSIDFromString((LPCOLESTR)ScriptingDictionaryGUID, &guid);
if (FAILED(whyNoConvert)) {
printf("can't convert string to guid, got error %d.\n", whyNoConvert);

/* ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680589(v=vs.85).aspx */
switch (whyNoConvert) {
case NOERROR:
puts("The CLSID was obtained successfully.");
break;
case CO_E_CLASSSTRING:
puts("The class string was improperly formatted.");
break;
case REGDB_E_CLASSNOTREG:
puts("The CLSID corresponding to the class string was not found in the registry.");
break;
case REGDB_E_READREGDB:
puts("The registry could not be opened for reading.");
break;
}
system("pause");
}
}
DumpGUID(guid);

HRESULT instantiationResult = CoCreateInstance (
guid,
NULL,
CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
IID_IDispatch,
&myDictionary
);
DumpGUID(guid);

if (FAILED(instantiationResult)) {
puts("Can't create a dictionary :(");
} else {
puts("I HAVE DONE IT!");
}

system("pause");
return 0;
}

我的代码无法创建字典对象(但它可以编译)...它也无法从我的字符串实际转换为 GUID,并出现错误“类字符串格式不正确。”。

我不知道我做错了什么(或者我在这方面做对了什么)。

编辑:工作代码(仍然需要弄清楚如何在创建对象后与对象交互)

#ifndef UNICODE
#define UNICODE
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef STRICT
#define STRICT
#endif
#pragma comment(lib, "uuid.lib")
#pragma comment(lib, "ole32.lib")

#include <windows.h>
#include <stdio.h>

int main(int argc, char **argv)
{
GUID dictionary_guid = {0xEE09B103, 0x97E0, 0x11CF, {0x97, 0x8F, 0x00, 0xA0, 0x24, 0x63, 0xE0, 0x6F}};
IDispatch* p_dictionary = nullptr;

/* anonymous scope: initializing COM */
{
HRESULT result = CoInitialize(NULL);
if (!SUCCEEDED(result)) {
printf("CoInitialize failed: %u.\n", result);
system("pause");
exit(0);
}
}

/* anonymous scope: creating instance and checking */
{
HRESULT result = CoCreateInstance (
dictionary_guid,
nullptr,
CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
IID_IDispatch,
reinterpret_cast<void**>(&p_dictionary)
);
if (!SUCCEEDED(result)) {
puts("CoCreateInstance failed");
system("pause");
exit(0);
}

puts ("I HAVE DONE IT!");
printf("HRESULT = %u.\n", result);
printf("p_dictionary: %u.\n", p_dictionary);
}
p_dictionary->Release();
CoUninitialize();

system("pause");
return EXIT_FAILURE;
}

提前致谢!

最佳答案

如果您使用 Visual Studio,有一个很酷的 #import directiveType Library 时生成简化 COM 的包装器可用。

大多数情况下,类型库包含在 COM 对象本身(为您所追求的对象提供服务的 DLL)中,或者包含在 DLL 旁边的 .TLB 中。这是一个简单的示例,其中包含驻留在 C:\Windows\System32\scrrun.dll 中的脚本对象:

#include "stdafx.h"

// import the .TLB that's compiled in scrrun.dll
#import "C:\Windows\System32\scrrun.dll" \
rename("FreeSpace", "FreeSpace2") // avoid name collision with Windows SDK's GetFreeSpace macro, this is specific to scrrun.dll

using namespace Scripting;

// sample usage of Scripting.Dictionary
void CreateDicAndAdd()
{
IDictionaryPtr ptr(__uuidof(Dictionary)); // create an instance of the Dictionary coclass and get an IDictionary pointer back
_variant_t foo = L"foo";
_variant_t bar = L"bar";
ptr->Add(&foo, &bar); // call the Add method

wprintf(L"%i\n", ptr->Count); // call the Count property (wrapper that was generated automatically)

_variant_t outBar;
ptr->get_Item(&foo, &outBar); // get the item for "foo"

// here we know it's a string (outBar.vt could tell you, in case you didn't know)
// in fact, it's a BSTR, but a BSTR is also a LPWSTR
// (the reverse is false, welcome to Automation :-)
wprintf(L"%s\n", outBar.bstrVal);
}

// sample driver code.
int main()
{
CoInitialize(NULL);
CreateDicAndAdd();
CoUninitialize();
return 0;
}

很棒的是所有这些包装器(_variant_t、IDictionaryPtr 等)都很智能,这意味着您不必显式释放或处置它们。最后,它与使用高级语言(VBScript、JScript、C# 等)编写 COM 的方式非常相似。

关于c++ - 如何使用 CoCreateInstance 实例化 Scripting.Dictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39307575/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com