Skip to content

Documentation Improvements around Customizing generated code #606

Closed as not planned

Description

The documentation does not outline exactly how to split the NativeMethods.txt into multiple files. It just says that it is possible.

Additionally, the documentation lists possible ways to customize generated code, but again doesn't list the appropriate settings for the NativeMethods.json.

Activity

  1. added
    bug
    on Jul 18, 2022
  2. elachlan commented on Jul 19, 2022

    @elachlan
    ContributorAuthor

    For the NativeMethods.txt, do you just name them like *.NativeMethods.txt or do the files need to be named all NativeMethods.txt but in separate directories?

    Edit:
    Looking at the code and reading it more carefully, it does look like they have to be in separate directories, would you consider adding support for multiple files if named in the style of *.NativeMethods.txt? I guess support for additional files named that way, or project root would be great.

  3. AArnott commented on Jul 19, 2022

    @AArnott
    Member

    We don't recommend splitting NativeMethods.txt up. At least, I haven't come across any scenario where it would be beneficial. I heard someone suggest a couple weeks ago that perhaps one in each feature directory of a library would make sense, where the APIs used by that feature would be listed. But that's not really sustainable because there is no enforcement, and it would be a lot of extra bookkeeping on the part of the consumer to try to avoid using APIs that weren't asked for in their local file.
    The reason we support multiple files is for Shared Project scenarios, where legitimately shared code requires p/invokes and should carry its own NativeMethods.txt file so that anyone who consumes the Shared Project will automatically generate the APIs it needs.

    But yes, we could document it better.

    The nativemethods.json isn't documented much except to include the $schema field, which is all you need in VS to get Intellisense in the file with docs that teach you what is available and what it does. Have you found that to be insufficient?

  4. elachlan commented on Jul 19, 2022

    @elachlan
    ContributorAuthor

    Thanks, that all makes sense. Possibly append the multiple NativeMethods.txt section with your recommendation above?

    In Winforms, the idea was to have a NativeMethods.txt file in each interop dll directory. I imagine the txt file will end up being several thousands of lines long if it was centralized. Is the idea of breaking the files up per import library not a good idea?

  5. AArnott commented on Jul 20, 2022

    @AArnott
    Member

    the idea was to have a NativeMethods.txt file in each interop dll directory

    Would you still have a whole directory per each interop dll if CsWin32 was generating most of the code? Ideally, you have little or no interop to write yourself, unless you're writing a higher level set of classes.

  6. elachlan commented on Jul 20, 2022

    @elachlan
    ContributorAuthor

    The winforms team looks like it will go with a single NativeMethods.txt.

    That being said, In Winforms IHandle is used on forms/controls/etc to provide access to the objects Handle. Winforms then has code like this:

    public unsafe static BOOL ValidateRect(IHandle hWnd, RECT* lpRect)
    {
        BOOL result = ValidateRect(hWnd.Handle, lpRect);
        GC.KeepAlive(hWnd);
        return result;
    }

    I brought this up in #602. It was suggested there was an alternative way using a struct.

    I suspect the winforms team will want to keep this structure. So the per interop dll folders might end up staying.

    If CsWin32 provided a similar interface and methods, then I'd image the folders wouldn't be needed.

  7. AArnott commented on Jul 20, 2022

    @AArnott
    Member

    Thanks. We are still reviewing the IHandle proposal. Not sure what CsWin32 will do to help with that yet.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions