-
-
Notifications
You must be signed in to change notification settings - Fork 224
Generate p/invoke methods automatically #56
Comments
This is the tool I am talking about: https://github.com/sharpdx/SharpDX/tree/master/Source/Tools/SharpGen Notice how it also has a CPP parser etc. |
Thanks for the idea. Anything we can do to expedite increasing our API coverage with less effort is worth investigating. FWIW, I've already been using pinvoke.net and the code generation feature of the P/Invoke Interop Assistant as a tool to help generate the signatures and struct types. I've been disappointed by the code generation of that tool though, as the method is may not be in the database, or it fails to parse the header files and produces nothing, or it produces a bunch of signatures that have to be fixed up simply because the header file used Ultimately the very best generation tool in the world would still emit methods that would need to be hand-inspected, Hopefully SharpGen does a better job. But even if it did, I'm not sure we'd use it to replace the process of maintaining source code for the library with simply generating the source code at build time. Not sure if that was what you were suggesting. If you (or anyone else) can try out |
As for I was considering writing a plugin which takes P/Invoke Interop Assistant's output, parsing it with Roslyn, and then using Roslyn, re-transpiling it into something we want (with fixes and tweaks). |
No. SafeHandle is supposed to represent native handles -- things that some sort of native release method must be called on. IntPtr can represent many things, including just a pointer-sized number (like the size of a buffer). Other times when they do represent handles, such as for structs that are initialized by the native code, the fields can't be SafeHandles because .NET can't marshal that way. Massaging the output of a code generation tool sounds interesting. This is to aid us in checking in source code, I assume? Or is it to replace our source code? I'd be much more quick to adopt the former (as a tool to help us author source code) than the latter (a replacement for source code). |
I think it is worth investing in Roslyn to see if it can replace our code. If not, it can always aid us. I am quite experienced in Roslyn, and think I can make a program that can very easily generate automatic code that follows exactly the same structure as we use now. But what to do about |
I don't think that a generated solution would be a good one in the long run. Lot of API require thinking on how to adapt them to good C#. The only thing something automated would be able to produce is bad C# :
And that's only the case I can think of that poped from the API already in there, i'm sure we will encounter a LOT more. That said a selenium based tool to generate at least a good baseline from the MSDN page would be extremely handy. Especially around documentation, cleaning it up by hand is ok but the copy-paste from MSDN part is just boring mechanical work. |
Alright, let me try to gather more intel then given the above information (thanks by the way).
Thanks for your clarification so far. |
The same native library can contain lots of functions that take a
Some are, some aren't completely depends on the API, we would have lots of special cases that can't be generated, and the more special casing the less useful an automated too would be.
It's possible but it might polute the API surface with lots of variants that have no chance to ever be used.
Documentation sometimes link to generic articles on MSDN, references constants values one by one (where we would use enums and so just reference it), specifies what happens when some values are null that we marshalled as There are a few changes like that that make the doc easier to consume for libraries users. (Nothing really critical but it's nice to have the library go the extra mile and provide adapted documentation that take the marshaling in account) |
Hmmm, I think we can close this then. Damn! Really wanted it to work somehow, but I get your points. |
I may have missed part of the conversation somewhere. Which points condemned this idea? |
Oh, nm. I got my issues confused. |
I'm pleased to say we have a very good option opening up to automatically generate All The Things. Reactivating to track. |
This is now closed by #565. |
There is a library out there called SharpDX. They allow full DirectX 11 support for C# through PInvoke-ish APIs, but the cool thing is that they are all auto-generated and auto-converted to C#.
Could we instead of what we are doing now get inspiration from them, and make a tool which auto-generates all C# code?
It would also make it a lot easier to maintain.
The text was updated successfully, but these errors were encountered: