[ComVisible(true)]
[
Guid("C9B334E4-BDAE-419E-9305-143B2C454CEA")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface ISample
{
String Value
{
[return: MarshalAs(UnmanagedType.BStr)]
get;
[param: In, MarshalAs(UnmanagedType.BStr)]
set;
}
void ShowValue();
}
class ManagedSample : ISample
{
public String Value { get; set; }
public int GetLength()
{
// Delphi wird mit hier mit "this" klarkommen, da es als ISample genutzt werden kann
return DelphiImports.GetValueLength(this);
}
public void ShowValue()
{
Console.WriteLine("From .Net {0}", Value);
}
}
static class DelphiImports
{
[DllImport("SampleInterfaceDLL")]
static extern void CreateSample([MarshalAs(UnmanagedType.Interface)]out ISample instance);
public static ISample CreateSample()
{
ISample instance;
CreateSample(out instance);
return instance;
}
[DllImport("SampleInterfaceDLL")]
public static extern int GetValueLength([MarshalAs(UnmanagedType.Interface)]ISample instance);
}
class Program
{
static void Main(string[] args)
{
var delphiInstance = DelphiImports.CreateSample();
delphiInstance.Value = "Test";
var l1 = DelphiImports.GetValueLength(delphiInstance);
var managedInstance = new ManagedSample { Value = "Def" };
var l2 = managedInstance.GetLength();
Array.ForEach(new[] { delphiInstance, managedInstance },
i => i.ShowValue());
}
}