I used a online example of C# COM and call it through C++. The following is the C# COM code, only one interface "ICalc" and one class "Calc".
namespace COMLib
{
[Guid("F40D7BC9-CF53-4613-AA5E-F269AD73808F")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ICalc
{
[DispId(1)]
long Factorial(int n);
}
[Guid("8EE38F2E-75BE-4B45-87B6-3F6D15FDBBC5")]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(ICalc))]
[ProgId("MyCalc")]
public class Calc : ICalc
{
long ICalc.Factorial(int n)
{
long fact = 1;
for (int i = 1; i <= n; i++)
fact *= i;
return fact;
}
}
}
The following code is in C++ to call this this function. It is worked. However, I am confused with the code in second line. where is the "ICalcPtr" come from? Or is this some mechanism?
CoInitialize(NULL);
COMLib::ICalcPtr pCalc;
HRESULT hRes = pCalc.CreateInstance(__uuidof(COMLib::Calc));
if(FAILED(hRes))
printf("ICalcPtr::CreateInstance failed w/err 0x%08lx\n", hRes);
else
{
printf("%d\n", pCalc->Factorial(3));
}
system("pause");
CoUninitialize();
return 0;
#importstatement? That's Visual Studio extension that generate classes based on a COM type library (.tlb). msdn.microsoft.com/en-us/library/8etzzkb6%28v=vs.71%29.aspx