You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* updating and correcting topic and sample app
* testing highlighted line number
* ids and tings
* Line numbers. I can't believe how labor-intensive this way of illustrating code is (embedding samples in the content, quoting snippets, highlighting lines).
* OMHG
Copy file name to clipboardExpand all lines: hub/apps/winui/winui3/desktop-winui3-app-with-basic-interop.md
+17-21Lines changed: 17 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,24 @@
1
1
---
2
-
description: Build a C# .NET and C++ desktop (Win32) application with WinUI 3 and basic Win32 interop capabilities using the Platform Invocation Services, or PInvoke.
3
2
title: Build a C# .NET app with WinUI 3 and Win32 interop
4
-
ms.date: 07/09/2024
3
+
description: Build a C# .NET application with WinUI 3 and basic Win32 interop capabilities using the Platform Invocation Services, or PInvoke.
4
+
ms.date: 03/04/2025
5
5
ms.topic: article
6
-
keywords: windows 10, windows 11, uwp, COM, win32, winui, interop
6
+
keywords: windows 11, windows 10, uwp, COM, win32, winui, interop
7
7
ms.localizationpriority: high
8
8
ms.custom: 19H1
9
9
---
10
10
11
11
# Build a C# .NET app with WinUI 3 and Win32 interop
12
12
13
-
In this article, we step through how to build a basic **C# .NET** application with WinUI 3 and Win32 interop capabilities using Platform Invocation Services ([PInvoke](https://github.com/dotnet/pinvoke)).
13
+
In this topic, we step through how to build a basic **C# .NET** application with WinUI 3 and Win32 interop capabilities using Platform Invocation Services ([PInvoke](https://github.com/dotnet/pinvoke)).
14
14
15
15
## Prerequisites
16
16
17
17
1.[Start developing Windows apps](../../get-started/start-here.md)
18
18
19
19
## Basic managed C#/.NET app
20
20
21
-
For this example, we'll specify the location and size of the app window, convert and scale it for the appropriate DPI, disable the window minimize and maximize buttons, and finally query the current process to show a list of modules loaded in the current process.
21
+
For this example, we'll specify the location and size of the app window, convert and scale it for the appropriate DPI, disable the window minimize and maximize buttons, and finally query the current process to show a list of the modules that are loaded into the current process.
22
22
23
23
We're going to build our example app from the initial template application (see [Prerequisites](#prerequisites)). Also see [WinUI 3 templates in Visual Studio](winui-project-templates-in-visual-studio.md).
24
24
@@ -48,39 +48,35 @@ The following code shows the MainWindow.xaml file from the initial template app,
48
48
49
49
### Configuration
50
50
51
-
1. To call Win32 APIs exposed in User32.dll, add the open source[**PInvoke.User32** NuGet package](https://github.com/dotnet/pinvoke) to the VS project (from the Visual Studio menus, select**Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution...** and search for "Pinvoke.User32"). For more details, see [Calling Native Functions from Managed Code](/cpp/dotnet/calling-native-functions-from-managed-code).
51
+
1. To call Win32 APIs exported from `User32.dll`, you can use the[**C#/Win32 P/Invoke Source Generator**](https://github.com/microsoft/CsWin32) in your Visual Studio project. Click**Tools** > **NuGet Package Manager** > **Manage NuGet Packages for Solution...**, and (on the **Browse** tab) search for *Microsoft.Windows.CsWin32*. For more details, see [Calling Native Functions from Managed Code](/cpp/dotnet/calling-native-functions-from-managed-code).
52
52
53
-
:::image type="content" source="images/build-basic/nuget-pkg-manager-pinvoke.png" alt-text="Screenshot of the Visual Studio NuGet Package Manager with PInvoke.User32 selected.":::<br/>*NuGet Package Manager with PInvoke.User32 selected.*
53
+
You can optionally confirm that installation was successful by confirming that *Microsoft.Windows.CsWin32* is listed under the **Dependencies** > **Packages** node in Solution Explorer.
54
54
55
-
Confirm installation was successful by checking the **Packages** folder in the VS project.
55
+
You can also optionally double-click the application project file (or right click and select **Edit project file**) to open the file in a text editor, and confirm that the project file now includes a NuGet `PackageReference` for "Microsoft.Windows.CsWin32".
56
56
57
-
:::image type="content" source="images/build-basic/solution-explorer-packages-pinvoke.png" alt-text="Screenshot of the Visual Studio Solution Explorer Packages with PInvoke.User32.":::<br/>*Solution Explorer Packages with PInvoke.User32.*
58
-
59
-
Next, double-click the application project file (or right click and select "Edit project file") to open the file in a text editor and confirm the project file now includes the NuGet `PackageReference` for "PInvoke.User32".
1. In the `App.xaml.cs` code-behind file, we get a handle to the [**Window**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.window) using the **WindowNative.GetWindowHandle** WinRT COM interop method (see [Retrieve a window handle (HWND)](../../develop/ui-input/retrieve-hwnd.md)).
61
+
1. In the `App.xaml.cs` code-behind file, we get a handle to the [**Window**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.window)by using the **WindowNative.GetWindowHandle** WinRT COM interop method (see [Retrieve a window handle (HWND)](../../develop/ui-input/retrieve-hwnd.md)).
66
62
67
-
This method is called from the app's [**OnLaunched**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.onlaunched) handler, as shown here:
63
+
That method is called from the app's [**OnLaunched**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.onlaunched) handler, as shown here:
1. We then call a `SetWindowDetails` method, passing the Window handle and preferred dimensions. Remember to add the `using static PInvoke.User32;` directive.
67
+
1. We then call a `SetWindowDetails` method, passing the Window handle and preferred dimensions.
72
68
73
69
In this method:
74
70
75
-
- We call [GetDpiForWindow](/windows/win32/api/winuser/nf-winuser-getdpiforwindow) to get the dots per inch (dpi) value for the window (Win32 uses actual pixels while WinUI 3 uses effective pixels). This dpi value is used to calculate the scale factor and apply it to the width and height specified for the window.
71
+
- We call [GetDpiForWindow](/windows/win32/api/winuser/nf-winuser-getdpiforwindow) to get the dots per inch (dpi) value for the window (Win32 uses physical pixels, while WinUI 3 uses effective pixels). This dpi value is used to calculate the scale factor, and apply it to the width and height specified for the window.
76
72
- We then call [SetWindowPos](/windows/win32/api/winuser/nf-winuser-setwindowpos) to specify the desired location of the window.
77
73
- Finally, we call [SetWindowLong](/windows/win32/api/winuser/nf-winuser-setwindowlongw) to disable the *Minimize* and *Maximize* buttons.
1. In the MainWindow.xaml file, we use a [ContentDialog](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.contentdialog) with a [ScrollViewer](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.scrollviewer) to display a list of all the modules loaded for the current process.
Copy file name to clipboardExpand all lines: hub/apps/winui/winui3/samples/WinUI-3-basic-win32-interop/WinUI-3-basic-win32-interop (Package)/WinUI-3-basic-win32-interop (Package).wapproj
0 commit comments