title: Using a COM dll from C# without a type library
body: <p>I need to use a COM component (a dll) developed in Delphi ages ago. The problem is: the dll does not contain a type library... and every interop feature (eg. TlbImp) in .NET seem to rely on TLBs. The component has been used in Delphi programs here for many years without problems because "It's not much of a problem using COM objects from Delphi, because we know the interfaces" (quote Delphi developer).</p>
  
  <p>Is there any way I can use this DLL from c# without a TLB? I've tried using the DLL as unmanaged, but the only method it exports are <code>DllUnregisterServer</code>, <code>DllRegisterServer</code>, <code>DllCanUnloadNow</code> and <code>DllGetClassObject</code>. I know the names of the classes and functions I'm going to use, if that can be of any help.</p>
  
  <p><strong>UPDATE:</strong>
  I've tried implementing Jeff's suggestion, but I'm getting this error: </p>
  
  <p><em>"Unable to cast COM object of type 'ComTest.ResSrvDll' to interface type 'ComTest.IResSrvDll'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{75400500-939F-11D4-9E44-0050040CE72C}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."</em></p>
  
  <p>This is what I've done:</p>
  
  <p>I got this interface definition from one of the Delphi-guys:</p>
  
  <pre><code>unit ResSrvDllIf;
  
  interface
  
  type
     IResSrvDll = interface
     ['{75400500-939F-11D4-9E44-0050040CE72C}']
      procedure clearAll;
  
      function  ResObjOpen(const aClientID: WideString; const aClientSubID: WideString;
                           const aResFileName: WideString; aResShared: Integer): Integer; {safecall;}
      ...
     end;
  implementation
  end.
  </code></pre>
  
  <p>From this I've made this interface</p>
  
  <pre><code>using System.Runtime.InteropServices;
  namespace ComTest
  {
      [ComImport]
      [Guid("75400500-939F-11D4-9E44-0050040CE72C")]
      [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
      public interface IResSrvDll
      {
          int ResObjOpen(string aClientID, string aClientSubID, string aResFileName, int aResShared);
  
      }
  }
  </code></pre>
  
  <p>And this coclass (got the guid from the delphi-guys)</p>
  
  <pre><code>using System.Runtime.InteropServices;
  
  namespace ComTest
  {
      [ComImport]
      [Guid("75400503-939F-11D4-9E44-0050040CE72C")]
      public class ResSrvDll
      {
      }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The solution from Jeff is the way to do it. It is worth noticing, though, that the interface definition must match the COM-components <em>exactly</em>! ie. same order, same names, etc.</p>