Is it possible to embed C code in a C# project?

前端 未结 4 882
攒了一身酷
攒了一身酷 2020-12-07 01:43

I know that it\'s possible to compile my C code into a dll, and then use P/Invoke to call that code.

What I wondered if it was possible to have a chunk of C code emb

相关标签:
4条回答
  • 2020-12-07 01:58

    You can write and compile your C code as a normal (non-.NET) assembly, then P/Invoke it:

    [DllImport ("mylib.dll")]
    private static extern int do_something_in_c(int i);
    
    public int DoSomething(int value)
    {
        return do_something_in_c(value);
    }
    
    0 讨论(0)
  • 2020-12-07 01:59

    It's not possible. While C# supports unsafe code (pointers), it is not backwards compatible with C or C++

    0 讨论(0)
  • 2020-12-07 02:00

    IMHO, it is not possible, since C is an unsafe and unmanaged language. Besides, C# has all the important features of C except pointers.

    0 讨论(0)
  • 2020-12-07 02:07

    It is possible to create a mixed-mode assembly (that is, one that has both managed and native code), but only the C++/CLI compiler can produce one of these. What you're looking to do is not supported by the C# compiler.

    0 讨论(0)
提交回复
热议问题