System.Runtime.InteropServices by Microsoft
NuGet / site data
Details
Info
Name: System.Runtime.InteropServices
Provides types that support COM interop and platform invoke services.
Commonly Used Types: System.Runtime.InteropServices.GCHandle System.Runtime.InteropServices.GuidAttribute System.Runtime.InteropServices.COMException System.DllNotFoundException System.Runtime.InteropServices.DllImportAttribute
Author: Microsoft
NuGet: https://www.nuget.org/packages/System.Runtime.InteropServices/
You can find more details at https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke-source-generation
Source: https://www.nuget.org/packages/System.Runtime.InteropServices/
Original Readme
About
Generate PInvoke calls
How to use
Example (source csproj, source files)
- CSharp Project
- Program.cs
- DemoMsgBox.cs
- globals.cs
This is the CSharp Project that references System.Runtime.InteropServices
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>
This is the use of System.Runtime.InteropServices in Program.cs
WriteLine(MessageBoxW(IntPtr.Zero, "asd", "asd", 1));
//RSCG
WriteLine(MessageBoxW_LI(IntPtr.Zero, "asd", "asd", 1));
This is the use of System.Runtime.InteropServices in DemoMsgBox.cs
partial class DemoImport
{
[DllImport("user32.dll", EntryPoint = "MessageBoxW", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int MessageBoxW(IntPtr hWnd, string lpText, string lpCaption, uint uType);
[LibraryImport("user32.dll", EntryPoint = "MessageBoxW", SetLastError = true,
StringMarshalling = StringMarshalling.Utf16)]
internal static partial int MessageBoxW_LI(IntPtr hWnd, string lpText, string lpCaption, uint uType);
}
This is the use of System.Runtime.InteropServices in globals.cs
global using static System.Console;
global using static DemoImport;
global using System.Runtime.InteropServices;
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- LibraryImports.g.cs
// <auto-generated/>
unsafe partial class DemoImport
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "7.0.10.26716")]
[System.Runtime.CompilerServices.SkipLocalsInitAttribute]
internal static partial int MessageBoxW_LI(nint hWnd, string lpText, string lpCaption, uint uType)
{
int __lastError;
int __retVal;
// Pin - Pin data in preparation for calling the P/Invoke.
fixed (void* __lpText_native = &global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.GetPinnableReference(lpText))
fixed (void* __lpCaption_native = &global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.GetPinnableReference(lpCaption))
{
System.Runtime.InteropServices.Marshal.SetLastSystemError(0);
__retVal = __PInvoke(hWnd, (ushort*)__lpText_native, (ushort*)__lpCaption_native, uType);
__lastError = System.Runtime.InteropServices.Marshal.GetLastSystemError();
}
System.Runtime.InteropServices.Marshal.SetLastPInvokeError(__lastError);
return __retVal;
// Local P/Invoke
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MessageBoxW", ExactSpelling = true)]
static extern unsafe int __PInvoke(nint hWnd, ushort* lpText, ushort* lpCaption, uint uType);
}
}
Useful
Download Example (.NET C#)
Share System.Runtime.InteropServices
https://ignatandrei.github.io/RSCG_Examples/v2/docs/System.Runtime.InteropServices
aaa
Category "EnhancementClass" has the following generators:
1 AOP.Logging.SourceGenerator
2026-08-30
2 ApparatusAOT
2023-04-16
3 AspectGenerator
2024-01-07
4 CommonCodeGenerator
2024-04-03
5 Comparison
2025-05-25
6 DudNet
2023-10-27
7 Enhanced.GetTypes
2024-09-17
8 FastGenericNew
2023-08-10
9 Immutype
2023-08-12
10 Ling.Audit
2023-12-12
11 Lombok.NET
2023-04-16
12 M31.FluentAPI
2023-08-25
13 MemberAccessor
2025-03-24
14 MemoryPack
2023-08-04
15 Meziantou.Polyfill
2023-10-10
16 Microsoft.Extensions.Logging
2023-04-16
17 Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator
2023-11-17
18 Microsoft.Interop.JavaScript.JSImportGenerator 2023-04-16
19 NLog.Extensions.ThisClass
2026-04-03
20 OptionToStringGenerator
2024-02-15
21 Pekspro.DataAnnotationValuesExtractor
2026-02-15
22 Program
2025-11-06
23 QueryStringGenerator
2024-11-07
24 RSCG_Decorator
2023-09-30
25 RSCG_UtilityTypes
2023-12-22
26 StaticReflection
2023-10-13
27 SyncMethodGenerator
2023-08-14
28 System.Runtime.InteropServices
2023-04-16
29 System.Text.RegularExpressions
2023-04-16
30 TelemetryLogging
2023-11-30
31 Tenekon.MethodOverloads.SourceGenerator
2026-09-05
32 ThisClass
2024-04-19
33 TrimItEasy
2026-08-28