Thread: Can I integrate C and C# code?

  1. #1
    Registered User stevespai's Avatar
    Join Date
    Jun 2006
    Posts
    23

    Can I integrate C and C# code?

    Hi,

    My question pertains to if it is possible to integrate some existing C code into a C# program. If possible, what method would I use to integrate it? Should I put it into a class? How would I include the libraries so the compiler recognizes that the code is C? Any additional information would be greatly apprecaited.

    Thanks.
    Steve

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you're going to get yelled at for posting the same thing in two threads.

    But to answer. you can integrate them by using a C++/CLI module. C++/CLI can do managed and unmanaged code together in an assembly. The C# can then use the C++ assembly almost as if it is C# code
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User stevespai's Avatar
    Join Date
    Jun 2006
    Posts
    23
    Sorry about posting in two threads. I should have read the guidlines. I was hoping that if the C# programmers didn't have experience in this, maybe the C programmers would.
    Last edited by stevespai; 07-25-2006 at 05:46 PM.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    oh, no prob. Just be glad I got to you first Others throw punches.

    I've been using C++/CLI for a couple months now. Many of the people here hate it, but I see it as extremely useful for bringing existing code to .NET.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    As Fyb already said, you have to write a dll to integrate your C/C++ code with .NET. You can either write a normal C dll and call it through interop like any other C dll in C#. This is somewhat ugly on the C# side of the program, but you could use the same dll from your C programs. Somewhat nicer from a .NET point of view is writing a managed wrapper, also called a mixed mode dll. C++/CLI compiles to a normal .NET assembly, that looks, behaves and works the same as any dll written in C#. In C++/CLI you can include unmanaged code. Most of the time this is used to bring legacy C code into a new .NET application.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    Registered User stevespai's Avatar
    Join Date
    Jun 2006
    Posts
    23
    Great, thanks for the info. I'll look into it and post with any more questions.

Popular pages Recent additions subscribe to a feed