3

When I import tlb file that is specific to a .net dll, into Delphi using type library importer, the methods which accept parameters of type .net specific, are replaced by IUnknown. When I want to invoke such method from my Delphi client application, I would like to pass a parameter value of type SQLTransaction. How do I achieve this? Do I need to change all the .net dll method parameters to user defined types that inherit from .net specific types?

I also have mscorlib_TLB.pas imported when I improted .net tlb.

.net method

public class MyConnection : IDisposable
{    
    public int BulkInsert(SqlTransaction tran);
    {...}
}

Method in Delphi imported tlb:

_MyConnection = interface(IDispatch)
    ['{9FB088F8-1033-3A99-B9C6-C7D7D2D40140}']    
    function BulkInsert(const SQLTransaction: IUnknown): int; safecall;
end;
  CoMyConnection = class
    class function Create: _MyConnection;
    class function CreateRemote(const MachineName: string): _MyConnection;
  end;

how do I call the BulkInsert method from my Delphi client application?

fr21
  • 1,746
  • 7
  • 26
  • 45

2 Answers2

3

That is entirely to be expected. The SqlTransaction type is a .net type, unknown to your COM interface. How can you expect some other party, a Delphi program in this case, to be able to obtain an instance of the .net SqlTransaction type.

Your COM interface needs to restrict itself to use only COM types. You will need to replace SqlTransaction with a COM interface. One way to approach the problem is to declare a COM interface, that exposes the necessary functionality of SqlTransaction. Then in your C# Code create a type that implements this interface by delegating the implementation to an instance of SqlTransaction. Then expose a method that allows instantiation of your wrapper interface. On the Delphi side the consumer will instantiate your wrapper interface and pass that to BulkInsert. Naturally you'll change BulkInsert to accept as its parameter the wrapper interface rather than SqlTransaction.

iMan Biglari
  • 4,674
  • 1
  • 38
  • 83
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks for the confirmation. This is exactly what I was afraid of and meant by "Do I need to change all the .net dll method parameters to user defined types that inherit from .net specific types?". I thought may be there is a better solution for this problem. – fr21 Jun 15 '16 at 08:52
  • If you don't need to call any methods on the .net types from the consumer side, your wrapper interface could even be `IUnknown`. In other words you might not need to do very much. – David Heffernan Jun 15 '16 at 08:53
  • I am dealing with COM interop for the first time. May be this is a stupid question. I don't need to call any method on the .net types. I want to create an instance and pass it for IUnknown parameter. Will the instance of TObject work as parameter and the .net dll considers it as an instance of SqlTransaction? – fr21 Jun 15 '16 at 09:00
  • You can't create an instance of it on the Delphi side. You'd have to do that on the C# side. At which point, why do you need to pass it around. Remember that all we can see is this one interface and one function. We have no context. – David Heffernan Jun 15 '16 at 09:01
  • Okay, I get it. Sorry for the incompleteness. There are other methods in the interface that share the transaction object. In other words, many operations using different methods done in one SqlTransaction. – fr21 Jun 15 '16 at 09:04
  • But I get your point, and it is clear how to deal with the situation. Thanks again. – fr21 Jun 15 '16 at 09:05
1

maybe you should first obtain the Interop proxy objects, usually importing a type library in a visual studio project it is done automatically by the ide, I think that Delphy ought to implement the same feature, but I don't sure..

Ciro Corvino
  • 2,038
  • 5
  • 20
  • 33
  • 1
    The second code excerpt is indeed the result of the Delphi IDE importing the type library – David Heffernan Jun 15 '16 at 08:45
  • I figure out, you should see if Delphy compliant with interop objects, I think so, but I don't sure of.. else you may search for converting typelibrary for deplhi C# projects – Ciro Corvino Jun 15 '16 at 08:50
  • What proxy objects? All we have here is `SqlTransaction` which is not exposed as a COM Object. – David Heffernan Jun 15 '16 at 08:52
  • interop proxy objects automatically created by the ide (or the framework that supports type library-interop conversion) – Ciro Corvino Jun 15 '16 at 09:00
  • see also the question related to prevoius answer link: http://stackoverflow.com/q/5834100/3762855 – Ciro Corvino Jun 15 '16 at 09:04
  • why this minus? AFAIK exists Inter Operability framework among TypeLibrary Com objects and .NET Framework Class Library that allows efficiently to invoke Com methods from C# managed code.. this happens through proxies created by Interop Framework provided by .NET. these proxies wrap the TypeLibrary Interface in a way that it is possible execute a simple method call by a .net managed language as C# or VB.NET. – Ciro Corvino Jun 15 '16 at 09:18
  • Regarding that comment, we aren't trying to invoke COM methods from C#. We are exporting C# methods as COM objects to be invoked by other code. – David Heffernan Jun 15 '16 at 09:20
  • From the question seemed to me foregone that the object SqlTransaction required were present in TypeLibrary. It wasn't clear that it were ought to be implemented. I thougth that were a import matter.. – Ciro Corvino Jun 15 '16 at 09:28
  • `SqlTransaction` is a plain .net type, not a COM type: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction(v=vs.110).aspx – David Heffernan Jun 15 '16 at 09:37
  • The down vote is still there. Somebody also up voted you. – David Heffernan Jun 15 '16 at 14:05
  • oh well.. if you want remove it, you are welcome :) but thank you for your informations and also thanks to whom has upvoted me ;) (I hadn't to do programmer but politician) – Ciro Corvino Jun 15 '16 at 16:05
  • I guess you were downvoted because somebody felt your answer was poor. – David Heffernan Jun 15 '16 at 16:12
  • oops pardon .. thank you for your availability you are very kind.. and compliments for answers and competence.. I am a newbie – Ciro Corvino Jun 15 '16 at 16:14