8000 Cleanup dllimport by iSazonov · Pull Request #8847 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Cleanup dllimport#8847

Merged
iSazonov merged 17 commits intoPowerShell:masterfrom
iSazonov:cleanup-dllimport
Feb 27, 2019
Merged

Cleanup dllimport#8847
iSazonov merged 17 commits intoPowerShell:masterfrom
iSazonov:cleanup-dllimport

Conversation

@iSazonov
Copy link
Collaborator
@iSazonov iSazonov commented Feb 8, 2019

PR Summary

  • Remove unused DllImport-s.
  • Fix return bool in still used signatures.

PR Context

After moving to .Net Core 2 we migrate to ported APIs and now we have many unused Windows pinvokes.

Also return: MarshalAs(UnmanagedType.Bool) was added where it was missing.

PR Checklist

@iSazonov iSazonov requested a review from BrucePay as a code owner February 8, 2019 10:38
@iSazonov iSazonov changed the title WIP: Cleanup dllimport Cleanup dllimport Feb 8, 2019
@iSazonov iSazonov added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label Feb 9, 2019
NativeMethods.ReadConsole(
consoleHandle.DangerousGetHandle(),
buffer,
out buffer,
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure why this was changed to an out parameter, since a valid object reference is passed in?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It come from the method signature https://docs.microsoft.com/en-us/windows/console/readconsole
My intentions were to fix whole signatures, but it’s too much work and I stop this.
I can revert the change too.

Copy link
Contributor

Choose a reason for hiding this comment

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

By pure coincidence, I noticed this change, and I don't think it's correct. See the last comment here. The original MSDN mag article seems to be gone, but the relevant part is quoted:

StringBuilder and Marshaling
The CLR marshaler has built-in knowledge of the StringBuilder type and treats it differently from other types. By default, StringBuilder is passed as [InAttribute, OutAttribute]. StringBuilder is special because it has a Capacity property that can determine the size of the required buffer at run time, and it can be changed dynamically. Therefore, during the marshaling process, the CLR can pin StringBuilder, directly pass the address of internal buffer used in StringBuilder, and allow the contents of this buffer to be changed by native code in place.

To take full advantage of StringBuilder, you'll need to follow all of these rules:

  1. Don't pass StringBuilder by reference (using out or ref). Otherwise, the CLR will expect the signature of this argument to be wchar_t ** instead of wchar_t *, and it won't be able to pin StringBuilder's internal buffer. Performance will be significantly degraded.
  2. Use StringBuilder when the unmanaged code is using Unicode. Otherwise, the CLR will have to make a copy of the string and convert it between Unicode and ANSI, thus degrading performance. Usually you should marshal StringBuilder as LPARRAY of Unicode characters or as LPWSTR.
  3. Always specify the capacity of StringBuilder in advance and make sure the capacity is big enough to hold the buffer. The best practice on the unmanaged code side is to accept the size of the string buffer as an argument to avoid buffer overruns. In COM, you can also use size_is in IDL to specify the size.

In reply to: 255789200 [](ancestors = 255789200)

Copy link
Collaborator Author
@iSazonov iSazonov Mar 5, 2019

Choose a reason for hiding this comment

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

@jazzdelightsme Thanks! I skipped that it is not buffer. Will revert.

Done #9066

);

[DllImport(PinvokeDllNames.ScrollConsoleScreenBufferDllName, SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand adding these attributes to be consistent throughout our codebase, but why are they needed? It seems like 'marshal to bool' is implied...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Staffan Gustafsson fixed a bug in one place (there is BOOLEAN not BOOL type in native code). I reviewed all such signatures and added the attribute to ensure that all right.

@iSazonov
Copy link
Collaborator Author

@PaulHigin Could you please continue the review?

@iSazonov iSazonov self-assigned this Feb 27, 2019
@iSazonov iSazonov merged commit 11ae24f into PowerShell:master Feb 27, 2019
@iSazonov iSazonov deleted the cleanup-dllimport branch February 27, 2019 03:33
@iSazonov
Copy link
Collaborator Author

@PaulHigin Thanks for code review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0