我正在尝试从本机COM客户端中的C#.NET DLL创建COM对象。这是C#.NET代码
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);
}
}
}
如果我告诉Visual Studio将其注册为COM interop并调用CoCreateInstance,它将按预期工作。
现在我想使用相同的DLL作为免费注册的并排COM。我将依赖项添加到我的应用程序清单
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="NetTestCom.X"
version="1.0.0.0" />
</dependentAssembly>
</dependency>
并创建一个文件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>
现在,当我尝试调用CoCreateInstance时,我得到了HRESULT 0x80070002。应用程序,DLL和清单都在同一目录中。
答案 0 :(得分:1)
似乎清单中存在问题。我在应用程序清单
中使用了这个依赖声明<dependency>
<dependentAssembly>
<assemblyIdentity
name="NetTestCom"
version="1.0.0.0"
processorArchitecture="MSIL" />
</dependentAssembly>
</dependency>
并显示为NetTestCom.manifest(使用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>
现在它按预期工作。