Skip to content

Provides a set of extensions which facilitates the exchange of information from and to .NET P/Invoke methods (or UnmanagedCallersOnly using NativeAOT).

License

Notifications You must be signed in to change notification settings

josephmoresena/Rxmxnx.PInvoke.Extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Quality Gate Status Bugs Coverage Lines of Code Reliability Rating Maintainability Rating Security Rating NuGet fuget.org


Table of Contents


Description

Rxmxnx.PInvoke.Extensions is a comprehensive library designed to streamline and enhance the interaction between .NET and native P/Invoke methods.

Features

  • UTF-8/ASCII String Handling: Seamlessly work with UTF-8 encoded strings in interop contexts.
  • Managed Buffers: Dynamically allocate object references on the stack with minimal effort.
  • Safe Memory Manipulation: Eliminate direct pointer manipulation and unsafe code requirements.

Getting Started

Installation

Install the library via NuGet:

dotnet add package Rxmxnx.PInvoke.Extensions

Note: This package currently supports .NET 6 and higher. Ensure your project targets a compatible framework before installing.


Abstractions

Rxmxnx.PInvoke.Extensions provides abstractions for managed handling of references and fixed memory segments.

Reference Interfaces

These interfaces represent a safe way to access a managed reference of a specific type.

IReadOnlyReferenceable<T>

This interface exposes a read-only reference to an object of type T, allowing the object to be used without modification.

Note: This interface inherits from IEquatable<T>. This type allows public implementation or inheritance.

Properties:

  • Reference

    Gets the read-only reference to the instance of an object of type T.

IReferenceable<T>

This interface exposes a reference to an object of type T, allowing the object to be used and potentially modified.

Note: This interface inherits from IReadOnlyReferenceable<T>. This type allows public implementation or inheritance.

Properties:

  • Reference

    Gets the reference to the instance of an object of type T.

Wrapper Interfaces

These interfaces represent a safe way to access a value or managed object of a specific type.

IWrapper<T>

This interface defines a wrapper for a T object.

Note: This interface inherits from IEquatable<T>. This type allows public implementation or inheritance.

Properties:

  • Value

    The wrapped T object.

Static Methods:

  • Create(T?)

    Creates a new instance of an object that implements IWrapper<T> interface.

Non-generic interface

IWrapper is a non-generic interface that exposes static methods for creating specific types of IWrapper<T> for concrete cases of value types, nullable values, and non-nullable reference types.

Static Methods:
  • Create<TValue>(TValue)

    Creates a new instance of an object that implements IWrapper<TValue> interface.

    Note: TValue generic type is struct.

  • CreateNullable<TValue>(TValue?)

    Creates a new instance of an object that implements IWrapper<TValue?> interface.

    Note: TValue generic type is struct.

  • CreateObject<TObject>(TObject)

    Creates a new instance of an object that implements IWrapper<TObject> interface.

    Note: TObject generic type is a reference type.

IMutableWrapper<T>

This interface defines a wrapper for an object whose value can be modified.

Note: This interface inherits from IWrapper<T>. This type allows public implementation or inheritance.

Properties:

  • Value

    The wrapped T object.

Static Methods:

  • Create(T?)

    Creates a new instance of an object that implements IMutableWrapper<T> interface.

Non-generic interface

IMutableWrapper is a non-generic interface that exposes static methods for creating specific types of IMutableWrapper<T> for concrete cases of value types, nullable values, and non-nullable reference types.

Static Methods:
  • Create<TValue>(TValue)

    Creates a new instance of an object that implements IMutableWrapper<TValue> interface.

    Note: TValue generic type is struct.

  • CreateNullable<TValue>(TValue?)

    Creates a new instance of an object that implements IMutableWrapper<TValue?> interface.

    Note: TValue generic type is struct.

  • CreateObject<TObject>(TObject)

    Creates a new instance of an object that implements IMutableWrapper<TObject> interface.

    Note: TObject generic type is a reference type.

IMutableReference<T>

This interface exposes a wrapper for T object that can be referenced and whose value can be modified.

Note: This interface inherits from IMutableWrapper<T> and IReferenceable<T>. This type allows public implementation or inheritance.

Properties:

  • Reference

    Reference to T wrapped instance.

Static Methods:

  • Create(T?)

    Creates a new instance of an object that implements IMutableReference<T> interface.

Non-generic interface

IMutableReference is a non-generic interface that exposes static methods for creating specific types of IMutableReference<T> for concrete cases of value types, nullable values, and non-nullable reference types.

Static Methods:
  • Create<TValue>(TValue)

    Creates a new instance of an object that implements IMutableReference<TValue> interface.

    Note: TValue generic type is struct.

  • CreateNullable<TValue>(TValue?)

    Creates a new instance of an object that implements IMutableReference<TValue?> interface.

    Note: TValue generic type is struct.

  • CreateObject<TObject>(TObject)

    Creates a new instance of an object that implements IMutableReference<TObject> interface.

    Note: TObject generic type is a reference type.

Fixed memory Interfaces

These interfaces represent a safe way to access a fixed address of native or managed memory.

IFixedPointer

Interface representing a pointer to a fixed block of memory.

Note: This type allows public implementation or inheritance.

Properties:

  • Pointer Gets the pointer to the fixed block of memory.

Disposable interface

IFixedPointer.IDisposable representing a disposable IFixedPointer object. This interface is used for managing fixed memory blocks that require explicit resource cleanup.

Note: This interface inherits from IFixedPointer and System.IDisposable. This type allows public implementation or inheritance.

IFixedMethod<TMethod>

Interface representing a method whose memory address is fixed in memory.

Note: This interface inherits from IFixedPointer. This type allows public implementation or inheritance.

Properties:

  • Method Gets the delegate that points to the fixed method in memory.
IReadOnlyFixedMemory

Interface representing a read-only fixed block of memory.

Note: This interface inherits from IFixedPointer. This type allows public implementation or inheritance.

Properties:

  • Bytes Gets a read-only binary span over the fixed block of memory.
  • Objects Gets a read-only object span over the fixed block of memory.

Methods:

  • AsBinaryContext()

    Creates a new instance of IReadOnlyFixedContext<Byte> from the current instance.

  • AsObjectContext()

    Creates a new instance of IReadOnlyFixedContext<Object> from the current instance.

Disposable interface

IReadOnlyFixedMemory.IDisposable representing a disposable IReadOnlyFixedMemory object. This interface is used for managing fixed memory blocks that require explicit resource cleanup.

Note: This interface inherits from IReadOnlyFixedMemory and IFixedPointer.IDisposable. This type allows public implementation or inheritance.

IReadOnlyFixedMemory<T>

Interface representing a read-only fixed block of memory for a specific type.

Note: This interface inherits from IReadOnlyFixedMemory. This type allows public implementation or inheritance.

Properties:

  • ValuePointer Gets the value pointer to the read-only fixed block of memory.
  • Values

    Gets a read-only T span over the fixed block of memory.

Disposable interface

IReadOnlyFixedMemory<T>.IDisposable representing a disposable IReadOnlyFixedMemory<T> object. This interface is used for managing fixed memory blocks that require explicit resource cleanup.

Note: This interface inherits from IReadOnlyFixedMemory<T> and IReadOnlyFixedMemory.IDisposable. This type allows public implementation or inheritance.

IFixedMemory

Interface representing a fixed block of memory.

Note: This interface inherits from IReadOnlyFixedMemory. This type allows public implementation or inheritance.

Properties:

  • Bytes Gets a binary span over the fixed block of memory.
  • Objects Gets an object span over the fixed block of memory.

Methods:

  • AsBinaryContext()

    Creates a new instance of IFixedContext<Byte> from the current instance.

  • AsObjectContext()

    Creates a new instance of IFixedContext<Object> from the current instance.

Disposable interface

IFixedMemory.IDisposable representing a disposable IFixedMemory object. This interface is used for managing fixed memory blocks that require explicit resource cleanup.

Note: This interface inherits from IFixedMemory and IReadOnlyFixedMemory.IDisposable. This type allows public implementation or inheritance.

IFixedMemory<T>

Interface representing a fixed block of memory for a specific type.

Note: This interface inherits from IReadOnlyFixedMemory<T> and IFixedMemory<T>. This type allows public implementation or inheritance.

Properties:

  • ValuePointer Gets the value pointer to the fixed block of memory.
  • Values

    Gets a T span over the fixed block of memory.

Disposable interface

IFixedMemory<T>.IDisposable representing a disposable IFixedMemory<T> object. This interface is used for managing fixed memory blocks that require explicit resource cleanup.

Note: This interface inherits from IReadOnlyFixedMemory<T> and IFixedMemory.IDisposable. This type allows public implementation or inheritance.

IReadOnlyFixedReference<T>

This interface exposes a read-only reference to an object of type T, allowing the object to be used without modification.

