Skip to content
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

Suppress P/Invoke warning for GetWindowThreadProcessId in UWP #3372

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- P/Invoke warning for GetWindowThreadProcessId no longer shows when using Sentry in UWP applications ([#3372](https://github.com/getsentry/sentry-dotnet/pull/3372))

## 4.6.2

### Fixes
Expand Down
5 changes: 4 additions & 1 deletion src/Sentry/Internal/ProcessInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ private static DateTimeOffset GetStartupTime()
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
// GetWindowThreadProcessId is only available in the Windows SDK, so trying to call this from UWP apps will fail.
// We wrap use of this in a try/catch as a workaround. However, we need `ExactSpelling = true` here to suppress
// warnings about the use of this API when compiling UWP applications.
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
Copy link
Contributor

Choose a reason for hiding this comment

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

I get that the warning stated

Please make sure the P/Invoke either points to a Windows API allowed in UWP applications, or a native DLL that is part of the package. 
If for some reason your P/Invoke does not satisfy those requirements, please use [DllImport(ExactSpelling=true) to indicate that you understand the implications of using non-UWP A 

but isn't adding ExactSpelling=true more a "Trust me, I know what I'm doing" and not a suppression of a warning?
But does it make a difference? The comment explains it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I published a new Sentry.nupkg file after making this change, referenced it in a UWP solution and rebuilt... yes the warnings go away if you do that. It's a very bizarre way to suppress warnings if you ask me (thus the code comment).

private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
}
Loading