I compiled the following VB code and got the dll ("TestVB.dll") and tlb ("TestVB.tlb") files as output.
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
I selected the
Register for COM interop
option in the project settings for the VB project.
I am trying to access the above functionality from a C++ code. I included the following import statement in the stdafx.h file in C++.
#import "TestVB.tlb" no_function_mapping, no_namespace, named_guids
and trying to access the COM object as follows:
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";
}
I am seeing hr = REGDB_E_CLASSNOTREG Class not registered
error after making the CoCreateInstance
call.
I am not sure what I am doing wrong.
In my C++ application, there is a call to AfxOleInit()
before CoInitialize(NULL)
. If I remove the call to CoInitialize(NULL)
, I was able to get it working.