Note: This interface inherits from IReadOnlyReferenceable<T> and IReadOnlyFixedMemory. This type allows public implementation or inheritance.

Methods:

  • Transformation(out IReadOnlyFixedMemory)

    Reinterprets the read-only T fixed memory reference as a read-only TDestination memory reference.

Disposable interface

IReadOnlyFixedReference<T>.IDisposable representing a disposable IReadOnlyFixedReference<T> object. This interface is used for managing fixed memory blocks that require explicit resource cleanup.

Note: This interface inherits from IReadOnlyFixedReference<T> and IReadOnlyFixedMemory.IDisposable. This type allows public implementation or inheritance.

IFixedReference<T>

This interface represents a mutable reference to a fixed memory location.

Note: This interface inherits from IReferenceable<T>, IReadOnlyFixedReference<T> and IFixedMemory. This type allows public implementation or inheritance.

Methods:

  • Transformation(out IFixedMemory)

    Reinterprets the T fixed memory reference as a TDestination memory reference.

  • Transformation(out IReadOnlyFixedMemory)

    Reinterprets the T fixed memory reference as a TDestination memory reference.

Disposable interface

IFixedReference<T>.IDisposable representing a disposable IFixedReference<T> object. This interface is used for managing fixed memory blocks that require explicit resource cleanup.

Note: This interface inherits from IFixedReference<T> and IReadOnlyFixedReference<T>.IDisposable. This type allows public implementation or inheritance.

IReadOnlyFixedContext<T>

Interface representing a context from a read-only block of fixed memory.

Note: This interface inherits from IReadOnlyFixedMemory<T>. This type allows public implementation or inheritance.

Methods:

  • Transformation(out IReadOnlyFixedMemory)

    Reinterprets the T fixed memory block as TDestination memory block.

Disposable interface

IReadOnlyFixedContext<T>.IDisposable representing a disposable IReadOnlyFixedContext<T> object. This interface is used for managing fixed memory blocks that require explicit resource cleanup.

Note: This interface inherits from IReadOnlyFixedContext<T> and IReadOnlyFixedMemory<T>.IDisposable. This type allows public implementation or inheritance.

IFixedContext<T>

Interface representing a context from a block of fixed memory.

Note: This interface inherits from IReadOnlyFixedContext<T> and IFixedMemory<T>. This type allows public implementation or inheritance.

Methods:

  • Transformation(out IFixedMemory)

    Reinterprets the T fixed memory block as a TDestination memory block.

  • Transformation(out IReadOnlyFixedMemory) Reinterprets the `T` fixed memory block as a `TDestination` memory block.

Disposable interface

IFixedContext<T>.IDisposable representing a disposable IFixedContext<T> object. This interface is used for managing fixed memory blocks that require explicit resource cleanup.

Note: This interface inherits from IFixedContext<T> and IFixedMemory<T>.IDisposable. This type allows public implementation or inheritance.

Functional Interfaces

These interfaces expose functionalities for internal types or default functional implementations.

IEnumerableSequence<T>

Defines methods to support a simple iteration over a sequence of a specified type.

Note: This interface inherits from IEnumerable<T>. This type allows public implementation or inheritance.

Methods:

  • GetItem(Int32) Retrieves the element at the specified index.
  • GetSize() Retrieves the total number of elements in the sequence.

Protected Methods:

  • DisposeEnumeration(Int32)

    Method to call when IEnumerator<T> is disposing.

IUtf8FunctionState<TSelf>

Interface representing a value state for functional CString creation.

Note: TSelf generic type is struct. This type allows public implementation or inheritance.

Properties:

  • IsNullTerminated Indicates whether resulting UTF-8 text is null-terminated.

Static Abstract/Virtual Methods:

  • GetSpan(TSelf) Retrieves the span from state.
  • GetLength(in TSelf) Retrieves the span length from state.
IManagedBuffer<T>

This interfaces exposes a managed buffer.

Note: This type not allow public implementation or inheritance.

Static Methods:

  • GetMetadata<TBuffer>()

    Retrieves the BufferTypeMetadata<T> instance from TBuffer.

IManagedBinaryBuffer<T>

This interfaces exposes a binary managed buffer.

Note: This interface inherits from IManagedBuffer<T>. This type not allow public implementation or inheritance.

Properties:

  • Metadata<TBuffer>()

    Retrieves the BufferTypeMetadata<T> instance from current instance.

IManagedBinaryBuffer<TBuffer, T>

This interfaces exposes a binary managed buffer.

Note: TBuffer generic type is struct. This interface inherits from IManagedBinaryBuffer<T>. This type not allow public implementation or inheritance.

Span Delegates

These delegates encapsulate methods that operate with Span instances.

ReadOnlySpan<T> ReadOnlySpanFunc<T>()

Encapsulates a method that has no parameters and returns a read-only span of type T.

ReadOnlySpan<T> ReadOnlySpanFunc<T, in TState>(TState)

Encapsulates a method that has a TState parameter and returns a read-only span of type T.

Note: In .NET 9.0+ TState allows ref struct.

TResult SpanFunc<T, in TArg, out TResult>(Span<T>, TArg)

Encapsulates a method that receives a span of type T, a state object of type TArg and returns a result of type TResult.

Note: In .NET 9.0+ TState allows ref struct.

TResult ReadOnlySpanFunc<T, in TArg, out TResult>(Span<T>, TArg)

Encapsulates a method that receives a read-only span of type T, a state object of type TArg and returns a result of type TResult.

Note: In .NET 9.0+ TState allows ref struct.

Fixed memory Delegates

These delegates encapsulate methods that operate with fixed memory blocks.

void FixedAction(in IFixedMemory)

Represents an action that operates on a fixed memory instance.

void FixedAction<in TArg>(in IFixedMemory, TArg)

Represents an action that operates on a fixed memory instance using an additional state object.

Note: In .NET 9.0+ TState allows ref struct.

void ReadOnlyFixedAction(in IReadOnlyFixedMemory)

Represents an action that operates on a read-only fixed memory instance.

void ReadOnlyFixedAction<in TArg>(in IReadOnlyFixedMemory, TArg)

Represents an action that operates on a read-only fixed memory instance using an additional state object.

Note: In .NET 9.0+ TState allows ref struct.

TResult FixedFunc<out TResult>(in IFixedMemory)

Represents a function that operates on a fixed memory instance.

TResult FixedFunc<in TArg, out TResult>(in IFixedMemory, TArg)

Represents a function that operates on a fixed memory instance using an additional state object.

Note: In .NET 9.0+ TState allows ref struct.

TResult ReadOnlyFixedFunc<out TResult>(in IReadOnlyFixedMemory<T>)

Represents a function that operates on a read-only fixed memory instance.

TResult ReadOnlyFixedFunc<in TArg, out TResult>(in IReadOnlyFixedMemory<T>, TArg)

Represents a function that operates on a read-only fixed memory instance using an additional state object.

Note: In .NET 9.0+ TState allows ref struct.

Fixed memory context Delegates

These delegates encapsulate methods that operate on fixed memory blocks of a specific type.

void FixedContextAction<T>(in IFixedContext<T>)

Encapsulates a method that receives an instance of IFixedContext<T>.

void FixedContextAction<T, in TArg,>(in IFixedContext<T>, TArg)

Encapsulates a method that receives an instance of IFixedContext<T> and a state object of type TArg.

Note: In .NET 9.0+ TState allows ref struct.

void ReadOnlyFixedContextAction<T>(in IReadOnlyFixedContext<T>)

Encapsulates a method that receives an instance of IReadOnlyFixedContext<T>.

void ReadOnlyFixedContextAction<T, in TArg,>(in IFixedContext<T>, TArg)

Encapsulates a method that receives an instance of IReadOnlyFixedContext<T> and a state object of type TArg.

Note: In .NET 9.0+ TState allows ref struct.

TResult FixedContextFunc<T, out TResult>(in IFixedContext<T>)

Encapsulates a method that receives an instance of IFixedContext<T> and returns a value of type TResult.

TResult FixedContextFunc<T, in TArg, out TResult>(in IFixedContext<T>, TArg)

Encapsulates a method that receives an instance of IFixedContext<T> and returns a value of type TResult.

Note: In .NET 9.0+ TState allows ref struct.

TResult ReadOnlyFixedContextFunc<T, out TResult>(in IReadOnlyFixedContext<T>)

Encapsulates a method that receives an instance of IReadOnlyFixedContext<T> and returns a value of type TResult.

TResult FixedContextFunc<T, in TArg, out TResult>(in IReadOnlyFixedContext<T>, TArg)

Encapsulates a method that receives an instance of IReadOnlyFixedContext<T> and returns a value of type TResult.

Note: In .NET 9.0+ TState allows ref struct.

Fixed memory reference Delegates

These delegates encapsulate methods that operate on fixed memory references of a specific type.

