-
Notifications
You must be signed in to change notification settings - Fork 976
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
Using the CsWin32 PInvoke library #7444
Conversation
Converted these threads: -GetCurrentActCtx -GetExitCodeThread -GetThreadLocale -GlobalFree Fixed the information messages in: -COM2PropertyDescriptor.cs -Control.cs
cc: @AArnott |
@@ -8,3 +8,6 @@ GlobalUnlock | |||
GetCurrentProcessId | |||
GlobalReAlloc | |||
GlobalFree | |||
GetExitCodeThread |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
return s_contextCreationSucceeded | ||
&& Kernel32.GetCurrentActCtx(out IntPtr current).IsTrue() | ||
&& current == s_hActCtx; | ||
&& PInvoke.GetCurrentActCtx(¤t) == true |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conversion is implicit
&& PInvoke.GetCurrentActCtx(¤t) == true | |
&& PInvoke.GetCurrentActCtx(¤t) |
@@ -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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@@ -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()); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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) || |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
Converted these threads:
-GetCurrentActCtx
-GetExitCodeThread
-GetThreadLocale
-GlobalFree
Fixed the information messages in:
-COM2PropertyDescriptor.cs
-Control.cs
Microsoft Reviewers: Open in CodeFlow