Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Provide custom marshaller for USER_INFO_1 #81705

Closed
AaronRobinsonMSFT opened this issue Feb 6, 2023 · 2 comments · Fixed by #82350
Closed

Provide custom marshaller for USER_INFO_1 #81705

AaronRobinsonMSFT opened this issue Feb 6, 2023 · 2 comments · Fixed by #82350
Labels
area-System.Runtime.InteropServices good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@AaronRobinsonMSFT
Copy link
Member

In PR #81480, the following P/Invoke was failing to compile when targeting .NET 8.

[LibraryImport("netapi32.dll", SetLastError = true)]
internal static partial uint NetUserAdd([MarshalAs(UnmanagedType.LPWStr)] string servername, uint level, ref USER_INFO_1 buf, out uint parm_err);

The failure is because in the current dotnet/runtime official build this P/Invoke is only compiled to target .NET 6 and so the LibraryImport source generated degrades to a DllImport. We should provide a custom marshaller for the struct below

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct USER_INFO_1
{
public string usri1_name;
public string usri1_password;
public uint usri1_password_age;
public uint usri1_priv;
public string usri1_home_dir;
public string usri1_comment;
public uint usri1_flags;
public string usri1_script_path;
}

This scenario could be made to "just work" with the following issue addressed: #81656

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Feb 6, 2023
@ghost
Copy link

ghost commented Feb 6, 2023

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Issue Details

In PR #81480, the following P/Invoke was failing to compile when targeting .NET 8.

[LibraryImport("netapi32.dll", SetLastError = true)]
internal static partial uint NetUserAdd([MarshalAs(UnmanagedType.LPWStr)] string servername, uint level, ref USER_INFO_1 buf, out uint parm_err);

The failure is because in the current dotnet/runtime official build this P/Invoke is only compiled to target .NET 6 and so the LibraryImport source generated degrades to a DllImport. We should provide a custom marshaller for the struct below

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct USER_INFO_1
{
public string usri1_name;
public string usri1_password;
public uint usri1_password_age;
public uint usri1_priv;
public string usri1_home_dir;
public string usri1_comment;
public uint usri1_flags;
public string usri1_script_path;
}

This scenario could be made to "just work" with the following issue addressed: #81656

Author: AaronRobinsonMSFT
Assignees: -
Labels:

area-System.Runtime.InteropServices

Milestone: -

@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 8.0.0 milestone Feb 6, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Feb 6, 2023
@AaronRobinsonMSFT AaronRobinsonMSFT added the good first issue Issue should be easy to implement, good for first-time contributors label Feb 6, 2023
@elinor-fung
Copy link
Member

For anyone that might want to pick this up, here is an example of a struct with custom marshalling used in downlevel and current targets:

#if NET7_0_OR_GREATER
[NativeMarshalling(typeof(Marshaller))]
#endif
[StructLayout(LayoutKind.Sequential)]
internal struct EvtRpcLogin

@elinor-fung elinor-fung added the help wanted [up-for-grabs] Good issue for external contributors label Feb 17, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Feb 18, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Mar 9, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Apr 9, 2023
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
area-System.Runtime.InteropServices good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants