[英]Load a com DLL in c#
我想在c#控制台应用程序中动态加载COM
dll。
到目前为止,我已经尝试了以下代码:
// load exe with args as EXE DllName classNameTobeLoaded
try
{
// load assembly
Assembly Dll = Assembly.LoadFile(@"C:\My_Dir\TestComDll.dll");
// get the type names
foreach(Type t in Dll.GetExportedTypes())
{
dynamic vClassLoaded = Activator.CreateInstance(t,"Test");
Console.WriteLine("Type Loaded");
}
Console.WriteLine("DLL is loaded");
}
catch (Exception ex)
{
Console.WriteLine("Unable to load a DLL because \n" + ex.Message);
}
但是在加载dll时出现以下错误:
{System.BadImageFormatException: The module was expected to contain an assembly manifest. (Exception from HRESULT: 0x80131018)
at System.Reflection.RuntimeAssembly.nLoadFile(String path, Evidence evidence)
at System.Reflection.Assembly.LoadFile(String path)
at ThirdPartyDLLLoader.Program.Main(String[] args) in h:\Test Exe\ThirdPartyDLLLoader\ThirdPartyDLLLoader\Program.cs:line 18}
相同的代码对于.NET DLL
正常工作。
谁能告诉我为什么代码不能动态加载COM dll?
如果不是,请告诉我我该怎么做。
感谢您的任何建议和帮助。
我有一个函数加载类库dll
public ICollection<Assembly> GetAssemblies(string pathOfDLL)
{
List<Assembly> baseAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
var controllersAssembly = Assembly.LoadFrom(pathOfDLL);
baseAssemblies.Add(controllersAssembly);
return baseAssemblies;
}
希望能帮助到你!
无法做到这一点。 Assembly.LoadFrom
方法用于加载.NET程序集。 必须注册COM库,然后才能使用Activator.CreateInstance
方法实例化类。
这是我访问MS Word的方式:
Type type = Type.GetTypeFromProgID("Word.Application");
object obj = Activator.CreateInstance(type);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.