Skip to content

Commit c2ee6d8

Browse files
Add files via upload
1 parent ba0151e commit c2ee6d8

File tree

86 files changed

+23923
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+23923
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
program COMDispInterface;
2+
3+
{$APPTYPE CONSOLE}
4+
{$R *.res}
5+
6+
uses
7+
{$IF CompilerVersion > 22}
8+
System.SysUtils,
9+
{$ELSE}
10+
SysUtils,
11+
{$IFEND}
12+
CNClrLib.Host, CNClrLib.Core;
13+
14+
(* Mathematics.Dll Source Code
15+
namespace Mathematics
16+
{
17+
public class Mathematics
18+
{
19+
[DispId(0)]
20+
public int Add(int a, int b)
21+
{
22+
return a + b;
23+
}
24+
25+
[DispId(1)]
26+
public int Subtract(int a, int b)
27+
{
28+
return a - b;
29+
}
30+
31+
[DispId(2)]
32+
public bool Equal(int a, int b)
33+
{
34+
return a == b;
35+
}
36+
}
37+
}
38+
*)
39+
40+
type
41+
//Corresponding Delphi DispInterface type of Mathematics type in the Mathematics.dll
42+
_Mathematics = dispinterface
43+
['{D77959BD-C7AC-4D65-9980-A88510F776B8}']
44+
function Add(a, b : Integer) : Integer; dispid 0;
45+
function Subtract(a, b : Integer) : Integer; dispid 1;
46+
function Equal(a, b : Integer) : WordBool; dispid 2;
47+
end;
48+
49+
var
50+
Console : _Console;
51+
Mathematics : _Mathematics;
52+
53+
54+
procedure LoadMathematicAssembly;
55+
begin
56+
// If error occurs while executing LoadFrom then
57+
// Right-Click on the file and select properties, click on the
58+
// unblock button to allow access.
59+
TClrAssembly.LoadFrom('Mathematics.dll');
60+
end;
61+
62+
procedure CreateMathematicTypeInstance;
63+
begin
64+
Mathematics := _Mathematics(TClrDispatchActivator.CreateInstance('Mathematics.Mathematics'));
65+
end;
66+
67+
procedure AccessMathematicsObjectMethods;
68+
begin
69+
Console.WriteLine_15('Add(30, 50): {0}', Mathematics.Add(30, 50));
70+
Console.WriteLine_15('Subtract(30, 50): {0}', Mathematics.Subtract(30, 50));
71+
Console.WriteLine_15('Equal(30, 50): {0}', Mathematics.Equal(30, 50));
72+
Console.WriteLine_15('Equal(50, 50): {0}', Mathematics.Equal(50, 50));
73+
end;
74+
75+
begin
76+
Console := CoConsole.CreateInstance;
77+
Console.WriteLine_14('Hello! Welcome to .Net Runtime Library for Delphi.');
78+
Console.WriteLine_14('==================================================');
79+
Console.WriteLine_14('This program demonstrate how to use COM DispInterface to communicate with .Net library type members');
80+
Console.WriteLine;
81+
try
82+
LoadMathematicAssembly;
83+
CreateMathematicTypeInstance;
84+
AccessMathematicsObjectMethods;
85+
except
86+
on E: Exception do
87+
Console.WriteLine_15('Exception: {0}', e.Message);
88+
end;
89+
Console.ReadKey;
90+
end.
91+
92+

‎Demo/Application Domains and Assemblies/COM DispInterface/COMDispInterface.dproj

