C ++项目中的loadFromRemoteSources

时间:2013-12-09 13:27:05

标签: c++ visual-studio-2010 dll interop com-interop

我已经创建了一个C ++ DLL(它必须是C ++),它将.Net DLL与主机Web服务器联系起来。 .Net DLL将Web服务调用传递给C ++ DLL,该DLL也通过.Net DLL进行评估和响应。

类似的东西:

     HSPWebService.DLL
-----------------------------------
HSPProxy.DLL | HSPWebServiceLib.DLL
-----------------------------------
      HSPSendData.DLL
  • HSPWebService.DLL - 正确的C ++ DLL。
  • HSPProxy.DLL - 由MIDL编译器通过HSP.DLL MIDL接口生成的代理。
  • HSPWebServiceLib.DLL - tlmbimp通过HSP.DLL生成的typelib。
  • HSPSendData.DLL - 托管Web服务服务器的.Net DLL

一切都像魅力一样。问题是DLL文件在网络共享(// myPc / share)中。我的应用程序日志显示错误0x80131515:

CreateAssemblyInstance错误:无法创建程序集实例。 (HR = 80131515)

在我的研究中,我发现发生了0x80131515错误,因为.Net框架默认情况下不会从外部源加载程序集。对于.Net项目,可以将选项设置为项目设置。但我在Visual Studio 2010上有一个C ++项目,我不知道在我的项目(或我的代码)上使用这个配置。有什么想法吗?

CreateAssemblyInstance函数:

HRESULT CHSPWebServiceObjectHost::CreateAssemblyInstance(_AppDomain* pDefAppDomain, CComPtr<IDispatch>& spDisp, LPCTSTR pszAsseblyName, LPCTSTR pszClassNameWithNamespace) const
{
  spDisp = NULL;
  REQUIRE_IN_POINTER(pDefAppDomain);
  try
  {
    _bstr_t _bstrAssemblyName(pszAsseblyName);
    _bstr_t _bstrszClassNameWithNamespace(pszClassNameWithNamespace);
    //Creates an Assembly instance
    CComPtr<_ObjectHandle> spObjectHandle;
    HRESULT hr = pDefAppDomain->CreateInstanceFrom(_bstrAssemblyName,   _bstrszClassNameWithNamespace, &spObjectHandle);
    if (FAILED(hr))
    {
      Log(logDriver, _T("CHSPWebServiceObjectHost::CreateAssemblyInstance ERROR: Could not create an assembly instance. (hr=%08X)"), hr);
      return hr;
    }

    CComVariant VntUnwrapped;
    hr = spObjectHandle->Unwrap(&VntUnwrapped);
    if (FAILED(hr))
    {
      Log(logDriver, _T("CHSPWebServiceObjectHost::CreateAssemblyInstance ERROR: Could not unwrap assembly object. (hr=%08X)"), hr);
      return hr;
    }

    spDisp = VntUnwrapped.pdispVal;
  }
  catch (_com_error& e)
  {
    return e.Error();
  }
  return S_OK;
}

由StartCLR函数调用:

HRESULT CHSPWebServiceObjectHost::StartCLR(CComPtr<ICorRuntimeHost>& spRuntimeHost, CComPtr<IDispatch>& spDispHost) const
{
  spRuntimeHost = NULL;
  spDispHost = NULL;

  //Retrieve a pointer to the ICorRuntimeHost interface
  HRESULT hr = CorBindToRuntimeEx(L"v4.0.30319",
                                  L"wks",
                                  STARTUP_LOADER_SAFEMODE | STARTUP_CONCURRENT_GC, 
                                  CLSID_CorRuntimeHost,
                                  IID_ICorRuntimeHost,
                                  (void**)&spRuntimeHost);

  if (FAILED(hr))
  {
    Log(logDriver, _T("CHSPWebServiceObjectHost::StartCLR ERROR: Could not load CLR into unmanaged host process. (    hr=%08X)"), hr);    
    return hr;    
  }    

  //Start the CLR
  hr = spRuntimeHost->Start();
  if (FAILED(hr))
  {
    Log(logDriver, _T("CHSPWebServiceObjectHost::StartCLR ERROR: Could not start CLR. (hr=%08X)"), hr);
    return hr;
  }

  //Retrieve the IUnknown default AppDomain
  CComPtr<IUnknown> spUnknown;
  hr = spRuntimeHost->GetDefaultDomain(&spUnknown);
  if (FAILED(hr))
  {
    Log(logDriver, _T("CHSPWebServiceObjectHost::StartCLR ERROR: Could not retrieve pointer to domain interface. (    hr=%08X)"), hr);    
    return hr;    
  }    

  CComQIPtr<_AppDomain> spDefAppDomain(spUnknown);
  if (spDefAppDomain == NULL)
    return E_NOINTERFACE;

  CString strAssemblyFullPath = _T(".\\HSPSendData.dll");

  return CreateAssemblyInstance(spDefAppDomain, spDispHost, strAssemblyFullPath, _T("Elipse.HSPWebService.    HSPWebServiceHost"));    
}    

2 个答案:

答案 0 :(得分:0)

鉴于您的程序集位于网络共享上,您需要在app.config文件中添加<loadFromRemoteSources>元素。例如:

<configuration>
   <runtime>
      <loadFromRemoteSources enabled="true"/>
   </runtime>
</configuration>

您没有提及您的主机进程,但假设它是MyService.exe,则会进入MyService.exe.config文件。

答案 1 :(得分:0)

显然 .NET更新已经解决了这个问题。我试图回溯哪个更新已经完成,但我无法找到它。