Load a com DLL in c#

I want to load a COM dll dynamically in c# console application.

Till now I have tried the following code:

// load exe with args as EXE DllName classNameTobeLoaded
try
{
 // load assembly
 Assembly Dll = Assembly.LoadFile(@"C:My_DirTestComDll.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);
}

But while loading the dll I am getting error as:

{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 ExeThirdPartyDLLLoaderThirdPartyDLLLoaderProgram.cs:line 18}

The same code is working fine for .NET DLL.

Can any one tell me why the code is not able to load a COM dll dynamically?

and if it is not can you please tell me how I can do the same.

Thanks for any suggestion and help.


Source: dll

Leave a Reply