views:

208

answers:

1

This is what i am doing: I am currently required to open and use a .NET assembly in delphi. The Assembly that i am trying to use also has an assembly of objects it is using. I was able to use regasm to create a type library for the main assembly. I was also able to assign a SN and register it with the GAC.

Now when i try to import the type library into delphi, after the pas file is created, no methods show up in the pas file. I can see random create methods but none of the original methods i created in the C# version I can not think of what the problem could be that is causing this to fail. Any Ideas?

+1  A: 

The whole COM system revolves around interfaces. Basically, you'll need to alter your classes in the assembly to implement interfaces that contain the methods you wish to call. You'll then be able to create an instance of each class in the assembly as an interface from Delphi, and call the methods that way.

For example:

// In the assembly
public interface IMyInterface
{
    void DoSomething();
}

public class MyImplementingClass : IMyInterface
{
    void DoSomething()
    {
        //
    }
}

Now your PAS file will get an interface declaration including the DoSomething method, and a couple GUIDs.

To call this method from Delphi, you'd use code similar to this:

procedure CallDoSomething;
var
  hr: HResult;
  Intf: IMyInterface;
  TypeLib: ITypeLib;
begin
  OLECHECK(LoadRegTypeLib(LIBID_MyAssembly, MyAssemblyMajorVersion, MyAssemblyMinorVersion, 0, TypeLib));

  Intf := nil;
  hr := CoCreateInstance(CLASS_MyImplementingClass, nil, CLSCTX_INPROC_SERVER, IID_IMyInterface, Intf);
  if Failed(hr) then
    raise Exception.CreateFmt('Failed to create instance of interface. (%x)', [hr]);

  Intf.DoSomething();
end;
Jon Benedicto
I went and created a test assembly to mimmic yours. And the PAS file create has the GUIDs but does not have the method DoSomething.
Andy Xufuris
Does the PAS file contain the interface declaration?
Jon Benedicto