-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathUnmanagedDisposable.cs
More file actions
123 lines (115 loc) · 4.57 KB
/
Copy pathUnmanagedDisposable.cs
File metadata and controls
123 lines (115 loc) · 4.57 KB
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
using System;
using System.Runtime.InteropServices;
#if NET48_OR_GREATER
using NativeLibraryLoader;
#endif
namespace Cuemon.Assets
{
public class UnmanagedDisposable : FinalizeDisposable
{
internal IntPtr _handle = IntPtr.Zero;
internal IntPtr _libHandle = IntPtr.Zero;
public delegate bool CloseHandle(IntPtr hObject);
public delegate IntPtr CreateFileDelegate(string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
public delegate IntPtr PtSname(int fd);
#if NET48_OR_GREATER
internal NativeLibrary _nativeLibrary;
#endif
public UnmanagedDisposable()
{
#if NET9_0_OR_GREATER
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
if (NativeLibrary.TryLoad("kernel32.dll", GetType().Assembly, DllImportSearchPath.System32, out _libHandle))
{
if (NativeLibrary.TryGetExport(_libHandle, "CreateFileW", out var functionHandle))
{
var createFileFunc = Marshal.GetDelegateForFunctionPointer<CreateFileDelegate>(functionHandle);
_handle = createFileFunc(@"C:\TestFile.txt",
0x80000000, //access read-only
1, //share-read
IntPtr.Zero,
3, //open existing
0,
IntPtr.Zero);
}
}
}
else if (Environment.OSVersion.Platform == PlatformID.Unix)
{
var libraryName = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "libSystem.B.dylib" : "libc.so.6";
if (NativeLibrary.TryLoad(libraryName, GetType().Assembly, DllImportSearchPath.SafeDirectories, out _libHandle))
{
_handle = _libHandle; // i don't know of any native methods on unix
}
}
#else
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
_nativeLibrary = new NativeLibrary("kernel32.dll");
_libHandle = _nativeLibrary.Handle;
var functionHandle = _nativeLibrary.LoadFunction("CreateFileW");
var createFileFunc = Marshal.GetDelegateForFunctionPointer<CreateFileDelegate>(functionHandle);
_handle = createFileFunc(@"C:\TestFile.txt",
0x80000000, //access read-only
1, //share-read
IntPtr.Zero,
3, //open existing
0,
IntPtr.Zero);
}
else if (Environment.OSVersion.Platform == PlatformID.Unix)
{
_nativeLibrary = new NativeLibrary("libc.so.6");
_libHandle = _nativeLibrary.Handle;
_handle = _libHandle; // i don't know of any native methods on unix
}
#endif
}
protected override void OnDisposeManagedResources()
{
}
protected override void OnDisposeUnmanagedResources()
{
#if NET9_0_OR_GREATER
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
if (_handle != IntPtr.Zero)
{
if (NativeLibrary.TryGetExport(_libHandle, "CloseHandle", out var closeHandle))
{
var closeHandleAction = Marshal.GetDelegateForFunctionPointer<CloseHandle>(closeHandle);
closeHandleAction(_handle);
}
}
NativeLibrary.Free(_libHandle);
}
else if (Environment.OSVersion.Platform == PlatformID.Unix)
{
NativeLibrary.Free(_libHandle);
}
#else
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
if (_handle != IntPtr.Zero)
{
var closeHandle = _nativeLibrary.LoadFunction("CloseHandle");
var closeHandleAction = Marshal.GetDelegateForFunctionPointer<CloseHandle>(closeHandle);
closeHandleAction(_handle);
}
_nativeLibrary.Dispose();
}
else if (Environment.OSVersion.Platform == PlatformID.Unix)
{
_nativeLibrary.Dispose();
}
#endif
}
}
}