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

Using the CsWin32 PInvoke library #7444

Merged
merged 2 commits into from
Jul 21, 2022

Conversation

fernandanavamoya
Copy link
Contributor

@fernandanavamoya fernandanavamoya commented Jul 19, 2022

Converted these threads:
-GetCurrentActCtx
-GetExitCodeThread
-GetThreadLocale
-GlobalFree

Fixed the information messages in:
-COM2PropertyDescriptor.cs
-Control.cs

Microsoft Reviewers: Open in CodeFlow

Converted these threads:
-GetCurrentActCtx
-GetExitCodeThread
-GetThreadLocale
-GlobalFree

Fixed the information messages in:
-COM2PropertyDescriptor.cs
-Control.cs
@fernandanavamoya fernandanavamoya requested a review from a team as a code owner July 19, 2022 00:07
@fernandanavamoya
Copy link
Contributor Author

cc: @AArnott

@@ -8,3 +8,6 @@ GlobalUnlock
GetCurrentProcessId
GlobalReAlloc
GlobalFree
GetExitCodeThread
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to add a comment to this file that would explain what it is for?

Copy link
Contributor

Choose a reason for hiding this comment

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

The file format does support lines starting with // for comments. That said, I don't recommend it as it wouldn't sort properly and it would probably get outdated. Why not just search for the method name in the code (or use Find References) if you're curious how a method is used?

Copy link
Member

Choose a reason for hiding this comment

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

I'm curious how this file is used.

Copy link
Member

@dreddy-work dreddy-work Jul 19, 2022

Choose a reason for hiding this comment

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

Any public documentation on this? Also, does it require explicit build of the project for source generators to trigger?

Copy link
Contributor

Choose a reason for hiding this comment

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

The documentation is at https://github.com/microsoft/cswin32 (the readme file).
This is a standard roslyn source generator. As such, it does not require an explicit build. In fact the generated source code is never saved to disk, but rather produced in memory during compilation.

Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to add a link to this readme file to the top of the NativeMethods.txt file. If this is a single line comment, it wouldn't interfere with sorting file lines.

src/System.Windows.Forms.Primitives/src/NativeMethods.txt Outdated Show resolved Hide resolved
return s_contextCreationSucceeded
&& Kernel32.GetCurrentActCtx(out IntPtr current).IsTrue()
&& current == s_hActCtx;
&& PInvoke.GetCurrentActCtx(&current) == true
Copy link
Member

@RussKie RussKie Jul 19, 2022

Choose a reason for hiding this comment

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

Does PInvoke.GetCurrentActCtx(...) return a bool?
I checked and it returns BOOL, and this looks wonky...

Copy link
Member

Choose a reason for hiding this comment

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

The conversion is implicit

Suggested change
&& PInvoke.GetCurrentActCtx(&current) == true
&& PInvoke.GetCurrentActCtx(&current)

@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Windows.Win32;
using Foundation = Windows.Win32.Foundation;
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this alias?
image

Ditto below

Copy link
Contributor

Choose a reason for hiding this comment

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

Foundation holds BOOL and Handle as well as other types.

Copy link
Member

Choose a reason for hiding this comment

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

Correct, but in this case it doesn't appear necessary as the screenshot shows. I suspect it's probably true for other instances in this PR too.

@RussKie RussKie added the 📭 waiting-author-feedback The team requires more information from the author label Jul 19, 2022
@elachlan elachlan mentioned this pull request Jul 19, 2022
@@ -3231,21 +3232,21 @@ public unsafe bool HasPropertyPages()

private unsafe void ShowPropertyPageForDispid(Ole32.DispatchID dispid, Guid guid)
{
IntPtr pUnk = Marshal.GetIUnknownForObject(GetOcx());
nint pUnk = Marshal.GetIUnknownForObject(GetOcx());
Copy link
Contributor

@AArnott AArnott Jul 19, 2022

Choose a reason for hiding this comment

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

nit: I would leave this at IntPtr. You probably don't need the syntax support of nint (e.g. arithmetic), and IMO IntPtr is better for representing an opaque value like a pointer.
Similar feedback below. It looks pretty weird to cast pointers to nint.

Copy link
Member

Choose a reason for hiding this comment

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

In .NET 7 nint and IntPtr will refer to the exact same thing so changing between them does not make any difference.

if ((returnValue.IsTrue() && exitCode != NativeMethods.STILL_ACTIVE) ||
(returnValue.IsFalse() && Marshal.GetLastWin32Error() == ERROR.INVALID_HANDLE) ||
if ((returnValue == true && exitCode != NativeMethods.STILL_ACTIVE) ||
(returnValue == false && Marshal.GetLastWin32Error() == ERROR.INVALID_HANDLE) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

returnValue and !returnValue should work. No need to spell out == true and == false unless that's part of your codebase style guide, which at least for == true would be weird, IMO.

Copy link
Contributor

@AArnott AArnott left a comment

Choose a reason for hiding this comment

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

Change looks good, although I would personally try to avoid mixing the syntax only changes (e.g. IntPtr to nint) with the CsWin32 changes.

@ghost ghost removed the 📭 waiting-author-feedback The team requires more information from the author label Jul 19, 2022
@dreddy-work dreddy-work added ready-to-merge PRs that are ready to merge but worth notifying the internal team. and removed ready-to-merge PRs that are ready to merge but worth notifying the internal team. labels Jul 20, 2022
@RussKie RussKie added the 📭 waiting-author-feedback The team requires more information from the author label Jul 21, 2022
@ghost ghost removed the 📭 waiting-author-feedback The team requires more information from the author label Jul 21, 2022
@fernandanavamoya fernandanavamoya merged commit ec41384 into dotnet:feature/win32 Jul 21, 2022
@fernandanavamoya fernandanavamoya deleted the ThreadAPI18 branch July 21, 2022 17:55
@ghost ghost locked as resolved and limited conversation to collaborators Aug 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants