Cannot Call .Net Service From C++
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 erro开发者_运维百科r 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?
Perhaps this will help. Looks way more complicated than i would have guessed :)
Create WCF service for unmanaged C++ clients
good luck
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.
精彩评论