初始化com对象的问题
Issue with initializing COM object
我编译了以下VB代码,并获取了DLL(" testvb.dll"(和tlb(" testvb.tlb"(文件作为输出。
Imports System
Imports System.Runtime.InteropServices
Namespace TesterNS
<ComVisible(True),
Guid("4B673F5A-A953-4C20-9A90-8F94ED2F6DDF"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)>
Public Interface _Tester
Function GetMonth() As Integer
End Interface
<ComVisible(True),
Guid("FEC833EE-37E9-4406-9344-8A8BD5C43B07"),
ClassInterface(ClassInterfaceType.None),
ProgId("Tester.Numbers1")> Public Class Tester
Implements _Tester
Public Tester()
Public Function GetMonth() As Integer Implements _Tester.GetMonth
GetMonth = DateTime.Now.Month
End Function
End Class
End Namespace
我选择了
注册com Interop
在VB项目的项目设置中选项。
我正在尝试从C 代码访问上述功能。我在c 中的stdafx.h文件中包括以下导入语句。
#import "TestVB.tlb" no_function_mapping, no_namespace, named_guids
并尝试访问COM对象如下:
HRESULT hr = CoInitialize(NULL);
_TesterPtr t1 = NULL;
CLSID cls;
std::string s = "";
bool flag = false;
hr = CLSIDFromProgID(L"Tester.Numbers1", &cls);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(cls, NULL, CLSCTX_INPROC_SERVER, __uuidof(_Tester), (void**)&t1);
if (SUCCEEDED(hr))
s = "success";
else
s = "fail";
}
我看到hr = REGDB_E_CLASSNOTREG Class not registered
拨打CoCreateInstance
呼叫后的错误。
我不确定我在做什么错。
在我的C 应用程序中,在CoInitialize(NULL)
之前有一个呼叫AfxOleInit()
。如果我删除了CoInitialize(NULL)
的呼叫,我就可以使其正常工作。
相关文章:
- 在CToolBar对象中使用PNG时出现问题
- 将父类对象强制转换为子类的问题
- 类对象数组的问题会导致崩溃
- 在C++中遇到对象和类问题
- 在 Android Studio 中使用 C++ 共享对象时出现问题
- 在C ++ ASIO中,如何为HTTP或HTTPS创建相同的套接字对象(接口/抽象问题?
- 由于缺少类模板,NVCC 编译面向对象代码时出现问题
- C++删除/(递归)对象销毁问题
- C++ - 面试问题中的堆栈上的对象
- 将四元数旋转的游戏对象旋转另一个四元数时出现问题
- 通过 COM 对象连接 x64 应用程序时出现问题
- 擦除矢量问题矢量中的对象
- 将任意对象附加到数组有什么问题
- 在C++中将类实例添加到对象层次结构中的问题
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- C++ 迭代后销毁对象的问题
- "Extern"对象问题:错误:Id 返回 1 个退出状态
- 将文件从 iOS 应用程序加载到 C++ 对象中/<iostream>iOS 上的问题
- 错误 C2280 / 在 std::vector 中实例化对象时复制构造函数出现问题?
- 与 32 位共享对象的链接问题