void FixedReferenceAction<T>(in IFixedReference<T>)

Encapsulates a method that receives an instance of IFixedReference<T>.

void FixedReferenceAction<T, in TArg,>(in IFixedReference<T>, TArg)

Encapsulates a method that receives an instance of IFixedReference<T> and a state object of type TArg.

Note: In .NET 9.0+ TState allows ref struct.

void ReadOnlyFixedReferenceAction<T>(in IReadOnlyFixedReference<T>)

Encapsulates a method that receives an instance of IReadOnlyFixedReference<T>.

void ReadOnlyFixedReferenceAction<T, in TArg,>(in IFixedReference<T>, TArg)

Encapsulates a method that receives an instance of IReadOnlyFixedReference<T> and a state object of type TArg.

Note: In .NET 9.0+ TState allows ref struct.

TResult FixedReferenceFunc<T, out TResult>(in IFixedReference<T>)

Encapsulates a method that receives an instance of IFixedReference<T> and returns a value of type TResult.

TResult FixedReferenceFunc<T, in TArg, out TResult>(in IFixedReference<T>, TArg)

Encapsulates a method that receives an instance of IFixedReference<T> and returns a value of type TResult.

Note: In .NET 9.0+ TState allows ref struct.

TResult ReadOnlyFixedReferenceFunc<T, out TResult>(in IReadOnlyFixedReference<T>)

Encapsulates a method that receives an instance of IReadOnlyFixedReference<T> and returns a value of type TResult.

TResult FixedReferenceFunc<T, in TArg, out TResult>(in IReadOnlyFixedReference<T>, TArg)

Encapsulates a method that receives an instance of IReadOnlyFixedReference<T> and returns a value of type TResult.

Note: In .NET 9.0+ TState allows ref struct.

Fixed address method Delegates

These delegates encapsulate methods that operate on methods with a fixed memory address.

void FixedMethodAction<T>(in IFixedMethod<T>)

Encapsulates a method that receives an instance of IFixedMethod<T>.

void FixedMethodAction<T, in TArg,>(in IFixedMethod<T>, TArg)

Encapsulates a method that receives an instance of IFixedMethod<T> and a state object of type TArg.

Note: In .NET 9.0+ TState allows ref struct.

TResult FixedMethodFunc<T, out TResult>(in IFixedMethod<T>)

Encapsulates a method that receives an instance of IFixedMethod<T> and returns a value of type TResult.

TResult FixedMethodFunc<T, in TArg, out TResult>(in IFixedMethod<T>, TArg)

Encapsulates a method that receives an instance of IFixedMethod<T> and returns a value of type TResult.

Note: In .NET 9.0+ TState allows ref struct.

Fixed memory list Delegates

These delegates encapsulate methods that operate on a list of fixed memory blocks.

void FixedListAction(FixedMemoryList)

Encapsulates a method that receives an instance of FixedMemoryList.

void FixedListAction<in TArg,>(FixedMemoryList, TArg)

Encapsulates a method that receives an instance of FixedMemoryList and a state object of type TArg.

Note: In .NET 9.0+ TState allows ref struct.

void ReadOnlyFixedListAction(ReadOnlyFixedMemoryList)

Encapsulates a method that receives an instance of ReadOnlyFixedMemoryList.

void ReadOnlyFixedListAction<in TArg,>(FixedMemoryList, TArg)

Encapsulates a method that receives an instance of ReadOnlyFixedMemoryList and a state object of type TArg.

Note: In .NET 9.0+ TState allows ref struct.

TResult FixedListFunc<out TResult>(FixedMemoryList)

Encapsulates a method that receives an instance of FixedMemoryList and returns a value of type TResult.

TResult FixedListFunc<in TArg, out TResult>(FixedMemoryList, TArg)

Encapsulates a method that receives an instance of FixedMemoryList and returns a value of type TResult.

Note: In .NET 9.0+ TState allows ref struct.

TResult ReadOnlyFixedListFunc<out TResult>(ReadOnlyFixedMemoryList)

Encapsulates a method that receives an instance of ReadOnlyFixedMemoryList and returns a value of type TResult.

TResult FixedListFunc<in TArg, out TResult>(ReadOnlyFixedMemoryList, TArg)

Encapsulates a method that receives an instance of ReadOnlyFixedMemoryList and returns a value of type TResult.

Note: In .NET 9.0+ TState allows ref struct.

UTF-8/ASCII Delegates

These delegates encapsulate methods that allow creating and operating with UTF-8/ASCII text.

void CStringSequenceCreationAction<in TArg>(Span<Byte>, Int32, TArg)

Encapsulates a method that receives a span of bytes, an index and a state object of type TArg.

Note: In .NET 9.0+ TState allows ref struct.

void CStringSequenceAction(FixedCStringSequence)

Encapsulates a method that operates on a FixedCStringSequence instance.

Note: In .NET 9.0+ TState allows ref struct.

void CStringSequenceAction<in TArg>(FixedCStringSequence, TArg)

Encapsulates a method that operates on a FixedCStringSequence instance and a state object of type TArg.

Note: In .NET 9.0+ TState allows ref struct.

TResult CStringSequenceFunc<out TResult>(FixedCStringSequence)

Encapsulates a method that operates on a FixedCStringSequence instance and returns a value of type TResult.

TResult CStringSequenceFunc<in TArg, out TResult>(FixedCStringSequence, TArg)

Encapsulates a method that operates on a FixedCStringSequence instance, a state object of type TArg and returns a value of type TResult.

Note: In .NET 9.0+ TState allows ref struct.

Buffers Delegates

These delegates encapsulate methods that operate on instances of managed buffers.

void ScopedBufferAction<T>(ScopedBuffer)

Encapsulates a method that receives a buffer of objects of type T.

Note: In .NET 9.0+ TState allows ref struct.

void ScopedBufferAction<T, in TArg>(ScopedBuffer, TArg)

Encapsulates a method that receives a buffer of objects of type T and a state object of type TArg.

Note: In .NET 9.0+ TState allows ref struct.

TResult ScopedBufferFunc<T, out TResult>(ScopedBuffer)

Encapsulates a function that receives a buffer of objects of type T and returns a result of type TResult.

TResult ScopedBufferFunc<T, in TArg, out TResult>(ScopedBuffer, TArg)

Encapsulates a method that receives a buffer of objects of type T, a state object of type TArg and returns a value of type TResult.

Note: In .NET 9.0+ TState allows ref struct.


Structs

Rxmxnx.PInvoke.Extensions provides value types for the managed handling of references, fixed memory segments, and stack-allocated buffers

Pointers

These structures safely represent memory addresses for references or methods of a specific type.

ReadOnlyValPtr<T>

Represents a platform-specific type used to manage a pointer to a read-only value of type T.

Note: This struct implements IComparable, ISpanFormattable, ISerializable, IWrapper<IntPtr>, IEquatable<ReadOnlyValPtr<T>> and IComparable<ReadOnlyValPtr<T>> interfaces.

Static Properties:

  • Zero A read-only field that represents a pointer that has been initialized to zero.
  • IsUnmanaged

    Indicates if T type is an unmanaged type.

Properties:

  • Pointer

    Internal pointer as an IntPtr.

  • IsZero

    Indicates whether the current pointer is null.

  • Reference A read-only reference to the value pointed to by this instance.

Methods:

  • GetUnsafeFixedReference(IDisposable?)

    Retrieves an unsafe IReadOnlyFixedReference<T>.IDisposable instance from current read-only reference pointer.

  • GetUnsafeFixedContext(IDisposable?)

    Retrieves an unsafe IReadOnlyFixedContext<T>.IDisposable instance from current read-only reference pointer.

ValPtr<T>

Represents a platform-specific type used to manage a pointer to a mutable value of type T.

Note: This struct implements IComparable, ISpanFormattable, ISerializable, IWrapper<IntPtr>, IEquatable<ValPtr<T>> and IComparable<ValPtr<T>> interfaces.

Static Properties:

  • Zero A read-only field that represents a pointer that has been initialized to zero.
  • IsUnmanaged

    Indicates if T type is an unmanaged type.

Properties:

  • Pointer

    Internal pointer as an IntPtr.

  • IsZero Indicates whether the current pointer is `null`.
  • Reference A reference to the value pointed to by this instance.

Methods:

  • GetUnsafeFixedReference(IDisposable?)

    Retrieves an unsafe IReadOnlyFixedReference<T>.IDisposable instance from current reference pointer.

  • GetUnsafeFixedContext(IDisposable?)

    Retrieves an unsafe IReadOnlyFixedContext<T>.IDisposable instance from current reference pointer.

FuncPtr<TDelegate>

Represents a platform-specific type used to handle a pointer to a method of type .

Note: This struct implements ISpanFormattable, ISerializable, IWrapper<IntPtr> and IEquatable<FuncPtr<T>> interfaces.

