How could I compile a .cs program that references a COM DLL from the command line?
Assume I have COM DLL with the following path: C:\Projects\com1.dll
Assume C# code is in file: C:\Projects\program.cs
I’ve tried this
csc.exe /lib:”C:\Projects” /reference:com1.dll “C:\Projects\program.cs”
hoping to get program.exe This fails so the references for COM dlls by this method is wrong route.
— Adding specifics in reference to first answer —
Okay assuming in this case I’m using ADO
tlbimp "C:\Program Files\Common Files\Syste \ado\msador15.dll"
Get this response
Type library imported to ADOR.dll
Now placing that new DLL created by tlbimp into the same directory as a program that has using ADODB in it:
csc /lib:. /reference:ADOR.dll Program.cs
Returns
Program.cs(2,7): error CS0246:
The type or namespace name 'ADODB' could not be found
(are you missing a using directive or an assembly reference?)
— Overloads Issue —
using System;
using ADODB;
class Program
{
static void Main(string[] args)
{
Connection connection = new Connection();
connection.Open(args[0]);
//connection.Open(args[0], "", "", -1);
Console.WriteLine(connection.State);
connection.Close();
}
}
You’ll need to create an interop assembly with tlbimp (or by hand) and reference that, not the COM dll directly. You’ll run tlbimp on com1.dll and then reference Interop.com1.dll (or whatever you call the interop) that tlbimp generates. Note that to use tlbimp the COM component must have a type library.
tlbimp’s default namespace is going to be
Interop.Foo, notFoo, if you want to specifyFooyou need to use/namespace:Foo.