Previously written CS in DelphiProgramToday, the customer needs to add several modules, just click C #, and the subsequent modules will be compiled into DLL files using C, when calling the DLL file written in C # using Delphi, I wrote it down after a while.
1. Open vs2005
Create a Windows application project named SFRM and delete the automatically generated program. CS.
(Because we want to generate DLL files)
Create an interface shfrm for the form class to implement the interfaceCodeAs follows:
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. Data. sqlclient;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Namespace SFRM
{
Public interface shfrm // This interface is required for calling in Delphi
{
Void shchildfrm ();
}
Public partial class form1: form, shfrm
{
Private bindingsource bindingsource1 = new bindingsource ();
Private sqldataadapter dataadapter = new sqldataadapter ();
Public form1 ()
{
Initializecomponent ();
}
/// <Summary>
/// Display window
/// </Summary>
Public void shchildfrm ()
{
Form1 FRM = new form1 ();
FRM. Show ();
}
/// <Summary>
/// Button event
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void button#click (Object sender, eventargs E)
{
Datagridview1.datasource = bindingsource1;
Getdata ("select * from MERs ");
}
Private void getdata (string selectcommand)
{
Try
{
String connectionstring = "Data Source =.; initial catalog = northwind; user id = sa; Pwd = ";
Dataadapter = new sqldataadapter (selectcommand, connectionstring );
Sqlcommandbuilder commandbuilder = new sqlcommandbuilder (dataadapter );
Datatable table = new datatable ();
Table. locale = system. Globalization. cultureinfo. invariantculture;
Dataadapter. Fill (table );
Bindingsource1.datasource = table;
Datagridview1.autoresizecolumns (
Datagridviewautosizecolumnsmode. allcellsexceptheader );
}
Catch (sqlexception)
{
MessageBox. Show ("to run this example, replace the value of the" +
"Connectionstring variable with a connection string that is" +
"Valid for your system .");
}
}
}
}
Right-click the project name and change the output type to "class library" in the Properties dialog box. On the page, click the assembly information button, as shown in:
Make the Assembly com visible must be selected
Complete DLL file generation
Ii. Package the DOTNET class library into a com-Type Library (perform the following operations on the vs command line)
Tlbexp.exe SFRM. dll/out: SFRM. TLB
3. Register a com-Type Library
Regasm.exe SFRM. dll
Iv. Delphi Import Type Library
Delpi, project-> Import Type Library, Selected Type Library: dotnet2com. TLB,
Generate the dotnet2com_tlb unit file. There is an interface shfrm in the unit file.
Shfrm = interface (idispatch)
['{D8400C54-E4B2-36BD-B970-45CD204F319A}']
Procedure shchildfrm; safecall;
End; and the proxy class declares tform1 and obtains the attributes of the shfrm interface:
Property defaultinterface: _ form1 read getdefaultinterface;
V. Use in Delphi
Uses
Sfrm_tlb;
{$ R *. DFM}
Procedure tform1.button1click (Sender: tobject );
VaR
FRM: tform1;
Begin
FRM: = tform1.create (Self );
(FRM. defaultinterface as shfrm). shchildfrm (); // display the form in the DLL file
End;
The Delhi program running result is as follows:
Note: installation is required in the running environment. . NET environment and register the DLL file. Otherwise, no registration category is reported.