Skip to content

Add managed C#/VB declarations (DllImport + StructLayout) to PInvokeLib.dll reference article#52104

Open
Copilot wants to merge 4 commits intomainfrom
copilot/update-marshaling-example
Open

Add managed C#/VB declarations (DllImport + StructLayout) to PInvokeLib.dll reference article#52104
Copilot wants to merge 4 commits intomainfrom
copilot/update-marshaling-example

Conversation

Copy link
Contributor

Copilot AI commented Mar 6, 2026

  • Create C# snippet file with managed struct declarations (StructLayout) and DllImport prototypes matching pinvokelib.h
  • Create VB snippet file with equivalent declarations
  • Update marshalling-data-with-platform-invoke.md to include the C# and VB managed code sections
  • Update article frontmatter with dev_langs and ai-usage
  • Verify snippets compile correctly
  • Fix library name consistency: change "Pinvoke.dll" to "PInvokeLib.dll" on line 58
Original prompt

This section details on the original issue you should resolve

<issue_title>You're missing 50% of the example here</issue_title>
<issue_description>This is a great example EXCEPT that it doesn't show the DllImport or StructLayout details from C#, which is the other (necessary) 50% that makes this useful...


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Comments on the Issue (you are @copilot in this section)

@dotnet-bot This issue has been closed as part of the issue backlog grooming process outlined in #22351.

That automated process may have closed some issues that should be addressed. If you think this is one of them, reopen it with a comment explaining why. Tag the @dotnet/docs team for visibility.</comment_new>
<comment_new>@BillWagner
Thanks @mr-intj

reopening.</comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.


Internal previews

📄 File 🔗 Preview link
docs/framework/interop/marshalling-data-with-platform-invoke.md Marshalling Data with Platform Invoke

…-invoke.md

- Add StructLayout and DllImport managed type/function declarations
- Create C# and VB snippet files for PInvokeLibManaged
- Update article frontmatter with dev_langs and ai-usage

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copilot AI changed the title [WIP] Update marshaling example to include DllImport and StructLayout Add managed C#/VB declarations (DllImport + StructLayout) to PInvokeLib.dll reference article Mar 6, 2026
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, and it's ready for final review.

@BillWagner BillWagner marked this pull request as ready for review March 6, 2026 22:04
@BillWagner BillWagner requested a review from a team as a code owner March 6, 2026 22:04
Copilot AI review requested due to automatic review settings March 6, 2026 22:04
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR completes the PinvokeLib.dll example in marshalling-data-with-platform-invoke.md by adding the managed C# and Visual Basic declarations (struct mappings and DllImport signatures) that match the unmanaged header, and wiring those declarations into the article via snippet includes.

Changes:

  • Added new C# and VB snippet projects containing managed StructLayout declarations and DllImport prototypes for PinvokeLib.dll.
  • Updated the article to include “Managed type declarations” and “Managed function prototypes” sections that reference the new snippet files.
  • Updated article metadata (ms.date, dev_langs, and ai-usage).

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
docs/framework/interop/marshalling-data-with-platform-invoke.md Adds new managed-declarations subsections and snippet includes; updates frontmatter metadata.
docs/framework/interop/snippets/marshalling-data-with-platform-invoke/csharp/PInvokeLibManaged/NativeMethods.cs Introduces the C# StructLayout types and DllImport signatures for PinvokeLib.dll.
docs/framework/interop/snippets/marshalling-data-with-platform-invoke/csharp/PInvokeLibManaged/PInvokeLibManaged.csproj Adds a minimal C# project to host/compile the snippet source.
docs/framework/interop/snippets/marshalling-data-with-platform-invoke/csharp/PInvokeLibManaged/Program.cs Adds a minimal entry point for the snippet project.
docs/framework/interop/snippets/marshalling-data-with-platform-invoke/vb/PInvokeLibManaged/NativeMethods.vb Introduces the VB StructLayout types and DllImport signatures for PinvokeLib.dll.
docs/framework/interop/snippets/marshalling-data-with-platform-invoke/vb/PInvokeLibManaged/PInvokeLibManaged.vbproj Adds a minimal VB project to host/compile the snippet source.
docs/framework/interop/snippets/marshalling-data-with-platform-invoke/vb/PInvokeLibManaged/Program.vb Adds a minimal entry point for the VB snippet project.

Comment on lines 63 to +66

[!code-cpp[PInvokeLib#2](../../../samples/snippets/cpp/VS_Snippets_CLR/pinvokelib/cpp/pinvokelib.h#2)]

To call the library functions from managed code, first implement the managed prototypes for each function you want to invoke.
If the unmanaged code uses any custom types, you must also declare those types in your managed code.
Decorate the prototype with the <xref:System.Runtime.InteropServices.DllImportAttribute> attribute.
To call the library functions from managed code, implement managed prototypes for each function you want to invoke. If the unmanaged code uses any custom types, you must also declare those types in your managed code. Decorate each prototype with the <xref:System.Runtime.InteropServices.DllImportAttribute> attribute, and use <xref:System.Runtime.InteropServices.StructLayoutAttribute> to control the layout of managed structures so they match the unmanaged equivalents.
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The section header is "PinvokeLib.dll", but the sentence immediately above this new paragraph still says the functions are provided by "Pinvoke.dll". Update that reference to "PinvokeLib.dll" so the library name is consistent throughout the section.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot change the text on line 58 to use "PInvokeLib.dll"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 2b6c51a — "Pinvoke.dll" is now "PInvokeLib.dll".

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

You're missing 50% of the example here

3 participants