Static Properties:

  • Zero A read-only field representing a null-initialized function pointer.

Properties:

  • Pointer

    Internal pointer as an IntPtr.

  • IsZero Indicates whether the current pointer is `null`.
  • Invoke A managed delegate using the method address pointed to by this instance.

Fixed memory lists

These structures represent lists of fixed memory block addresses.

ReadOnlyFixedMemoryList

Represents a list of IReadOnlyFixedMemory instances.

Note: This type is a ref struct. Instances of current type can be used with foreach.

Properties:

  • Count Gets the total number of elements in the list.
  • IsEmpty Indicates whether the current list is empty.

Indexer:

  • Item

    Gets the IReadOnlyFixedMemory at the specified index.

Methods:

  • ToArray()

    Creates an array from the current ReadOnlyFixedMemoryList instance.

FixedMemoryList

Represents a list of IFixedMemory instances.

Note: This type is a ref struct. Instances of current type can be used with foreach.

Properties:

  • Count Gets the total number of elements in the list.
  • IsEmpty Indicates whether the current list is empty.

Indexer:

  • Item

    Gets the IFixedMemory at the specified index.

Methods:

  • ToArray()

    Creates an array from the current FixedMemoryList instance.

FixedCStringSequence

Represents a CStringSequence that is fixed in memory.

Note: This type is a ref struct. Instances of current type can be used with foreach.

Properties:

  • Values

    Gets the list of CString values in the sequence.

Indexer:

  • Item Gets the element at the given index in the sequence.

Methods:

  • ToArray()

    Creates an array of IReadOnlyFixedMemory instances from the current instance.

Managed buffer types

These structures represent managed buffers that can be stored on the stack.

Atomic<T>

Atomic binary buffer.

Note: This type represents the binary unit 20 space.

Composite<TBufferA, TBufferB,T>

Composite binary buffer.

Notes:

  • This type represents a composite binary buffer of size A + B.
  • TBufferA and TBufferB must be binary buffers.
  • TBufferB must always be a space of 2n.
  • TBufferA must be a buffer smaller than or equal to TBufferB.
  • If TBufferA and TBufferB are equal, the current type will represent a binary space of 2n + 1.
NonBinarySpace<TArray, T>

Non-binary buffer space.

Notes:

  • This type represents a non-binary buffer based on a custom structure TArray.
  • TArray and T must be compatible.
  • The size of this space is the amount of elements of type T can be stored in a TArray instance.

Classes

Rxmxnx.PInvoke.Extensions provides reference types for the managed handling of UTF8/ASCII texts, memory regions, and metadata for stack-allocated buffer management.

ValueRegion<T>

This class represents a region of memory that contains a sequence of T values.

Note: T must be unmanaged. This type not allow public inheritance.

Properties:

  • IsMemorySlice Indicates whether the current instance represents a subregion of a memory region.

Indexer:

  • Item Retrieves an item from the memory region at the specified zero-based index.

Methods:

  • ToArray() Copies the contents of this memory region into a new array.

Operators:

  • ReadOnlySpan<T>(ValueRegion<T>)

    Converts the value of the current ValueRegion<T> to its equivalent read-only span representation.

  • T[]?(ValueRegion<T>)

    Converts the value of the current ValueRegion<T> to its equivalent array representation.

Static Methods:

  • Create(T[])

    Creates a new ValueRegion<T> instance from an array of T values.

  • Create(IntPtr, Int32)

    Creates a new ValueRegion<T> instance from a pointer to a native memory region.

  • Create(ReadOnlySpanFunc<T>)

    Creates a new ValueRegion<T> instance from a ReadOnlySpanFunc<T> function.

  • Create<TState>(TState, ReadOnlySpanFunc<T, TState>)

    Creates a new ValueRegion<T> instance from a ReadOnlySpanFunc<T, TState> function and TState instance.

BufferTypeMetadata

Represents the metadata of a managed buffer type.

Note: Implements IEnumerableSequence<BufferTypeMetadata>. This type not allow public inheritance.

Properties:

  • IsBinary Indicates whether current type is binary space.
  • Size Buffer capacity.
  • IsBinary Number of components.
  • BufferType Buffer type.

Indexer:

  • Item Retrieves a component from current metadata at the specified zero-based index.

Generic class

BufferTypeMetadata<T> representing a generic BufferTypeMetadata object.

Note: Inherits from BufferTypeMetadata. This type not allow public inheritance.

CString

Represents a sequence of UTF-8 encoded characters.

Notes:

  • Implements ICloneable, IComparable, IComparable<CString>, IComparable<String>, IEquatable<CString>, IEquatable<String> and IEnumerableSequence<Byte>.
  • This type is sealed.
  • Instances of this type can be fixed but not pinned.
  • Instances of this type can be iterated using both IEnumerable<Byte> and ReadOnlySpan<Byte>.Enumerator.
  • Range operations can be used on instances of this type.
  • This type exposes APIs to Join, Concat and Compare CString instances.
  • This type exposes operators of comparison and equality of CString and String instances.
  • In .NET 9.0+ params is used with ReadOnlySpan<> arguments instead of [] arguments.

Static Fields:

  • Empty Represents an empty UTF-8 string. This field is read-only.
  • Zero Represents a null-pointer UTF-8 string. This field is read-only.

Properties:

  • IsNullTerminated

    Gets a value indicating whether the text in the current CString instance ends with a null-termination character.

  • IsReference

    Gets a value indicating whether the UTF-8 text is referenced by, and not contained within, the current CString instance.

  • IsSegmented

    Gets a value indicating whether the current CString instance is a segment (or slice) of another CString instance.

  • IsFunction

    Gets a value indicating whether the current CString instance is a function.

Indexer:

  • Item

    Gets the Byte value at a specified position in the current CString object.

Constructors:

  • CString(Byte, Int32)

    Initializes a new instance of the CString class to the value indicated by a specified UTF-8 character repeated a specified number of times.

  • CString(Byte, Byte, Int32)

    Initializes a new instance of the CString class to the value indicated by a specified UTF-8 sequence repeated a specified number of times.

  • CString(Byte, Byte, Byte, Int32)

    Initializes a new instance of the CString class to the value indicated by a specified UTF-8 sequence repeated a specified number of times.

  • CString(Byte, Byte, Byte, Byte, Int32)

    Initializes a new instance of the CString class to the value indicated by a specified UTF-8 sequence repeated a specified number of times.

  • CString(ReadOnlySpan<Byte>)

    Initializes a new instance of the CString class using the UTF-8 characters indicated in the specified read-only span.

  • CString(ReadOnlySpanFunc<Byte>)

    Initializes a new instance of the CString class that contains the UTF-8 string returned by the specified ReadOnlySpanFunc<Byte>.

Methods:

  • ToArray()

    Copies the UTF-8 text of the current CString instance into a new byte array.

  • AsSpan()

    Retrieves the UTF-8 units of the current CString as a read-only span of bytes.

  • ToHexString()

    Returns a String that represents the current UTF-8 text as a hexadecimal value.

Operators:

  • CString?(Byte[]?)

    Defines an implicit conversion of a given Byte array to CString.

  • CString?(String?)

    Defines an explicit conversion of a given String to CString.

  • ReadOnlySpan<Byte>(CString?)

    Defines an implicit conversion of a given CString to a read-only span of bytes.

Static Methods:

  • IsNullOrEmpty(CString?)

    Determines whether the specified CString is null or an empty UTF-8 string.

  • Create(ReadOnlySpan<Byte>)

    Creates a new instance of the CString class using the UTF-8 characters provided in the specified read-only span.

  • Create(ReadOnlySpanFunc<Byte>?)

    Creates a new instance of the CString class using the ReadOnlySpanFunc<Byte> delegate provided.

  • Create(Byte[]?)

    Creates a new instance of the CString class using the binary internal information provided.

  • CreateCreate<TState>(TState)

    Creates a new instance of the CString class using a TState instance.

  • CreateUnsafe(IntPtr, Int32, useFullLength)

    Creates a new instance of the CString class using the pointer to a UTF-8 character array and length provided.

CStringSequence

Represents a sequence of null-terminated UTF-8 text strings.

Notes:

  • Implements ICloneable, IEquatable<CString>, IComparable<CStringSequence>, IReadOnlyList<CString> and IEnumerableSequence<CString>.
  • This type is sealed.
  • The instances of this type can be fixed but not pinned.
  • Range operations can be used on instances of this type.
  • This type exposes constructors to create sequences from up to 8 instances of ReadOnlySpan<Byte> in order to optimize memory usage.
  • In .NET 9.0+ params is used with ReadOnlySpan<> arguments instead of [] arguments.

Static Properties:

  • Empty Represents an empty sequence.

Properties:

  • Count

    Gets the number of CString instances contained in this CStringSequence.

Indexer:

  • Item

    Gets the CString at the specified index.

