How to use ATL COM component developed using C++ in WCF REST services in C#?

0

I have developed ATL COM DLL in visual c++. I register the dll and had added reference to the dll in c++. I try to use c++ COM dll in WCF REST services in c#,but I get the following error:

Unable to cast COM object of type 'System.__ComObject' to interface type 'ProVisionCOMDemoLib.ReadDATFile'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{91820B46-B3FD-41FE-9A75-45A72424F480}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

The following code used to call com c++ dll in WCF rest web services in c#:

       object sResult;
       ProVisionCOMDemoLib.ReadDATFile objTest = new ProVisionCOMDemoLib.ReadDATFile();
       objTest.ReadDATFile(mFileName, out sResult) 

Please help me to solve this error.Please suggest some ways to call c++ com dll in WCF Rest services in c#.

****My IDL file contents are below****:

import "oaidl.idl";
import "ocidl.idl";

[
    object,
    uuid(91820B46-B3FD-41FE-9A75-45A72424F480),
    pointer_default(unique)
]
interface IReadDATFile : IUnknown
{

    [] HRESULT ReadDatFile([in] BSTR FilePath, [out] VARIANT* pVariant);
};
[
   uuid(A30039A8-FDC6-4EC5-A2D7-D6257251B44F),
   version(1.0),
]
library ProVisionCOMDemoLib
{
   importlib("stdole2.tlb");
   [
      uuid(FD96E613-579F-4319-AE21-8E4D2A1DF975)        
   ]
   coclass ReadDATFile
   {
       [default] interface IReadDATFile;
   };
};

ReadDATFile.h that contains the BEGIN_COM_MAP section:

class ATL_NO_VTABLE CReadDATFile :
   public CComObjectRootEx<CComSingleThreadModel>,
   public CComCoClass<CReadDATFile, &CLSID_ReadDATFile>,
   public IReadDATFile
   {
     public:
       CReadDATFile()
       {
       }

     DECLARE_REGISTRY_RESOURCEID(IDR_READDATFILE)


     BEGIN_COM_MAP(CReadDATFile)
         COM_INTERFACE_ENTRY(IReadDATFile)
     END_COM_MAP()



     DECLARE_PROTECT_FINAL_CONSTRUCT()

     HRESULT FinalConstruct()
     {
       return S_OK;
     }

     void FinalRelease()
     {
     }

    public:

        STDMETHOD(ReadDatFile)(BSTR FilePath, VARIANT* pVariant);
   };

   OBJECT_ENTRY_AUTO(__uuidof(ReadDATFile), CReadDATFile)

COM component Registration

The ATL COM dll is registered successfully before using it in WCF REST services by the following steps:

  1. Run command prompt as administrator
  2. Regsvr32 "path of the dll"

The COM dll is 32bit dll and WCF rest service is also 32bit

Thanks and Regards vivek

c#
c++
com
asked on Stack Overflow Mar 29, 2017 by vivek • edited Mar 30, 2017 by vivek

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0