0

I have written a .NET Windows service which has a WCF service built into it. I can call this from a C# client written in Visual Studio 2010. However, I also need to call it from a MFC app written in Visual Studio 6. I have added COM Interop code to try and achieve this, created a snk file, used regasm to register it and installed it.

Both the WCF service and the MFC app are 32 bit.

My WCF service interface code looks something like this

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IGASR" in both code and config file together.
[Guid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")]
[ServiceContract]
public interface IMyClass
{
    [OperationContract]
    void AddFile(string sFilename);
}

[ClassInterface(ClassInterfaceType.None)]
[Guid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)]
public class MyClass : IMyClass
{
    public void AddFile(string sFilename)
    {
        ...
    }
}

My MFC code looks like:

CoInitialize(NULL);

MyNamespace::IMyClassPtr pDotNetCOMPtr;

HRESULT hRes = 
    pDotNetCOMPtr.CreateInstance(MyNamespace::CLSID_MyClass);
if (hRes == S_OK)
{
    pDotNetCOMPtr->AddFile ( _bstr_t(m_strFilename));
}

CoUninitialize ();   //DeInitialize all COM Components

However, the CreateInstance fails with a HRESULT of -2147221164. i.e. the class is not registered. What am I missing?

UPDATE: I added ComVisible(true) to the Guid line on the interface and added the /codebase option to the regasm call. Now, from the MFC app, the CreateInstance succeeds but the call to AddFile doesn't do anything. There is no error but the method isn't being called. I know this because I have logging in the method. This method works fine from a C# .NET client.

So, I am getting closer but now do not understand why the method call does not create an error and yet isn't calling into my .NET service.

Does anyone have any ideas?

Jonnster
  • 3,094
  • 5
  • 33
  • 45

2 Answers2

0

Did you set your C# project to register for COM interop in its project build settings? Either than or you need to run regasm to register your .NET DLL with COM manually.

shf301
  • 31,086
  • 2
  • 52
  • 86
  • The register for COM Interop option in the build settings is greyed out. I have no idea why. Perhaps this is related to the problem. If anyone has any idea how to enable this option then please do let me know. I have used regasm manually but it doesn't help. – Jonnster Jun 06 '11 at 11:59
  • Please seem my update in the question. It now seems to be registered properly as CreateInstance succeeds. However, the call to the method fails (no error but just doesn't get called) – Jonnster Jun 06 '11 at 12:56
0

Perhaps this will help. Looks way more complicated than i would have guessed :)

Create WCF service for unmanaged C++ clients

good luck

Community
  • 1
  • 1
jonchicoine
  • 510
  • 3
  • 14
  • If it really is that compilcated then I might as well knock the whole project on the head. What I am doing at the moment is just a test app. Doing that for the full project just looks like a maintenance nightmare. Thanks for the link though. – Jonnster Jun 06 '11 at 12:20
  • I think it really boils down to just creating a .net dll and use the "add service reference" option to create a service proxy, and then call the .net dll methods from the c++ app that expose the proxy methods. I was a c++ developer for 12+ years, but haven't used c++ in 6 years or so... it's just to painful :) – jonchicoine Jun 06 '11 at 16:33