Constructors:

  • CStringSequence(params String?[])

    Initializes a new instance of the CStringSequence class from a collection of strings.

  • CStringSequence(params CString?[])

    Initializes a new instance of the CStringSequence class from a collection of UTF-8 strings.

  • CStringSequence(ReadOnlySpan<CString>)

    Initializes a new instance of the CStringSequence class from a read-only span of UTF-8 strings.

  • CStringSequence(ReadOnlySpan<String>)

    Initializes a new instance of the CStringSequence class from a read-only span of strings.

  • CStringSequence(IReadOnlySpan<String?>)

    Initializes a new instance of the CStringSequence class from a collection of strings.

  • CStringSequence(IReadOnlySpan<CString?>)

    Initializes a new instance of the CStringSequence class from a collection of UTF-8 strings.

Methods:

  • ToCString()

    Returns a CString that represents the current sequence.

  • GetFixedPointer()

    Creates an IFixedPointer.IDisposable instance by pinning the current instance, allowing safe access to the fixed memory region.

  • WithSafeTransform(CStringSequenceAction)

    Executes a specified action using the current instance treated as a FixedCStringSequence.

  • WithSafeTransform<TState>(TState, CStringSequenceAction)

    Executes a specified action on the current instance treated as a FixedCStringSequence, using an additional parameter passed to the action.

  • WithSafeTransform<TResult>(CStringSequenceFunc<TResult>)

    Executes a specified function using the current instance treated as a FixedCStringSequence.

  • WithSafeTransform<TState, TResult>(TState, CStringSequenceFunc<TResult>)

    Executes a specified function using the current instance treated as a FixedCStringSequence, and an additional parameter passed to the function.

Operators:

  • CString?(Byte[]?)

    Defines an implicit conversion of a given Byte array to CString.

  • CString?(String?)

    Defines an explicit conversion of a given String to CString.

  • ReadOnlySpan<Byte>(CString?)

    Defines an implicit conversion of a given CString to a read-only span of bytes.

  • ReadOnlySpan<Byte>(CString?)

    Defines an implicit conversion of a given CString to a read-only span of bytes.

Static Methods:

  • Create<TState>(TState, CStringSequenceCreationAction<TState>, params Int32?[]) Creates a new UTF-8 text sequence with specific lengths, and initializes each UTF-8 text string in it after creation using the specified callback.
  • Create(ReadOnlySpan<Char>)

    Creates a new CStringSequence instance from a UTF-8 buffer.

  • Parse(String?)

    Converts the buffer of a UTF-8 sequence to a CStringSequence instance.


Extensions

Rxmxnx.PInvoke.Extensions provides static classes that expose APIs to facilitate the management of managed memory, pointer usage, and this package's internal resources.

BinaryExtensions

Set of useful methods when working with bytes, byte arrays, and byte spans in a PInvoke context.

  • ToValue<T>(this Byte[])

    Retrieves a T value from the given byte array.

    Note: ´T´ is ´unmanaged´.

  • ToValue<T>(this Span<Byte>)

    Retrieves a T value from the given byte span.

    Note: ´T´ is ´unmanaged´.

  • ToValue<T>(this ReadOnlySpan<Byte>)

    Retrieves a T value from the given read-only byte span.

    Note: ´T´ is ´unmanaged´.

  • AsValue<T>(this ReadOnlySpan<Byte>)

    Retrieves a read-only reference to a T value from the given read-only byte span.

    Note: ´T´ is ´unmanaged´.

  • AsValue<T>(this Span<Byte>)

    Retrieves a reference to a T value from the given byte span.

    Note: ´T´ is ´unmanaged´.

  • AsHexString(this Byte[]) Gets the hexadecimal string representation of a byte array.
  • AsHexString(this Byte) Gets the hexadecimal string representation of a byte.
  • WithSafeFixed(this Span<Byte>, FixedAction) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified action has completed.
  • WithSafeFixed(this Span<Byte>, ReadOnlyFixedAction) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified action has completed.
  • WithSafeFixed(this ReadOnlySpan<Byte>, ReadOnlyFixedAction) Prevents the garbage collector from relocating the current read-only span by pinning its memory address until the specified action has completed.
  • WithSafeFixed<TArg>(this Span<Byte>, FixedAction<TArg>) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified action has completed.
  • WithSafeFixed<TArg>(this Span<Byte>, ReadOnlyFixedAction<TArg>) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified action has completed.
  • WithSafeFixed<TArg>(this ReadOnlySpan<Byte>, ReadOnlyFixedAction<TArg>) Prevents the garbage collector from relocating the current read-only span by pinning its memory address until the specified action has completed.
  • WithSafeFixed<TResult>(this Span<Byte>, FixedFunc<TResult>) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified function has completed.
  • WithSafeFixed<TResult>(this Span<Byte>, ReadOnlyFixedFunc<TResult>) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified function has completed.
  • WithSafeFixed<TResult>(this ReadOnlySpan<Byte>, ReadOnlyFixedFunc<TResult>) Prevents the garbage collector from relocating the current read-only span by pinning its memory address until the specified function has completed.
  • WithSafeFixed<TArg, TResult>(this Span<Byte>, TArg, FixedFunc<TArg, TResult>) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified function has completed.
  • WithSafeFixed<TArg, TResult>(this Span<Byte>, TArg, ReadOnlyFixedFunc<TArg, TResult>) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified function has completed.
  • WithSafeFixed<TArg, TResult>(this ReadOnlySpan<Byte>, TArg, ReadOnlyFixedFunc<TArg, TResult>) Prevents the garbage collector from relocating the current read-only span by pinning its memory address until the specified function has completed.
DelegateExtensions

Additional functionality for working with delegates.

  • GetUnsafeFuncPtr<TDelegate>(this TDelegate)

    Creates a FuncPtr<TDelegate> from a memory reference to a TDelegate delegate instance.

  • GetUnsafeIntPtr<TDelegate>(this TDelegate)

    Retrieves an IntPtr from a memory reference to a TDelegate delegate instance.

  • GetUnsafeUIntPtr<TDelegate>(this TDelegate)

    Retrieves a UIntPtr from a memory reference to a TDelegate delegate instance.

  • WithSafeFixed<TDelegate>(this TDelegate, FixedMethodAction<TDelegate>) Prevents the garbage collector from relocating a delegate in memory and fixes its address while an action is being performed.
  • WithSafeFixed<TDelegate, TArg>(this TDelegate, TArg, FixedMethodAction<TDelegate, TArg>) Prevents the garbage collector from relocating a delegate in memory and fixes its address while an action is being performed, passing an additional argument to the action.
  • WithSafeFixed<TDelegate, TResult>(this TDelegate, TArg, FixedMethodFunc<TDelegate, TResult>)

    Prevents the garbage collector from relocating a delegate in memory, fixes its address, and invokes the function that returns a TResult value.

  • WithSafeFixed<TDelegate, TArg, TResult>(this TDelegate, TArg, FixedMethodFunc<TDelegate, TArg, TResult>)

    Prevents the garbage collector from relocating a delegate in memory, fixes its address, invokes the function that returns a TResult value, passing an additional argument to the function.

MemoryBlockExtensions

