ClangSharp icon indicating copy to clipboard operation
ClangSharp copied to clipboard

Add support for translating raw C source files into C# code.

Open AraHaan opened this issue 3 years ago • 2 comments

Let's take for example a library like coolscrollbars (written in C but compiles to a static library, supports only x86 currently, uses static c runtime, is untested on x64 and arm64, and unmaintained for running on Windows Vista or newer). As such I could use something like ClangSharp to convert its actual C code into complete C# code that p/invokes the Windows API's it uses without being forced to update the C code (which I did not write) and then compile and test it as a dll with dll c runtime as well as build it for x86, x64, and arm64 where it might be impossible to test and see if it will work for all 3 of those cpu types (I lack a Windows PC that is arm64) to be able to test all 3 targets.

The library used as an example uses win32 apis that will: hook into the WndProc of an hWND and then override the scrollbars the OS draws to allow custom drawing of them (where the application fully takes over on it). I need it in my case to override the default scrollbars in winforms controls like ListView, and Panels (to name a few controls) without a few "half working" hacks to try to get a manually drawn scrollbar. So I feel like ClangSharp should have 2 modes, generating p/invokes, and the other for essentially migrating C/C++ code to C# without needing to build the actual C/C++ code and needing to distribute them (as it might be a pain to setup building for x86, x64, and arm64 c++ in github actions for example just to package the built binaries inside of a nuget package).

AraHaan avatar Oct 07 '22 20:10 AraHaan

There is very limited support for this already (see here) but it is unlikely to work for very complex cases like entire programs (these code paths iirc have been added for the smaller helper functions/macros that tend to sneak into headers).

Making a full-blown C-to-C# converter is likely something that is infeasible. However, if you're willing to contribute better support here I'm sure it'd be well-received.

Perksey avatar Oct 08 '22 18:10 Perksey

I will see what can be done to support such a thing.

AraHaan avatar Oct 09 '22 00:10 AraHaan