Lines changed: 494 additions & 0 deletions
Large diffs are not rendered by default.
Binary file not shown.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
program CreateInstanceUsingActivator;
2+
3+
{$APPTYPE CONSOLE}
4+
{$R *.res}
5+
6+
uses
7+
{$IF CompilerVersion > 22}
8+
System.SysUtils, System.Variants,
9+
{$ELSE}
10+
SysUtils, Variants,
11+
{$IFEND}
12+
CNClrLib.Host, CNClrLib.Core;
13+
14+
var
15+
Console : _Console;
16+
Activator : _Activator;
17+
18+
procedure DisplayObjectTypeInfo(AObject : OleVariant);
19+
var
20+
m_type : _Type;
21+
begin
22+
if VarIsEmpty(AObject) or VarIsClear(AObject) then
23+
Console.WriteLine_14('object has not been instantiated')
24+
else
25+
begin
26+
m_type := TClrAssembly.GetObjectType(AObject);
27+
Console.WriteLine_14('object has been instantiated');
28+
Console.WriteLine_15('Assembly FullName: {0}', m_type.Assembly.FullName);
29+
Console.WriteLine_15('FullName: {0}', m_type.FullName);
30+
Console.WriteLine_15('GUID: {0}', m_type.GUID.ToString);
31+
Console.WriteLine_15('ToString: {0}', m_type.ToString);
32+
Console.WriteLine;
33+
Console.WriteLine;
34+
end;
35+
end;
36+
37+
procedure CreateInstanceFromAssemblyString;
38+
var
39+
m_sqlConnHnd : _ObjectHandle;
40+
begin
41+
m_sqlConnHnd := Activator.CreateInstance_5('System.Data, Version=2.0.0.0, Culture=neutral, ' +
42+
'PublicKeyToken=b77a5c561934e089', 'System.Data.SqlClient.SqlConnection');
43+
DisplayObjectTypeInfo(m_sqlConnHnd.Unwrap);
44+
end;
45+
46+
procedure CreateInstanceFromTypeName;
47+
var
48+
m_sqlConnobj : OleVariant;
49+
m_sqlConType : _Type;
50+
begin
51+
TClrAssembly.Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089');
52+
m_sqlConType := TClrAssembly.GetType('System.Data.SqlClient.SqlConnection', True);
53+
m_sqlConnobj := Activator.CreateInstance_4(m_sqlConType);
54+
DisplayObjectTypeInfo(m_sqlConnobj);
55+
end;
56+
57+
procedure CreateInstanceFromTypeWithParam;
58+
var
59+
m_qlConnHnd : OleVariant;
60+
m_objArray : _ObjectArray;
61+
m_sqlConType : _Type;
62+
begin
63+
//Create Instance with a parameter
64+
m_objArray := CoObjectArray.CreateInstance(1);
65+
m_objArray[0] := 'Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myDomain\myUsername;Password=myPassword;';
66+
67+
//Assembly has been loaded in CreateInstance2, no need to reload assembly
68+
m_sqlConType := TClrAssembly.GetType('System.Data.SqlClient.SqlConnection', True);
69+
m_qlConnHnd := Activator.CreateInstance_2(m_sqlConType, m_objArray);
70+
DisplayObjectTypeInfo(m_qlConnHnd);
71+
end;
72+
73+
procedure CreateInstanceFromFile;
74+
var
75+
m_sqlConnobj : _ObjectHandle;
76+
begin
77+
m_sqlConnobj := Activator.CreateInstanceFrom('C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll',
78+
'System.Data.SqlClient.SqlConnection');
79+
DisplayObjectTypeInfo(m_sqlConnobj.Unwrap);
80+
end;
81+
82+
begin
83+
Console := CoConsole.CreateInstance;
84+
Activator := CoActivator.CreateInstance;
85+
Console.WriteLine_14('Hello! Welcome to .Net Runtime Library for Delphi.');
86+
Console.WriteLine_14('==================================================');
87+
Console.WriteLine_14('The program demonstrate how to use Activator interface to Create Object Instance');
88+
Console.WriteLine;
89+
try
90+
CreateInstanceFromAssemblyString;
91+
CreateInstanceFromTypeName;
92+
CreateInstanceFromTypeWithParam;
93+
CreateInstanceFromFile;
94+
except
95+
on E:Exception do
96+
Console.WriteLine_15('Exception : {0}', E.Message);
97+
end;
98+
Console.ReadKey;
99+
end.
100+

0 commit comments

Comments
 (0)