Additional functionality for working with memory blocks.

  • GetUnsafeValPtr<T>(this Span<T>)

    Retrieves an unsafe ValPtr<T> pointer from Span<T> instance.

  • GetUnsafeValPtr<T>(this ReadOnlySpan<T>)

    Retrieves an unsafe ValPtr<T> pointer from ReadOnlySpan<T> instance.

  • GetUnsafePtr<T>(this Span<T>)

    Retrieves an unsafe IntPtr pointer from Span<TZ instance.

  • GetUnsafePtr<T>(this ReadOnlySpan<T>)

    Retrieves an unsafe IntPtr pointer from ReadOnlySpan<T> instance.

  • GetUnsafeUIntPtr<T>(this Span<T>)

    Retrieves an unsafe UIntPtr pointer from Span<T> instance.

  • GetUnsafeUIntPtr<T>(this ReadOnlySpan<T>)

    Retrieves an unsafe UIntPtr pointer from ReadOnlySpan<T> instance.

  • AsBytes<TSource>(this Span<TSource>)

    Reinterprets the span of TSource as a binary span.

    Note: TSource is unmanaged.

  • AsBytes<TSource>(this ReadOnlySpan<TSource>)

    Reinterprets the read-only span of TSource as a read-only binary span.

    Note: TSource is unmanaged.

  • AsValues<TSource, TDestination>(this Span<TSource>)

    Reinterprets the span of TSource as a span of TDestination.

    Note: TSource is unmanaged. TDestination is unmanaged.

  • AsValues<TSource, TDestination>(this ReadOnlySpan<TSource>)

    Reinterprets the read-only span of TSource as a read-only span of TDestination.

    Note: TSource is unmanaged. TDestination is unmanaged.

  • AsValues<TSource, TDestination>(this Span<TSource>, Span<Byte>)

    Reinterprets the span of TSource as a span of TDestination.

    Note: TSource is unmanaged. TDestination is unmanaged.

  • AsValues<TSource, TDestination>(this Span<TSource>, ReadOnlySpan<Byte>)

    Reinterprets the span of TSource as a read-only span of TDestination.

    Note: TSource is unmanaged. TDestination is unmanaged.

  • AsValues<TSource, TDestination>(this ReadOnlySpan<TSource>, ReadOnlySpan<Byte>)

    Reinterprets the read-only span of TSource as a read-only span of TDestination.

    Note: TSource is unmanaged. TDestination is unmanaged.

  • GetFixedContext<T>(this ReadOnlyMemory<T>)

    Creates an IReadOnlyFixedContext<T>.IDisposable instance by pinning the current ReadOnlyMemory<T> instance, ensuring a safe context for accessing the fixed memory.

    Note: T is unmanaged.

  • GetFixedContext<T>(this Memory<T>)

    Creates an IFixedContext<T>.IDisposable instance by pinning the current Memory<T> instance, ensuring a safe context for accessing the fixed memory.

    Note: T is unmanaged.

  • GetFixedMemory<T>(this ReadOnlyMemory<T>)

    Creates an IReadOnlyFixedMemory<T>.IDisposable instance by pinning the current ReadOnlyMemory<T> instance, ensuring a safe context for accessing the fixed memory.

    Note: T is unmanaged.

  • GetFixedMemory<T>(this Memory<T>)

    Creates an IFixedMemory<T>.IDisposable instance by pinning the current Memory<T> instance, ensuring a safe context for accessing the fixed memory.

    Note: T is unmanaged.

  • AsMemory<T>(this T[...]) Creates a new memory region over the target array.
  • AsSpan<T>(this T[...]) Creates a new span over a target array.
  • WithSafeFixed<T>(this Span<T>, FixedContextAction<T>) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified action has completed.
  • WithSafeFixed<T>(this Span<T>, ReadOnlyFixedContextAction<T>) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified action has completed.
  • WithSafeFixed<T>(this ReadOnlySpan<T>, ReadOnlyFixedContextAction<T>) Prevents the garbage collector from relocating the current read-only span by pinning its memory address until the specified action has completed.
  • WithSafeFixed<T, TArg>(this Span<T>, FixedContextAction<T, TArg>) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified action has completed.
  • WithSafeFixed<T, TArg>(this Span<T>, ReadOnlyFixedContextAction<T, TArg>) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified action has completed.
  • WithSafeFixed<T, TArg>(this ReadOnlySpan<T>, ReadOnlyFixedContextAction<T, TArg>) Prevents the garbage collector from relocating the current read-only span by pinning its memory address until the specified action has completed.
  • WithSafeFixed<T, TResult>(this Span<T>, FixedContextFunc<T, TResult>) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified function has completed.
  • WithSafeFixed<T, TResult>(this Span<T>, ReadOnlyFixedContextFunc<T, TResult>) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified function has completed.
  • WithSafeFixed<T, TResult>(this ReadOnlySpan<T>, ReadOnlyFixedContextFunc<T, TResult>) Prevents the garbage collector from relocating the current read-only span by pinning its memory address until the specified function has completed.
  • WithSafeFixed<T, TArg, TResult>(this Span<T>, TArg, FixedContextFunc<T, TArg, TResult>) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified function has completed.
  • WithSafeFixed<T, TArg, TResult>(this Span<T>, TArg, ReadOnlyFixedContextFunc<T, TArg, TResult>) Prevents the garbage collector from relocating the current span by pinning its memory address until the specified function has completed.
  • WithSafeFixed<T, TArg, TResult>(this ReadOnlySpan<T>, TArg, ReadOnlyFixedContextFunc<T, TArg, TResult>) Prevents the garbage collector from relocating the current read-only span by pinning its memory address until the specified function has completed.
PointerExtensions

Set of extensions for basic operations with IntPtr and UIntPtr instances.

  • IsZero(this IntPtr)

    Determines if the IntPtr instance is zero.

  • IsZero(this UIntPtr)

    Determines if the UIntPtr instance is zero.

  • ToUIntPtr(this IntPtr)

    Converts the specified IntPtr instance to a UIntPtr instance.

  • ToUIntPtr(this MemoryHandle) Converts the specified `MemoryHandle` instance to a `UIntPtr` instance.
  • ToIntPtr(this UIntPtr)

    Converts the specified UIntPtr instance to a IntPtr instance.

  • ToIntPtr(this MemoryHandle)

    Converts the specified MemoryHandle instance to a IntPtr instance.

  • GetUnsafeString(this IntPtr)

    Generates a String instance from the memory at the given IntPtr, interpreting the contents as UTF-16 text.

  • GetUnsafeString(this UIntPtr)

    Generates a String instance from the memory at the given UIntPtr, interpreting the contents as UTF-16 text.

  • GetUnsafeString(this MemoryHandle)

    Generates a String instance from the memory at the given MemoryHandle, interpreting the contents as UTF-16 text.

  • GetUnsafeString(this IntPtr, Int32)

    Generates a String instance from the memory at the given IntPtr, interpreting the contents as UTF-16 text.

  • GetUnsafeString(this UIntPtr, Int32)

    Generates a String instance from the memory at the given UIntPtr, interpreting the contents as UTF-16 text.

  • GetUnsafeString(this MemoryHandle, Int32)

    Generates a String instance from the memory at the given MemoryHandle, interpreting the contents as UTF-16 text.

  • GetUnsafeArray<T>(this IntPtr, Int32)

    Generates a T array by copying values from memory starting at the location referenced by an IntPtr.

    Note: ´T´ is ´unmanaged´.

  • GetUnsafeArray<T>(this UIntPtr, Int32)

    Generates a T array by copying values from memory starting at the location referenced by a UIntPtr.

    Note: ´T´ is ´unmanaged´.

  • GetUnsafeArray<T>(this MemoryHandle, Int32)

    Generates a T array by copying values from memory starting at the location referenced by a MemoryHandle.

    Note: ´T´ is ´unmanaged´.

  • GetUnsafeSpan<T>(this IntPtr, Int32)

    Generates a Span<T> instance from an IntPtr, interpreting the memory at the specified location as a sequence of unmanaged values.

    Note: ´T´ is ´unmanaged´.

  • GetUnsafeSpan<T>(this UIntPtr, Int32)

    Generates a Span<T> instance from a UIntPtr, interpreting the memory at the specified location as a sequence of unmanaged values.

    Note: ´T´ is ´unmanaged´.

  • GetUnsafeSpan<T>(this MemoryHandle, Int32)

    Generates a Span<T> instance from a MemoryHandle, interpreting the memory at the specified location as a sequence of unmanaged values.

    Note: ´T´ is ´unmanaged´.

  • GetUnsafeReadOnlySpan<T>(this IntPtr, Int32)

    Generates a ReadOnlySpan<T> instance from an IntPtr, interpreting the memory at the specified location as a sequence of unmanaged values.

    Note: ´T´ is ´unmanaged´.

  • GetUnsafeReadOnlySpan<T>(this UIntPtr, Int32)

    Generates a ReadOnlySpan<T> instance from a UIntPtr, interpreting the memory at the specified location as a sequence of unmanaged values.

    Note: ´T´ is ´unmanaged´.

  • GetUnsafeReadOnlySpan<T>(this MemoryHandle, Int32)

    Generates a ReadOnlySpan<T> instance from a MemoryHandle, interpreting the memory at the specified location as a sequence of unmanaged values.

    Note: ´T´ is ´unmanaged´.

  • GetUnsafeDelegate<TDelegate>(this IntPtr)

    Generates a delegate of type TDelegate from an IntPtr.

  • GetUnsafeDelegate<TDelegate>(this UIntPtr)

    Creates a delegate of type TDelegate from a UIntPtr.

  • GetUnsafeReference<T>(this IntPtr)

    Generates a memory reference to an unmanaged value of type T from IntPtr.

  • GetUnsafeReference<T>(this UIntPtr)

    Generates a memory reference to an unmanaged value of type T from UIntPtr.

  • GetUnsafeReadOnlyReference<T>(this IntPtr)

    Generates a read-only memory reference to an unmanaged value of type T from IntPtr.

  • GetUnsafeReadOnlyReference<T>(this UIntPtr)

    Generates a read-only memory reference to an unmanaged value of type T from UIntPtr.

PointerCStringExtensions

