-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add managed C#/VB declarations (DllImport + StructLayout) to PInvokeLib.dll reference article #52104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/update-marshaling-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+230
−11
Open
Add managed C#/VB declarations (DllImport + StructLayout) to PInvokeLib.dll reference article #52104
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
.../snippets/marshalling-data-with-platform-invoke/csharp/PInvokeLibManaged/NativeMethods.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| // <ManagedTypes> | ||
| // Managed type declarations that correspond to the unmanaged types in PinvokeLib.dll. | ||
|
|
||
| [StructLayout(LayoutKind.Sequential)] | ||
| internal struct MyPoint | ||
| { | ||
| public int x; | ||
| public int y; | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] | ||
| internal struct MyPerson | ||
| { | ||
| public string first; | ||
| public string last; | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Sequential)] | ||
| internal struct MyPerson2 | ||
| { | ||
| public IntPtr person; // Pointer to a MyPerson structure | ||
| public int age; | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Sequential)] | ||
| internal struct MyPerson3 | ||
| { | ||
| public MyPerson person; // Embedded MyPerson structure | ||
| public int age; | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Explicit)] | ||
| internal struct MyUnion | ||
| { | ||
| [FieldOffset(0)] public int i; | ||
| [FieldOffset(0)] public double d; | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Sequential)] | ||
| internal struct MyArrayStruct | ||
| { | ||
| public bool flag; | ||
|
|
||
| [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] | ||
| public int[] vals; | ||
| } | ||
| // </ManagedTypes> | ||
|
|
||
| // <NativeMethods> | ||
| internal static class NativeMethods | ||
| { | ||
| [DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)] | ||
| internal static extern int TestArrayOfInts( | ||
| [In, Out] int[] array, int size); | ||
|
|
||
| [DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)] | ||
| internal static extern int TestRefArrayOfInts( | ||
| ref IntPtr array, ref int size); | ||
|
|
||
| [DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)] | ||
| internal static extern int TestMatrixOfInts( | ||
| [In, Out] int[,] matrix, int row); | ||
|
|
||
| [DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)] | ||
| internal static extern int TestArrayOfStrings( | ||
| [In, Out] string[] array, int size); | ||
|
|
||
| [DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)] | ||
| internal static extern int TestArrayOfStructs( | ||
| [In, Out] MyPoint[] pointArray, int size); | ||
|
|
||
| [DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)] | ||
| internal static extern int TestArrayOfStructs2( | ||
| [In, Out] MyPerson[] personArray, int size); | ||
|
|
||
| [DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)] | ||
| internal static extern int TestStructInStruct(ref MyPerson2 person2); | ||
|
|
||
| [DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)] | ||
| internal static extern void TestStructInStruct3(MyPerson3 person3); | ||
|
|
||
| [DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)] | ||
| internal static extern void TestUnion(MyUnion u, int type); | ||
|
|
||
| [DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)] | ||
| internal static extern void TestArrayInStruct(ref MyArrayStruct myStruct); | ||
| } | ||
| // </NativeMethods> |
10 changes: 10 additions & 0 deletions
10
...s/marshalling-data-with-platform-invoke/csharp/PInvokeLibManaged/PInvokeLibManaged.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
2 changes: 2 additions & 0 deletions
2
...nterop/snippets/marshalling-data-with-platform-invoke/csharp/PInvokeLibManaged/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
|
|
||
| Console.WriteLine("Hello, World!"); |
94 changes: 94 additions & 0 deletions
94
...erop/snippets/marshalling-data-with-platform-invoke/vb/PInvokeLibManaged/NativeMethods.vb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| Imports System.Runtime.InteropServices | ||
|
|
||
| ' <ManagedTypes> | ||
| ' Managed type declarations that correspond to the unmanaged types in PinvokeLib.dll. | ||
|
|
||
| <StructLayout(LayoutKind.Sequential)> | ||
| Friend Structure MyPoint | ||
| Public x As Integer | ||
| Public y As Integer | ||
| End Structure | ||
|
|
||
| <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> | ||
| Friend Structure MyPerson | ||
| Public first As String | ||
| Public last As String | ||
| End Structure | ||
|
|
||
| <StructLayout(LayoutKind.Sequential)> | ||
| Friend Structure MyPerson2 | ||
| Public person As IntPtr ' Pointer to a MyPerson structure | ||
| Public age As Integer | ||
| End Structure | ||
|
|
||
| <StructLayout(LayoutKind.Sequential)> | ||
| Friend Structure MyPerson3 | ||
| Public person As MyPerson ' Embedded MyPerson structure | ||
| Public age As Integer | ||
| End Structure | ||
|
|
||
| <StructLayout(LayoutKind.Explicit)> | ||
| Friend Structure MyUnion | ||
| <FieldOffset(0)> Public i As Integer | ||
| <FieldOffset(0)> Public d As Double | ||
| End Structure | ||
|
|
||
| <StructLayout(LayoutKind.Sequential)> | ||
| Friend Structure MyArrayStruct | ||
| Public flag As Boolean | ||
|
|
||
| <MarshalAs(UnmanagedType.ByValArray, SizeConst:=3)> | ||
| Public vals As Integer() | ||
| End Structure | ||
| ' </ManagedTypes> | ||
|
|
||
| ' <NativeMethods> | ||
| Friend Class NativeMethods | ||
| <DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)> | ||
| Friend Shared Function TestArrayOfInts( | ||
| <[In], Out> ByVal array() As Integer, ByVal size As Integer) As Integer | ||
| End Function | ||
|
|
||
| <DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)> | ||
| Friend Shared Function TestRefArrayOfInts( | ||
| ByRef array As IntPtr, ByRef size As Integer) As Integer | ||
| End Function | ||
|
|
||
| <DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)> | ||
| Friend Shared Function TestMatrixOfInts( | ||
| <[In], Out> ByVal matrix(,) As Integer, ByVal row As Integer) As Integer | ||
| End Function | ||
|
|
||
| <DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)> | ||
| Friend Shared Function TestArrayOfStrings( | ||
| <[In], Out> ByVal array() As String, ByVal size As Integer) As Integer | ||
| End Function | ||
|
|
||
| <DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)> | ||
| Friend Shared Function TestArrayOfStructs( | ||
| <[In], Out> ByVal pointArray() As MyPoint, ByVal size As Integer) As Integer | ||
| End Function | ||
|
|
||
| <DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)> | ||
| Friend Shared Function TestArrayOfStructs2( | ||
| <[In], Out> ByVal personArray() As MyPerson, ByVal size As Integer) As Integer | ||
| End Function | ||
|
|
||
| <DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)> | ||
| Friend Shared Function TestStructInStruct( | ||
| ByRef person2 As MyPerson2) As Integer | ||
| End Function | ||
|
|
||
| <DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)> | ||
| Friend Shared Sub TestStructInStruct3(ByVal person3 As MyPerson3) | ||
| End Sub | ||
|
|
||
| <DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)> | ||
| Friend Shared Sub TestUnion(ByVal u As MyUnion, ByVal type As Integer) | ||
| End Sub | ||
|
|
||
| <DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)> | ||
| Friend Shared Sub TestArrayInStruct(ByRef myStruct As MyArrayStruct) | ||
| End Sub | ||
| End Class | ||
| ' </NativeMethods> |
9 changes: 9 additions & 0 deletions
9
...ppets/marshalling-data-with-platform-invoke/vb/PInvokeLibManaged/PInvokeLibManaged.vbproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <RootNamespace>PInvokeLibManaged</RootNamespace> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
7 changes: 7 additions & 0 deletions
7
...rk/interop/snippets/marshalling-data-with-platform-invoke/vb/PInvokeLibManaged/Program.vb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| Imports System | ||
|
|
||
| Module Program | ||
| Sub Main(args As String()) | ||
| Console.WriteLine("Hello World!") | ||
| End Sub | ||
| End Module |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.