Skip to content

Commit

Permalink
- Renamed ExtensionsSystem.cs to ExtensionsGeneral.cs
Browse files Browse the repository at this point in the history
- Removed warnings from `ExtensionsGeneral.cs` file
- Extracted `ProcessExtensions` class to separate file
- Return null when non windows computer attempts to retrieve parent process
- Updated VSCode tasks
  • Loading branch information
Allcharles committed Mar 19, 2020
1 parent c72b759 commit 61d28f6
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 148 deletions.
79 changes: 48 additions & 31 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,58 @@
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/src/AnalysisPrograms/AnalysisPrograms.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/src/AnalysisPrograms/AnalysisPrograms.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/src/AnalysisPrograms/AnalysisPrograms.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
"label": "test",
"command": "dotnet",
"type": "process",
"args": [
"test"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/src/AnalysisPrograms/AnalysisPrograms.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/src/AnalysisPrograms/AnalysisPrograms.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/src/AnalysisPrograms/AnalysisPrograms.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ExtensionsSystem.cs" company="QutEcoacoustics">
// <copyright file="ExtensionsGeneral.cs" company="QutEcoacoustics">
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group).
// </copyright>
// <summary>
Expand All @@ -9,27 +9,30 @@

namespace System
{
using Collections.Generic;
using Collections.Specialized;
using ComponentModel;
using Diagnostics;
using IO;
using Linq;
using Linq.Expressions;
using Runtime.InteropServices;
using Runtime.Serialization;
using Runtime.Serialization.Formatters.Binary;
using Text;
using Text.RegularExpressions;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;

public static class ExtensionsGeneral
{
/// <summary>Deserialise byte array to object.
/// <summary>
/// Gets ExecutingDirectory.
/// </summary>
public static string ExecutingDirectory => Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location);

/// <summary>Deserialize byte array to object.
/// </summary>
/// <param name="bytes">
/// The bytes.
/// </param>
/// <returns>Deserialised object.
/// <returns>Deserialized object.
/// </returns>
public static object BinaryDeserialize(this byte[] bytes)
{
Expand All @@ -46,7 +49,7 @@ public static object BinaryDeserialize(this byte[] bytes)
}

/// <summary>
/// Deserialise byte array to object.
/// Deserialize byte array to object.
/// </summary>
/// <param name="bytes">
/// The bytes.
Expand All @@ -55,7 +58,7 @@ public static object BinaryDeserialize(this byte[] bytes)
/// The binder.
/// </param>
/// <returns>
/// Deserialised object.
/// Deserialized object.
/// </returns>
public static object BinaryDeserialize(this byte[] bytes, SerializationBinder binder)
{
Expand All @@ -65,20 +68,19 @@ public static object BinaryDeserialize(this byte[] bytes, SerializationBinder bi
}

var formatter = new BinaryFormatter { Binder = binder };

using (var stream = new MemoryStream(bytes))
{
return formatter.Deserialize(stream);
}
}

/// <summary>
/// Convert an object to it's binary serialised form.
/// Convert an object to it's binary serialized form.
/// </summary>
/// <param name="o">
/// Object to serialise.
/// Object to serialize.
/// </param>
/// <returns>Serialised object.
/// <returns>Serialized object.
/// </returns>
public static byte[] BinarySerialize(this object o)
{
Expand Down Expand Up @@ -219,11 +221,6 @@ public static string ToByteDisplay(this long byteCount)
return size;
}

/// <summary>
/// Gets ExecutingDirectory.
/// </summary>
public static string ExecutingDirectory => Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location);

/// <summary>
/// Check if a dictionary has a value for a key.
/// </summary>
Expand Down Expand Up @@ -310,20 +307,20 @@ public static void AddRange<T>(this IList<T> list, IEnumerable<T> values)
/// <param name="source">IQueryable to page.</param>
/// <param name="page">Page number (begins at 1).</param>
/// <param name="pageSize">Number of items per page.</param>
/// <returns></returns>
/// <returns>Paged LINQ to SQL.</returns>
public static IQueryable<TSource> Page<TSource>(this IQueryable<TSource> source, int page, int pageSize)
{
return source.Skip((page - 1) * pageSize).Take(pageSize);
}

/// <summary>
/// Paging for LINQ
/// Paging for LINQ.
/// </summary>
/// <typeparam name="TSource">IEnumerable of 'object type' to page.</typeparam>
/// <param name="source">IEnumerable to page.</param>
/// <param name="page">Page number (begins at 1).</param>
/// <param name="pageSize">Number of items per page.</param>
/// <returns></returns>
/// <returns>Paged LINQ.</returns>
public static IEnumerable<TSource> Page<TSource>(this IEnumerable<TSource> source, int page, int pageSize)
{
return source.Skip((page - 1) * pageSize).Take(pageSize);
Expand All @@ -336,7 +333,7 @@ public static IEnumerable<TSource> Page<TSource>(this IEnumerable<TSource> sourc
/// <param name="value">IQueryable to page.</param>
/// <param name="startIndex">Index of first item to return.</param>
/// <param name="length">number of items to return.</param>
/// <returns></returns>
/// <returns>Paged IQueryable object.</returns>
public static IQueryable<T> PageByIndex<T>(this IQueryable<T> value, int? startIndex, int? length)
{
if (startIndex != null)
Expand Down Expand Up @@ -402,99 +399,20 @@ public static Expression<Func<T, bool>> False<T>()
return f => false;
}

public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
public static Expression<Func<T, bool>> Or<T>(
this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
return Expression.Lambda<Func<T, bool>>(Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
}

public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
public static Expression<Func<T, bool>> And<T>(
this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
}
}

public static class ProcessExtensions
{
#if DEBUG

/// <summary>
/// A utility class to determine a process parent.
/// http://stackoverflow.com/a/3346055
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct ParentProcessUtilities
{
// These members must match PROCESS_BASIC_INFORMATION
internal IntPtr Reserved1;

internal IntPtr PebBaseAddress;

internal IntPtr Reserved2_0;

internal IntPtr Reserved2_1;

internal IntPtr UniqueProcessId;

internal IntPtr InheritedFromUniqueProcessId;

[DllImport("ntdll.dll")]
private static extern int NtQueryInformationProcess(
IntPtr processHandle,
int processInformationClass,
ref ParentProcessUtilities processInformation,
int processInformationLength,
out int returnLength);

/// <summary>
/// Gets the parent process of the current process.
/// </summary>
/// <returns>An instance of the Process class.</returns>
public static Process GetParentProcess()
{
return GetParentProcess(Process.GetCurrentProcess().Handle);
}

/// <summary>
/// Gets the parent process of specified process.
/// </summary>
/// <param name="id">The process id.</param>
/// <returns>An instance of the Process class.</returns>
public static Process GetParentProcess(int id)
{
Process process = Process.GetProcessById(id);
return GetParentProcess(process.Handle);
}

/// <summary>
/// Gets the parent process of a specified process.
/// </summary>
/// <param name="handle">The process handle.</param>
/// <returns>An instance of the Process class.</returns>
public static Process GetParentProcess(IntPtr handle)
{
ParentProcessUtilities pbi = new ParentProcessUtilities();
int status = NtQueryInformationProcess(handle, 0, ref pbi, Marshal.SizeOf(pbi), out var returnLength);
if (status != 0)
{
throw new Win32Exception(status);
}

try
{
return Process.GetProcessById(pbi.InheritedFromUniqueProcessId.ToInt32());
}
catch (ArgumentException)
{
// not found
return null;
}
}
}
#endif
}
}
Loading

0 comments on commit 61d28f6

Please sign in to comment.