Skip to content

Remove 'out' in ReadConsole pinvoke - #9066

Merged
Dongbo Wang (daxian-dbw) merged 1 commit into
PowerShell:masterfrom
iSazonov:revert-out-in-readconsole
Mar 14, 2019
Merged

Dongbo Wang (daxian-dbw) merged 1 commit into
PowerShell:masterfrom
iSazonov:revert-out-in-readconsole

Conversation

@iSazonov

Copy link
Copy Markdown
Collaborator

PR Summary

Revert change based on the comment #8847 (comment)

PR Context

PR Checklist

@iSazonov Ilya (iSazonov) added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label Mar 5, 2019
@iSazonov Ilya (iSazonov) mentioned this pull request Mar 5, 2019
7 of 11 tasks
@iSazonov Ilya (iSazonov) changed the title Remove out in ReadConsole Remove 'out' in ReadConsole pinvoke Mar 5, 2019
@daxian-dbw

Dongbo Wang (daxian-dbw) commented Mar 13, 2019 •

Copy link
Copy Markdown
Member

Look goo to me.
Can you please update this signature as well: https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/namespaces/FileSystemProvider.cs#L7082
Should be changed from [return: MarshalAs(UnmanagedType.I1)] to [return: MarshalAs(UnmanagedType.Bool)].

No change is needed as per Dan Thompson (@jazzdelightsme) pointed out below.

@jazzdelightsme

Dan Thompson (jazzdelightsme) commented Mar 13, 2019 •

Copy link
Copy Markdown
Contributor

Dongbo Wang (@daxian-dbw) : No! CreateSymbolicLinkW really does return a 1-byte boolean value. (BOOLEAN versus BOOL).

(and I verified this by looking at type information in a debugger; I am very sure)

@daxian-dbw

Dongbo Wang (daxian-dbw) commented Mar 13, 2019 •

Copy link
Copy Markdown
Member

Dan Thompson (@jazzdelightsme) Thanks for catching that! Can you please explain a bit more about the difference between BOOLEAN and BOOL in C++?
Is BOOL actually defined as an int in some header files?

@daxian-dbw

Copy link
Copy Markdown
Member

Ha, I might find it -- https://stackoverflow.com/a/6420546/7129704

BOOL is a Microsoft specific type that is defined as an int. You can find it in windef.h

@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit 0f7efdd into PowerShell:master Mar 14, 2019
@iSazonov

Copy link
Copy Markdown
Collaborator Author

Dongbo Wang (@daxian-dbw) I see in https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/interop-pinvokes.md#strings that StringBuilder marshalling always re-allocate/re-copy buffers. Make sense review our code to reduce such allocations? Although at first glance I did not see how we could do it.

And the PR fix could be in 6.2.

@iSazonov
Ilya (iSazonov) deleted the revert-out-in-readconsole branch March 14, 2019 03:15
@daxian-dbw

Copy link
Copy Markdown
Member

Thanks Ilya (@iSazonov). By reading that guideline, it seems StringBuilder should never be used for P/Invoke as it incurs 4 allocations. I think it make sense to review our uses of it and replace with ArrayPool<char> when appropriate, because we will copy the existing pattern when writing new code. I will take a look at this ReadConsole case in particular.

@iSazonov

Copy link
Copy Markdown
Collaborator Author

Tracking issue #9139

I will take a look at this ReadConsole case in particular.

Please review my old PR first - it is related :-)

@daxian-dbw

Copy link
Copy Markdown
Member

Ilya (@iSazonov) We are using StringBuilder for the [out] string parameter in P/Invoke almost everywhere.

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
…voke (PowerShell#9066)

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.
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