我写了一个.NET Windows服务,它内置了一个WCF服务。我可以从Visual Studio 2010中编写的C#客户端调用此方法。但是,我也需要从Visual Studio 6编写的MFC应用程序中调用它。我添加了COM Interop代码来尝试实现此目的,创建了一个snk文件,使用了regasm注册并安装它。无法从C++调用.Net服务
WCF服务和MFC应用程序都是32位。
我的WCF服务接口的代码看起来是这样的
// 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)
{
...
}
}
我的MFC代码如下所示:
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
然而,的CreateInstance失败的-2147221164一个HRESULT。即班级没有登记。我错过了什么?
更新: 我添加ComVisible(true)到接口上的Guid行,并将/ codebase选项添加到regasm调用。现在,从MFC应用程序中,CreateInstance成功,但对AddFile的调用不起任何作用。没有错误,但方法未被调用。我知道这是因为我登录了该方法。此方法在C#.NET客户端上正常工作。
因此,我越来越近,但现在不明白为什么方法调用不会创建错误,但尚未调用到我的.NET服务中。
有没有人有任何想法?
如果确实是这样编译的话,那么我不妨敲掉整个项目的头部。目前我正在做的只是一个测试应用程序。这样做对于整个项目来说就像是一场维护噩梦。感谢您的链接。 – Jonnster 2011-06-06 12:20:03
我认为它真的归结为只创建一个.net dll并使用“添加服务引用”选项来创建一个服务代理,然后从C++应用程序调用公开代理方法的.net dll方法。我是一个C++开发人员12年以上,但在6年左右没有使用C++ ...这只是痛苦的:) – jonchicoine 2011-06-06 16:33:06