Skip to content

Add DefaultDllImportSearchPaths attribute to WindowsAppRuntime_EnsureIsLoaded method#10

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-9
Draft

Add DefaultDllImportSearchPaths attribute to WindowsAppRuntime_EnsureIsLoaded method#10
Copilot wants to merge 2 commits intomainfrom
copilot/fix-9

Conversation

Copy link

Copilot AI commented May 22, 2025

This PR fixes the code analysis warning CA5392 that occurs when WindowsAppSDKSelfContained and code analysis are turned on for a WinUI 3 project.

Issue Details

When building a WinUI 3 project with both WindowsAppSDKSelfContained set to true and code analysis enabled, the build fails due to CA5392 warning:

The method WindowsAppRuntime_EnsureIsLoaded didn't use DefaultDllImportSearchPaths attribute for P/Invokes.

This happens because the DllImport declaration in UndockedRegFreeWinRT-AutoInitializer.cs was missing the required attribute, which is considered a security risk due to potential DLL hijacking.

Solution

Added the DefaultDllImportSearchPaths attribute with DllImportSearchPath.SafeDirectories value to the WindowsAppRuntime_EnsureIsLoaded method:

[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
[DllImport("Microsoft.WindowsAppRuntime.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int WindowsAppRuntime_EnsureIsLoaded();

This value was chosen because:

  1. It provides the appropriate security settings without triggering additional code analysis warnings
  2. It includes the application directory, System32 directory, and user directories, which covers all required search paths
  3. It avoids the CA5393 warning that would occur if using only DllImportSearchPath.ApplicationDirectory

Fixes #9.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • microsoft.pkgs.visualstudio.com
    • Triggering command: dotnet build WindowsAppRuntime.sln (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…IsLoaded method

Co-authored-by: MuyuanMS <116717757+MuyuanMS@users.noreply.github.com>
Copilot AI changed the title [WIP] Missing DefaultDllImportSearchPaths attribute in WindowsAppRuntime_EnsureIsLoaded when WindowsAppSDKSelfContained is turned on Add DefaultDllImportSearchPaths attribute to WindowsAppRuntime_EnsureIsLoaded method May 22, 2025
Copilot AI requested a review from MuyuanMS May 22, 2025 06:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing DefaultDllImportSearchPaths attribute in WindowsAppRuntime_EnsureIsLoaded when WindowsAppSDKSelfContained is turned on

2 participants