what's wrong in my code related to COM?

不羁的心 提交于 2019-12-04 19:09:15

When working with COM, your assemblies should be "Release Builds". Make sure that's the case before digging around any further.

These two statements:

IDiscoverPtr pICalc(__uuidof(SqlClass));

HRESULT hRes=CoCreateInstance(Test::CLSID_SqlClass, NULL, CLSCTX_INPROC_SERVER, Test::IID_IDiscover, reinterpret_cast<void**> (&pICalc));

do exactly the same thing, so you don't need to run them in sequence.

The first one translates the HRESULT into an exception of type _com_error. I'd try something like:

  IDiscoverPtr pDiscover;
  HRESULT hr = pDiscover.CreateInstance(__uuidof(SqlClass));
  if (FAILED(hr))
  {
    printf("SQL DMO is not avilable. Error code: 0x%X\n", hr);
  }

Could you post back what the error code is?

The IDiscoverPtr constructor will throw an exception if it can't instantiate the COM object. The most likely reason is that the COM object is not registered in the registry of that machine (regasm was not run for the .NET assembly that implements the IDiscover).

I have seen the error 0x80004002 E_NOINTERFACE if you expose a property of type DateTime on the com visible type you are trying to call CreateInstance() on. I've also noticed that you have to Rebuild the project that imports the tlb file instead of a Build if the tlb file changes for example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!