COM C#, How to use C++ to call COM -
i used online example of c# com , call through c++. following c# com code, 1 interface "icalc" , 1 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; (int = 1; <= n; i++) fact *= i; return fact; } } }
the following code in c++ call this function. worked. however, confused code in second line. "icalcptr" come from? or 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;
if code works, because somewhere in c++ project, there following statement defined (it's automatic if used #import directive example):
_com_smartptr_typedef(icalc, __uuidof(icalc));
_com_smartptr_typedef macro defines "smart pointer" on icalc
. it's microsoft visual c++ magical extension easier com support. official documentation here: _com_ptr_t class
a smart pointer referenced typedef definition provided _com_smartptr_typedef macro. macro takes interface name , iid , declares specialization of _com_ptr_t name of interface plus suffix of ptr.
Comments
Post a Comment