Hi,
Could you please tell me if .NET component can be used with Delphi 2009 and, if so, could you please send me some example code.
Thanks in advance.
Hi,
Could you please tell me if .NET component can be used with Delphi 2009 and, if so, could you please send me some example code.
Thanks in advance.
Yes it is possible to use a .net component in a win32 program. The unfortunate news is that it is not very simple to do yourself, and I would strongly recommend Hydra.
You can use .NET components with Delphi for .NET which is part of the RAD studio
There are several ways of doing it and traditional COM/Interop is just one way to do it.
Another way is to use the already existing infrastructure that is built into the CLR to support COM/Interop and mixed-mode C++/CLI.
Lucky for you, I've answered a very similar question in another forum already. So I already had some sample code to start with. ;-)
What I show here might not be your cup of tea.
At the end of the day, I wouldn't use classic COM myself. Unless it really makes sense. (Like writing COM-Addins for Office)
In VisualStudio, you can use the refactor/extract interface wizard to get an interface from your component that has the methods that you require.
You need to provide these 3 Attributes
[ComVisible(true)]
[Guid("Create a GUID yourself"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IYourInterface
For the sake of keeping it simple, I assume your component has jut one simple method. Create a classlibrary in VisualStudio and follow the steps from the other page. (To be able to export functions)
[ComVisible(true)]
[Guid("Create a GUID yourself"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IYourInterface
{
void DoSomething(int value);
}
public class YourComponent : IYourInterface
{
public void DoSomething(int value)
{
return value + 1;
}
}
static class Exports
{
[DllExport("createyourcomponent")]
public static void CreateInstance([MarshalAs(UnmanagedType.Interface)]out IYourInterface instance)
{
instance = new YourComponent();
}
}
In Delphi, you can wrap this interface in a component if you wish. Which would hide the fact that .Net is involved:
type
IYourInterface = interface
['{Create a GUID yourself}']//Control+Shift+G in Delphi
// important, safecall is used by COM/Interop
procedure DoSomething(aValue : Integer); safecall;
end;
TYourComponent = class(TComponent)
private
fInnerInstance : IYourInterface;
public
procedure DoSomething(aValue : Integer);
constructor Create(aOwner : TComponent);
end;
implementation
procedure CreateManagedInstance(out aInstance : IYourInterface);
stdcall; external 'YourDotNetLibraryName'
name 'createyourcomponent';
constructor TYourComponent.Create(aOwner : TComponent);
begin
inherited Create(aOwner);
CreateManagedInstance(fInnerInstance);
end;
procedure TYourComponent.DoSomething(aValue : Integer);
begin
fInnerInstance.DoSomething(aValue);
end;
Disclaimer: I didn't have an IDE handy, so there could be typos or other mistakes in the sample code...