-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathProgram.cs
80 lines (69 loc) · 2.44 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
namespace OutOfProcDemo
{
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
/// <summary>
/// Managed definition of COM interface
/// </summary>
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("F38720E5-2D64-445E-88FB-1D696F614C78")]
internal interface IServer
{
double ComputePi();
}
class Program
{
private static int processId;
static void Main(string[] args)
{
// Record the current process ID
using (var p = Process.GetCurrentProcess())
{
processId = p.Id;
}
const string remoteObjectName = "OutOfProcDemoObject";
// Acquire the Running Object Table instance
using (var rot = new RunningObjectTable())
{
if (args.Length == 0 || args[0].Equals("server"))
{
Log($"Server running...");
// Register object instance in Running Object Table
var server = new NetServer.Server();
using (rot.Register(remoteObjectName, server))
{
var thisAssembly = Assembly.GetEntryAssembly().Location;
Log("Launching client...");
using (var client = Process.Start(thisAssembly, "client"))
{
client.WaitForExit();
}
}
}
else if (args[0].Equals("client"))
{
Log($"Client running...");
// Acquire remote object
object remoteObject = rot.GetObject(remoteObjectName);
var server = (OutOfProcDemo.IServer)remoteObject;
Log("Calling remote object...");
var pi = server.ComputePi();
Log($"\u03C0 = {pi}");
Log("Press any key to continue...");
Console.ReadKey();
}
else
{
throw new ArgumentException("Invalid demo mode");
}
}
}
static void Log(string fmt, params object[] fmtargs)
{
Console.WriteLine($"Process {processId}: {string.Format(fmt, fmtargs)}");
}
}
}