Skip to content

Commit bf1557f

Browse files
committed
Migrate P/Invoke to LibraryImport, suppress SYSLIB1054 for CreateProcess, enable AllowUnsafeBlocks, fix remaining warnings
1 parent b255184 commit bf1557f

8 files changed

Lines changed: 27 additions & 24 deletions

File tree

PanoramicData.SshServer.Test/CommandService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private void MessageLoop()
5151
break;
5252

5353
var data = bytes.Length != len
54-
? bytes.Take(len).ToArray()
54+
? [.. bytes.Take(len)]
5555
: bytes;
5656
DataReceived?.Invoke(this, data);
5757
}

PanoramicData.SshServer.Test/ExampleApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net10.0</TargetFramework>
6+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
67
</PropertyGroup>
78

89
<ItemGroup>

PanoramicData.SshServer.Test/MiniTerm/Native/ProcessApi.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace ExampleApp.MiniTerm.Native;
66
/// <summary>
77
/// PInvoke signatures for win32 process api
88
/// </summary>
9-
static class ProcessApi
9+
static partial class ProcessApi
1010
{
1111
internal const uint EXTENDED_STARTUPINFO_PRESENT = 0x00080000;
1212

@@ -92,29 +92,33 @@ public readonly bool Equals(SECURITY_ATTRIBUTES other) =>
9292
public static bool operator !=(SECURITY_ATTRIBUTES left, SECURITY_ATTRIBUTES right) => !left.Equals(right);
9393
}
9494

95-
[DllImport("kernel32.dll", SetLastError = true)]
95+
[LibraryImport("kernel32.dll", SetLastError = true)]
9696
[return: MarshalAs(UnmanagedType.Bool)]
97-
internal static extern bool InitializeProcThreadAttributeList(
97+
internal static partial bool InitializeProcThreadAttributeList(
9898
nint lpAttributeList, int dwAttributeCount, int dwFlags, ref nint lpSize);
9999

100-
[DllImport("kernel32.dll", SetLastError = true)]
100+
[LibraryImport("kernel32.dll", SetLastError = true)]
101101
[return: MarshalAs(UnmanagedType.Bool)]
102-
internal static extern bool UpdateProcThreadAttribute(
102+
internal static partial bool UpdateProcThreadAttribute(
103103
nint lpAttributeList, uint dwFlags, nint attribute, nint lpValue,
104104
nint cbSize, nint lpPreviousValue, nint lpReturnSize);
105105

106-
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
106+
// LibraryImport cannot be used here: STARTUPINFOEX contains non-blittable string fields (SYSLIB1051)
107+
#pragma warning disable SYSLIB1054 // Use 'LibraryImportAttribute' instead of 'DllImportAttribute'
108+
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
107109
[return: MarshalAs(UnmanagedType.Bool)]
108110
internal static extern bool CreateProcess(
109111
string lpApplicationName, string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
110112
ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags,
111113
nint lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFOEX lpStartupInfo,
112114
out PROCESS_INFORMATION lpProcessInformation);
115+
#pragma warning restore SYSLIB1054
113116

114-
[DllImport("kernel32.dll", SetLastError = true)]
117+
[LibraryImport("kernel32.dll", SetLastError = true)]
115118
[return: MarshalAs(UnmanagedType.Bool)]
116-
internal static extern bool DeleteProcThreadAttributeList(nint lpAttributeList);
119+
internal static partial bool DeleteProcThreadAttributeList(nint lpAttributeList);
117120

118-
[DllImport("kernel32.dll", SetLastError = true)]
119-
internal static extern bool CloseHandle(nint hObject);
121+
[LibraryImport("kernel32.dll", SetLastError = true)]
122+
[return: MarshalAs(UnmanagedType.Bool)]
123+
internal static partial bool CloseHandle(nint hObject);
120124
}

PanoramicData.SshServer.Test/MiniTerm/Terminal.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void Run() =>
5252
break;
5353
}
5454

55-
DataReceived?.Invoke(this, buf.Take(length).ToArray());
55+
DataReceived?.Invoke(this, [.. buf.Take(length)]);
5656
}
5757

5858
CloseReceived?.Invoke(this, 0);
@@ -88,9 +88,6 @@ private void Dispose(bool disposing)
8888
}
8989
}
9090

91-
public void Dispose()
92-
{
93-
// Do not change this code. Put clean-up code in 'Dispose(bool disposing)' method
91+
public void Dispose() =>
9492
Dispose(disposing: true);
95-
}
9693
}

PanoramicData.SshServer.Test/TcpForwardService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private void MessageLoop()
7676
break;
7777

7878
var data = bytes.Length != len
79-
? bytes.Take(len).ToArray()
79+
? [.. bytes.Take(len)]
8080
: bytes;
8181
DataReceived?.Invoke(this, data);
8282
}

PanoramicData.SshServer/Algorithms/DiffieHellman.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ public byte[] DecryptKeyExchange(byte[] keyEx)
8080
return bytes;
8181
}
8282

83-
private BigInteger BytesToBigint(byte[] bytes) => new(bytes.Reverse().Concat(new byte[] { 0 }).ToArray());
83+
private BigInteger BytesToBigint(byte[] bytes) => new([.. bytes.Reverse(), .. new byte[] { 0 }]);
8484

8585
private byte[] BigintToBytes(BigInteger bigint)
8686
{
8787
var bytes = bigint.ToByteArray();
8888
if (bytes.Length > 1 && bytes[bytes.Length - 1] == 0)
8989
{
90-
return bytes.Reverse().Skip(1).ToArray();
90+
return [.. bytes.Reverse().Skip(1)];
9191
}
9292

93-
return bytes.Reverse().ToArray();
93+
return [.. bytes.Reverse()];
9494
}
9595
}

PanoramicData.SshServer/Messages/Userauth/PublicKeyRequestMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected override void OnLoad(SshDataWorker reader)
2727
if (HasSignature)
2828
{
2929
Signature = reader.ReadBinary();
30-
PayloadWithoutSignature = RawBytes.Take(RawBytes.Length - Signature.Length - 5).ToArray();
30+
PayloadWithoutSignature = [.. RawBytes.Take(RawBytes.Length - Signature.Length - 5)];
3131
}
3232
}
3333
}

PanoramicData.SshServer/Services/Channel.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ public void SendData(byte[] data)
7676
}
7777

7878
if (buf == null || packetSize != buf.Length)
79-
{
80-
buf = new byte[packetSize];
81-
}
79+
{
80+
buf = new byte[packetSize];
81+
}
82+
8283
Array.Copy(data, offset, buf, 0, packetSize);
8384

8485
msg.Data = buf;

0 commit comments

Comments
 (0)