-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
229 lines (204 loc) · 8.32 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System.ComponentModel;
namespace VaListInterop
{
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
public class Program
{
public static void Main(string[] args)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
RunUnix();
}
else if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
RunWindows();
}
else
{
Console.Error.WriteLine("Platform not supported yet");
}
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
public static void RunWindows()
{
string arch;
// Detect the process architecture
switch (RuntimeInformation.ProcessArchitecture)
{
case Architecture.X86:
// Workaround for a detection issue. If x86 is detected, but IntPtr is 8 bytes long, we're on x64
// See https://github.com/dotnet/corefx/issues/25267
if (IntPtr.Size == 8)
{
arch = "x64";
}
else
{
arch = "x86";
}
break;
case Architecture.X64:
arch = "x64";
break;
default:
throw new PlatformNotSupportedException("Only x86 and x64 are supported at the moment");
}
Console.WriteLine(arch);
// Load the correct library
var projectDir = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))));
var handle = WindowsInterop.LoadLibrary(Path.Combine(projectDir, "nativeLibrary", "build", arch, "nativeLibrary.dll"));
if (handle == IntPtr.Zero)
{
Console.Error.WriteLine("Failed to load native library");
throw new Win32Exception(Marshal.GetLastWin32Error());
}
try
{
// Locate the "triggerCallback" function. This function just calls the callback with some parameters.
var procAddress = WindowsInterop.GetProcAddress(handle, "triggerCallback");
if (procAddress == IntPtr.Zero)
{
Console.Error.WriteLine("Failed to locate the triggerCallback function.");
throw new Win32Exception(Marshal.GetLastWin32Error());
}
var triggerCallback = Marshal.GetDelegateForFunctionPointer<NativeLibrary.TriggerCallback>(procAddress);
triggerCallback(WindowsCallback);
}
finally
{
WindowsInterop.FreeLibrary(handle);
}
}
public static void WindowsCallback(string format, IntPtr args)
{
// This implementation is pretty straightforward. The IntPtr can be passed to the functions and can be reused.
var byteLength = WindowsInterop._vscprintf(format, args) + 1;
var utf8Buffer = Marshal.AllocHGlobal(byteLength);
try
{
WindowsInterop.vsprintf(utf8Buffer, format, args);
Console.WriteLine(Utf8ToString(utf8Buffer));
}
finally
{
Marshal.FreeHGlobal(utf8Buffer);
}
}
public static void RunUnix()
{
string arch;
NativeLibrary.Callback callback;
// Detect the process architecture
switch (RuntimeInformation.ProcessArchitecture)
{
case Architecture.X86:
arch = "x86";
callback = LinuxX86Callback;
break;
case Architecture.X64:
arch = "x64";
callback = LinuxX64Callback;
break;
default:
throw new PlatformNotSupportedException("Only x86 and x64 are supported at the moment");
}
Console.WriteLine(arch);
// Load the correct library
var nativeExtension = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "so " : "dylib";
var handle = LinuxInterop.dlopen(Path.Combine(Environment.CurrentDirectory, "nativeLibrary", "build", $"libnativeLibrary.{arch}.{nativeExtension}"), LinuxInterop.RTLD_LAZY);
if (handle == IntPtr.Zero)
{
Console.Error.WriteLine("Failed to load native library");
throw new Exception(LinuxInterop.dlerror());
}
try
{
// Locate the "triggerCallback" function. This function just calls the callback with some parameters.
var procAddress = LinuxInterop.dlsym(handle, "triggerCallback");
if (procAddress == IntPtr.Zero)
{
Console.Error.WriteLine("Failed to locate the triggerCallback function.");
throw new Exception(LinuxInterop.dlerror());
}
var triggerCallback = Marshal.GetDelegateForFunctionPointer<NativeLibrary.TriggerCallback>(procAddress);
triggerCallback(callback);
}
finally
{
LinuxInterop.dlclose(handle);
}
}
public static void LinuxX86Callback(string format, IntPtr args)
{
// This implementation is pretty straightforward. The IntPtr can be passed to the functions and can be reused.
int byteLength = LinuxInterop.vsnprintf(IntPtr.Zero, UIntPtr.Zero, format, args) + 1;
var utf8Buffer = Marshal.AllocHGlobal(byteLength);
try
{
LinuxInterop.vsprintf(utf8Buffer, format, args);
Console.WriteLine(Utf8ToString(utf8Buffer));
}
finally
{
Marshal.FreeHGlobal(utf8Buffer);
}
}
public static void LinuxX64Callback(string format, IntPtr args)
{
// The args pointer cannot be reused between two calls. We need to make a copy of the underlying structure.
var listStructure = Marshal.PtrToStructure<VaListLinuxX64>(args);
int byteLength = 0;
UseStructurePointer(listStructure, listPointer =>
{
byteLength = LinuxInterop.vsnprintf(IntPtr.Zero, UIntPtr.Zero, format, listPointer) + 1;
});
var utf8Buffer = Marshal.AllocHGlobal(byteLength);
try
{
UseStructurePointer(listStructure, listPointer =>
{
LinuxInterop.vsprintf(utf8Buffer, format, listPointer);
Console.WriteLine(Utf8ToString(utf8Buffer));
});
}
finally
{
Marshal.FreeHGlobal(utf8Buffer);
}
}
public static void UseStructurePointer<T>(T structure, Action<IntPtr> action)
{
var listPointer = Marshal.AllocHGlobal(Marshal.SizeOf(structure));
try
{
Marshal.StructureToPtr(structure, listPointer, false);
action(listPointer);
}
finally
{
Marshal.FreeHGlobal(listPointer);
}
}
public static string Utf8ToString(IntPtr ptr)
{
if (ptr == IntPtr.Zero)
{
return null;
}
var length = 0;
while (Marshal.ReadByte(ptr, length) != 0)
{
length++;
}
byte[] buffer = new byte[length];
Marshal.Copy(ptr, buffer, 0, buffer.Length);
return Encoding.UTF8.GetString(buffer);
}
}
}