Search code examples
c#comside-by-side

HRESULT 0x80070002 (FILE_NOT_FOUND) when trying to call CoCreateInstance for a SxS C#.NET COM DLL


I'm trying to create a COM Object from a C#.NET DLL in an native COM client. This is the C#.NET code

using System;
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: Guid("3B6B6C37-A5BC-45DF-878E-E9D5C8B009D8")]

namespace NetTestCom
{
    [ComImport]
    [Guid("508012FC-26A9-4985-A985-3EBB03D8D3A6"),
        ComVisible(true)]
    public interface ITestClass
    {
        int StrToInt(String S);
    }

    [Guid("A3D56E20-0792-42D9-B2DD-BB8A8AD75394"),
        ComVisible(true)]
    public class TestClass : ITestClass
    {
        public int StrToInt(String S)
        {
            return Convert.ToInt32(S);
        }
    }
}

If i tell Visual Studio to register it for COM interop and call CoCreateInstance it works as expected.

Now i want to use the same DLL as a registration free Side-by-Side COM. I add the dependency to my application manifest

<dependency>
  <dependentAssembly>
    <assemblyIdentity
      type="win32"
      name="NetTestCom.X"
      version="1.0.0.0" />
  </dependentAssembly>
</dependency>

and create an file NetTestCom.X.manifest

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity
    type="win32"
    name="NetTestCom.X"
    version="1.0.0.0" />

  <clrClass
    clsid="{A3D56E20-0792-42D9-B2DD-BB8A8AD75394}"
    progid="NetTestCom.TestClass"
    threadingModel="Both"
    name="NetTestCom.TestClass"
    runtimeVersion="v2.0.50727" >
  </clrClass>

  <file name="NetTestCom.dll"></file>
</assembly>

Now when I try to call CoCreateInstance i get HRESULT 0x80070002. The application, DLL and manifest are all in the same directory.


Solution

  • It seems that there is a problem in the manifest. I used this dependency declaration for the application manifest

    <dependency>
      <dependentAssembly>
        <assemblyIdentity
          name="NetTestCom"
          version="1.0.0.0"
          processorArchitecture="MSIL" />
      </dependentAssembly>
    </dependency>
    

    and this manifest as NetTestCom.manifest (Generated with GenMan32.exe)

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
        <assemblyIdentity
            name="NetTestCom"
            version="1.0.0.0"
            processorArchitecture="MSIL" />
        <clrClass
            clsid="{A3D56E20-0792-42D9-B2DD-BB8A8AD75394}"
            progid="NetTestCom.TestClass"
            threadingModel="Both"
            name="NetTestCom.TestClass"
            runtimeVersion="v2.0.50727">
        </clrClass>
        <file name="NetTestCom.dll">
        </file>
    </assembly>
    

    Now it works as expected.