Set of extensions for CString operations with IntPtr and UIntPtr instances.

  • GetUnsafeCString(this IntPtr, Int32)

    Generates a CString instance using the memory reference pointed to by the given IntPtr, considering it as the start of a UTF-8 encoded string.

  • GetUnsafeCString(this UIntPtr, Int32)

    Generates a CString instance using the memory reference pointed to by the given UIntPtr, considering it as the start of a UTF-8 encoded string.

  • GetUnsafeCString(this MemoryHandle, Int32)

    Generates a CString instance using the memory reference pointed to by the given MemoryHandle, considering it as the start of a UTF-8 encoded string.

ReferenceExtensions

Set of extensions for basic operations with references to unmanaged values.

  • GetUnsafeValPtr<T>(ref this T)

    Obtains an unsafe pointer of type ValPtr<T> from a reference to an unmanaged value of type T.

  • GetUnsafeIntPtr<T>(ref this T)

    Obtains an unsafe pointer of type IntPtr from a reference to an unmanaged value of type T.

  • GetUnsafeUIntPtr<T>(ref this T)

    Obtains an unsafe pointer of type UIntPtr from a reference to an unmanaged value of type T.

  • Transform<TSource, TDestination>(ref this TSource)

    Generates a reference for an unmanaged value of type TDestination from an existing reference to an unmanaged value of type TSource.

  • AsBytes<TSource>(ref this TSource)

    Creates a Span<Byte> from a reference to an unmanaged value of type TSource.

  • WithSafeFixed<T>(ref this T, FixedReferenceAction<T>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided action.
  • WithSafeFixed<T>(ref this T, ReadOnlyFixedReferenceAction<T>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided read-only action.
  • WithSafeFixed<T, TArg>(ref this T, TArg, FixedReferenceAction<T, TArg>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided action along with an argument.
  • WithSafeFixed<T, TArg>(ref this T, TArg, ReadOnlyFixedReferenceAction<T, TArg>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided read-only action along with an argument.
  • WithSafeFixed<T, TResult>(ref this T, FixedReferenceFunc<T, TResult>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided function.
  • WithSafeFixed<T, TResult>(ref this T, ReadOnlyFixedReferenceFunc<T, TResult>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided read-only function.
  • WithSafeFixed<T, TArg, TResult>(ref this T, TArg, FixedReferenceFunc<T, TArg, TResult>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided function along with an argument.
  • WithSafeFixed<T, TArg, TResult>(ref this T, TArg, ReadOnlyFixedReferenceFunc<T, TArg, TResult>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided read-only function along with an argument.
StringExtensions

Set of extensions for basic operations with String instances.

  • WithSafeFixed(this String?, ReadOnlyFixedContextAction<Char>) Pins the current string to prevent the garbage collector from relocating its memory address during the execution of the specified action.
  • WithSafeFixed<TArg>(this String?, TArg, ReadOnlyFixedContextAction<Char, TArg>) Pins the current string to prevent the garbage collector from relocating its memory address during the execution of the specified action along with an argument.
  • WithSafeFixed<TResult>(this String?, ReadOnlyFixedContextFunc<Char, TResult>) Pins the current string to prevent the garbage collector from relocating its memory address during the execution of the specified function.
  • WithSafeFixed<TArg, TResult>(this String?, TArg, ReadOnlyFixedContextFunc<Char, TArg, TResult>) Pins the current string to prevent the garbage collector from relocating its memory address during the execution of the specified function along with an argument.
UnmanagedValueExtensions

Set of extensions for basic operations with unmanaged values.

  • ToBytes<T>(this T)

    Converts a given unmanaged value of type T into an array of Byte.

  • ToBytes<TSource>(this TSource[]?)

    Converts an array of unmanaged values of type TSource into an array of Byte.

  • ToValues<TSource, TDestination>(this TSource[]?)

    Converts an array of unmanaged values of type TSource into an array of another unmanaged value type TDestination.

  • ToValues<TSource, TDestination>(this TSource[]?, out Byte[]?)

    Converts an array of unmanaged values of type TSource into an array of another unmanaged value type TDestination and provides the residual binary array of the reinterpretation.

  • WithSafeFixed<T>(ref this T, FixedReferenceAction<T>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided action.
  • WithSafeFixed<T>(ref this T, ReadOnlyFixedReferenceAction<T>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided read-only action.
  • WithSafeFixed<T, TArg>(ref this T, TArg, FixedReferenceAction<T, TArg>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided action along with an argument.
  • WithSafeFixed<T, TArg>(ref this T, TArg, ReadOnlyFixedReferenceAction<T, TArg>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided read-only action along with an argument.
  • WithSafeFixed<T, TResult>(ref this T, FixedReferenceFunc<T, TResult>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided function.
  • WithSafeFixed<T, TResult>(ref this T, ReadOnlyFixedReferenceFunc<T, TResult>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided read-only function.
  • WithSafeFixed<T, TArg, TResult>(ref this T, TArg, FixedReferenceFunc<T, TArg, TResult>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided function along with an argument.
  • WithSafeFixed<T, TArg, TResult>(ref this T, TArg, ReadOnlyFixedReferenceFunc<T, TArg, TResult>) Temporarily fixes the location of a reference by preventing the garbage collector from moving it and executes a provided read-only function along with an argument.

Utilities

Rxmxnx.PInvoke.Extensions provides static classes that complement the APIs facilitating the management of read-only managed memory, enable interaction with the runtime environment, and allow the use of the package's internal resources.

AotInfo

Provides information about the Ahead-of-Time compilation.

Static Properties:

  • IsReflectionDisabled Indicates whether runtime reflection is disabled.
  • IsNativeAot Indicates whether the current runtime is NativeAOT.
BufferManager

This class allows to allocate buffers on stack if possible.

Static Properties:

  • BufferAutoCompositionEnabled Indicates whether metadata for any required buffer is auto-composed.

    Note: This property will always be false if compiled with IlcDisableReflection=true or if the PInvoke.DisableBufferAutoComposition feature switch is enabled.

Static Methods:

  • Alloc<T>(UInt16, ScopedBufferAction<T>, Boolean)

    Allocates a buffer with count elements and executes action.

  • Alloc<T, TState>(UInt16, TState, ScopedBufferAction<T, TState>, Boolean)

    Allocates a buffer with count elements and executes action.

  • Alloc<T, TResult>(UInt16, ScopedBufferFunc<T, TResult>, Boolean)

    Allocates a buffer with count elements and executes func.

  • Alloc<T, TState, TResult>(UInt16, TState, ScopedBufferFunc<T, TState, TResult>, Boolean)

    Allocates a buffer with count elements and executes func.

  • Register<TBuffer>() Registers object buffer.

    Note: TBuffer is struct and IManagedBuffer<Object>.

  • Register<T, TBuffer>()

    Registers T buffer.

    Note: T is struct. TBuffer is struct and IManagedBuffer<T>.

  • RegisterNullable<T, TBuffer>()

    Registers T? buffer.

    Note: T is struct. TBuffer is struct and IManagedBuffer<T?>.

NativeUtilities

Set of utilities for exchange data within the P/Invoke context.

Static Fields:

  • PointerSize Size in bytes of a memory pointer.

Static Methods:

  • SizeOf<T>()

    Gets the memory size of T structure.

    Note: T is unmanaged.

  • LoadNativeLib(String?, DllImportSearchPath?) Provides a high-level API for loading a native library.
  • LoadNativeLib(String?, ref EventHandler?, DllImportSearchPath?) Provides a high-level API for loading a native library.
  • GetNativeMethod<TDelegate>(IntPtr, String?) Gets the TDelegate delegate of an exported symbol.
  • GetUnsafeFuncPtr<TDelegate>(TDelegate)

    Creates an FuncPtr<TDelegate> from a memory reference to a TDelegate delegate instance.

  • GetUnsafeValPtr<T>(in T)

    Retrieves an unsafe ReadOnlyValPtr<T> pointer from a read-only reference to a T value.

  • GetUnsafeValPtrFromRef<T>(ref T)

    Retrieves an unsafe pointer of type ValPtr<T> from a reference to a value of type T.

  • GetUnsafeIntPtr<T>(in T)

    Retrieves an unsafe IntPtr pointer from a read-only reference to a T unmanaged value.

  • GetUnsafeUIntPtr<T>(in T)

    Retrieves an unsafe UIntPtr pointer from a read-only reference to a T unmanaged value.

  • Transform<TSource, TDestination>(in TSource)

    Transforms a read-only reference of an unmanaged value of type TSource into a read-only reference of an unmanaged value of type TDestination.

  • TransformReference<TSource, TDestination>(ref TSource)

    Transforms a reference of an unmanaged value of type TSource into a reference of an unmanaged value of type TDestination.

  • ToBytes<T>(in TSource)

    Retrieves a Byte array from a read-only reference to a TSource value.

    Note: TSource is unmanaged.

  • AsBytes<TSource>(in TSource)

    Creates a ReadOnlySpan<Byte> from an exising read-only reference to a TSource unmanaged value.

  • AsBinarySpan<TSource>(ref TSource)

    Creates a Span<Byte> from an exising reference to a TSource value.

    Note: TSource is unmanaged.

  • CreateArray<T, TState>(Int32, TState, SpanAction<T, TState>)

    Creates a new T array with a specific length and initializes it after creation by using the specified callback.

    Note: T is unmanaged.

  • CopyBytes<T>(in TSource, Span<Byte>, Int32)

    Performs a binary copy of the given TSource to the destination span.

    Note: TSource is unmanaged.

  • WithSafeReadOnlyFixed<T>(ref T, ReadOnlyFixedReferenceAction<T>) Prevents the garbage collector from relocating a given reference and fixes its memory address until action finishes.
  • WithSafeReadOnlyFixed<T, TArg>(ref T, TArg, ReadOnlyFixedReferenceAction<T, TArg>) Prevents the garbage collector from relocating a given reference and fixes its memory address until action finishes.
  • WithSafeReadOnlyFixed<T, TResult>(ref T, ReadOnlyFixedReferenceFunc<T, TResult>) Prevents the garbage collector from relocating a given reference and fixes its memory address until func finishes.
  • WithSafeReadOnlyFixed<T, TArg, TResult>(ref T, TArg, ReadOnlyFixedReferenceFunc<T, TArg, TResult>) Prevents the garbage collector from relocating a given reference and fixes its memory address until func finishes.
  • WithSafeFixed<T>(in T, ReadOnlyFixedReferenceAction<T>) Prevents the garbage collector from relocating a given read-only reference and fixes its memory address until action finishes.
  • WithSafeFixed<T>(ref T, FixedReferenceAction<T>) Prevents the garbage collector from relocating a given reference and fixes its memory address until action finishes.
  • WithSafeFixed<T, TArg>(in T, TArg, ReadOnlyFixedReferenceAction<T, TArg>) Prevents the garbage collector from relocating a given read-only reference and fixes its memory address until action finishes.
  • WithSafeFixed<T, TArg>(ref T, TArg, FixedReferenceAction<T, TArg>) Prevents the garbage collector from relocating a given reference and fixes its memory address until action finishes.
  • WithSafeFixed<T, TResult>(in T, ReadOnlyFixedReferenceFunc<T, TResult>) Prevents the garbage collector from relocating a given read-only reference and fixes its memory address until func finishes.
  • WithSafeFixed<T, TResult>(ref T, FixedReferenceFunc<T, TResult>) Prevents the garbage collector from relocating a given reference and fixes its memory address until func finishes.
  • WithSafeFixed<T, TArg, TResult>(in T, TArg, ReadOnlyFixedReferenceFunc<T, TArg, TResult>) Prevents the garbage collector from relocating a given read-only reference and fixes its memory address until func finishes.
  • WithSafeFixed<T, TArg, TResult>(ref T, TArg, FixedReferenceFunc<T, TArg, TResult>) Prevents the garbage collector from relocating a given reference and fixes its memory address until func finishes.
  • WithSafeFixed<TDelegate>(TDelegate, FixedMethodAction<TDelegate>) Prevents the garbage collector from relocating a given method delegate and fixes its memory address until action finishes.
  • WithSafeFixed<TDelegate, TArg>(TDelegate, TArg, FixedMethodAction<TDelegate, TArg>) Prevents the garbage collector from relocating a given method delegate and fixes its memory address until action finishes.
  • WithSafeFixed<TDelegate, TResult>(TDelegate, FixedMethodFunc<TDelegate, TResult>) Prevents the garbage collector from relocating a given method delegate and fixes its memory address until func finishes.
  • WithSafeFixed<TDelegate, TArg, TResult>(TDelegate, TArg, FixedMethodFunc<TDelegate, TArg, TResult>) Prevents the garbage collector from relocating a given method delegate and fixes its memory address until func finishes.
  • WithSafeFixed<T0, ..., T7>(Span<T0>, ..., Span<T7>, FixedListAction) Prevents the garbage collector from reallocating given spans and fixes their memory addresses until action completes.
  • WithSafeFixed<T0, ..., T7, TArg>(Span<T0>, ..., Span<T7>, TArg, FixedListAction<TArg>) Prevents the garbage collector from reallocating given spans and fixes their memory addresses until action completes.
  • WithSafeFixed<T0, ..., T7>(ReadOnlySpan<T0>, ..., ReadOnlySpan<T7>, ReadOnlyFixedListAction) Prevents the garbage collector from reallocating given spans and fixes their memory addresses until action completes.
  • WithSafeFixed<T0, ..., T7, TArg>(ReadOnlySpan<T0>, ..., ReadOnlySpan<T7>, TArg, ReadOnlyFixedListAction<TArg>) Prevents the garbage collector from reallocating given spans and fixes their memory addresses until action completes.
  • WithSafeFixed<T0, ..., T7, TResult>(Span<T0>, ..., Span<T7>, FixedListFunc<TResult>) Prevents the garbage collector from reallocating given spans and fixes their memory addresses until func completes.
  • WithSafeFixed<T0, ..., T7, TArg, TResult>(Span<T0>, ..., Span<T7>, TArg, FixedListFunc<TArg, TResult>) Prevents the garbage collector from reallocating given spans and fixes their memory addresses until func completes.
  • WithSafeFixed<T0, ..., T7, TResult>(ReadOnlySpan<T0>, ..., ReadOnlySpan<T7>, ReadOnlyFixedListFunc<TResult>) Prevents the garbage collector from reallocating given spans and fixes their memory addresses until func completes.
  • WithSafeFixed<T0, ..., T7, TArg, TResult>(Span<T0>, ..., Span<T7>, TArg, ReadOnlyFixedListFunc<TArg, TResult>) Prevents the garbage collector from reallocating given spans and fixes their memory addresses until func completes.
  • WithSafeReadOnlyFixed<T0, ..., T7>(Span<T0>, ..., Span<T7>, ReadOnlyFixedListAction) Prevents the garbage collector from reallocating given spans and fixes their memory addresses until action completes.
  • WithSafeReadOnlyFixed<T0, ..., T7, TArg>(Span<T0>, ..., Span<T7>, TArg, ReadOnlyFixedListAction<TArg>) Prevents the garbage collector from reallocating given spans and fixes their memory addresses until action completes.
  • WithSafeReadOnlyFixed<T0, ..., T7, TResult>(Span<T0>, ..., Span<T7>, ReadOnlyFixedListFunc<TResult>) Prevents the garbage collector from reallocating given spans and fixes their memory addresses until func completes.
  • WithSafeReadOnlyFixed<T0, ..., T7, TArg, TResult>(Span<T0>, ..., Span<T7>, TArg, ReadOnlyFixedListFunc<TArg, TResult>) Prevents the garbage collector from reallocating given spans and fixes their memory addresses until func completes.

License

This project is licensed under the MIT License, one of the most permissive and widely-used open-source licenses.

Key Highlights:

  • Freedom of Use: The package can be used in both open-source and closed-source projects without restrictions.
  • Modification and Distribution: You are free to modify, distribute, and even sublicense the software as needed.
  • Attribution: The only requirement is to include the original copyright notice and license text in any copies or substantial portions of the software.

Disclaimer:

The software is provided "as is," without warranty of any kind. The authors are not liable for any damages or issues that may arise from its use.

For more details, refer to the full license text included in the LICENSE file.


Contributing

We warmly welcome contributions to this open-source project! Whether you're here to report issues, propose enhancements, or contribute directly to the codebase, your help is greatly appreciated. Below are some ways you can get involved:

Reporting Issues

If you encounter a bug, experience unexpected behavior, or have suggestions for improvement, feel free to open an issue. Please include as much detail as possible, such as:

  • Steps to reproduce the problem
  • Your environment (e.g., OS, software version)
  • Any relevant logs or screenshots

Proposing Improvements

Have an idea for a new feature or enhancement? Open an issue with a clear description of your proposal and why you think it would benefit the project.

Contributing Code

You can directly contribute to the project by:

  • Fixing bugs
  • Adding new features
  • Improving existing functionality
  • Enhancing documentation

To contribute code:

  1. Fork the repository.
  2. Create a new branch for your changes.
  3. Make your changes and commit them with clear and concise messages.
  4. Submit a pull request for review.

Feel free to discuss your ideas or changes in an issue or pull request to align with the project's goals.

Translations

We currently support only a few languages, but we are open to adding more! If you'd like to help with translations, please open an issue or reach out to us. Your contributions to expanding the project's accessibility are highly valued.

Collaboration Guidelines

When contributing, please be respectful and constructive. We aim to create an inclusive and welcoming environment for everyone.

Thank you for considering contributing to this project! Your involvement, whether through reporting, coding, or translating, helps make this project better for everyone. 🚀

About

Provides a set of extensions which facilitates the exchange of information from and to .NET P/Invoke methods (or UnmanagedCallersOnly using NativeAOT).

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages