In our header file we have the class definition where methods GroupInit
, EditInit
were protected
.
I want to be able to access these methods by adding a reference to the generated GrpSvr.tlb file from a C# solution so I changed these to public...
#if !defined(AFX_GRPCALL_H__FFB54BC3_B15E_11D1_99BC_0000E803C444__INCLUDED_)#define AFX_GRPCALL_H__FFB54BC3_B15E_11D1_99BC_0000E803C444__INCLUDED_#if _MSC_VER >= 1000#pragma once#endifclassCGrpCall:publicCCmdTarget{
DECLARE_DYNCREATE(CGrpCall)CGrpCall();// protected constructor used by dynamic creation// Attributespublic:// Operationspublic:public:virtualvoidOnFinalRelease();
DECLARE_MESSAGE_MAP()
DECLARE_OLECREATE(CGrpCall)
afx_msg BSTR GroupInit(LPCTSTR bstrIniFile, BOOL bDiagErr, BOOL bProcErr);
afx_msg BSTR EditInit(LPCTSTR bstrIniFile);
DECLARE_DISPATCH_MAP()
DECLARE_INTERFACE_MAP()// Implementationprotected:virtual~CGrpCall();};#endif// !defined(AFX_GRPCALL_H__FFB54BC3_B15E_11D1_99BC_0000E803C444__INCLUDED_)
The GrpSvr.cpp code is
IMPLEMENT_DYNCREATE(CGrpCall,CCmdTarget)CGrpCall::CGrpCall(){EnableAutomation();AfxOleLockApp();Initialize(FALSE);}CGrpCall::~CGrpCall(){AfxOleUnlockApp();GroupTerm();}voidCGrpCall::OnFinalRelease(){CCmdTarget::OnFinalRelease();}
BEGIN_MESSAGE_MAP(CGrpCall,CCmdTarget)
END_MESSAGE_MAP()
BEGIN_DISPATCH_MAP(CGrpCall,CCmdTarget)
DISP_FUNCTION(CGrpCall,"GroupInit",GroupInit, VT_BSTR, VTS_BSTR VTS_BOOL VTS_BOOL)
DISP_FUNCTION(CGrpCall,"EditInit",EditInit, VT_BSTR, VTS_BSTR)
END_DISPATCH_MAP()staticconst IID IID_IGrpCall ={0xffb54bc2,0xb15e,0x11d1,{0x99,0xbc,0x0,0x0,0xe8,0x3,0xc4,0x44}};
BEGIN_INTERFACE_MAP(CGrpCall,CCmdTarget)
INTERFACE_PART(CGrpCall, IID_IGrpCall,Dispatch)
END_INTERFACE_MAP()
IMPLEMENT_OLECREATE(CGrpCall,"GrpSvr.GrpCall",0xffb54bc2,0xb15e,0x11d1,0x99,0xbc,0x0,0x0,0xe8,0x3,0xc4,0x44)
BSTR CGrpCall::GroupInit(LPCTSTR bstrIniFile, BOOL bDiagErr, BOOL bProcErr){/* Some Code */}
BSTR CGrpCall::EditInit(LPCTSTR bstrIniFile){/* Some Code */}
I can now see these methods by adding a reference to the COM library in C# and doing
GrpSvr.GrpCall g =newGrpSvr.GrpCall();
g.GroupInit(this.strCommandFilePath,true,true);
However, upon the instantiation of the GrpCall class this is giving
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll. ... Class not registered ...
I have registered the COM library using regsvr32 and regtlibv12 on the .tlb file so that the library shows up as a COM component and I can add it as a reference in my C# consumer. But this has not helped...
What do I have to do to this code in order to be able to call the methods directly (modify GrpSvr.cpp)?
Thanks for your time.
"Everything should be made as simple as possible, but not simpler" - Einstein