Thread: Can i put C code into my C# project?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    93

    Can i put C code into my C# project?

    Hi,

    Ive got a simple C# program which I am using to read and display a CSV file. Ive writen a C program to do some maths calculations. Im wondering if I can implement this into my C# program or if it must be coded in C#.

    Thanks!

    James.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    If the C code is compiled as a DLL with exported functions, you can use PInvoke functionality to call the C functions from C#.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Sure you can. Add it as a seperate project within your solution and reference it like a DLL.

    Although it sounds like it might just be simpler to re-write the code in C# and add it to your solution...
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by spadez View Post
    Hi,

    Ive got a simple C# program which I am using to read and display a CSV file. Ive writen a C program to do some maths calculations. Im wondering if I can implement this into my C# program or if it must be coded in C#.

    Thanks!

    James.
    Depending on what you're doing, summing up values or only doing calculations on values that match your criteria, C# may be able to do this for you a single nifty one-liner.

    Code:
    IEnumerableCollectionWithValues.Where(x => x % 2 == 0).Aggregate(n => n*n);
    Not completely sure about aggregate's syntax, but pretty sure it's very similar. That particular LINQ query returns all even values to the second power.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    As has already been said, P/Invoke is a nifty way of achieving exactly what you want. Here some interesting background documentation: http://msdn.microsoft.com/en-us/magazine/cc164123.aspx

    However, if all your C code is doing is some Math, it would make better sense to port the code to C#.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Project Euler Solved but want help improving the code Newbie
    By whiterebbit in forum C++ Programming
    Replies: 4
    Last Post: 12-09-2008, 07:00 AM
  2. Advice for Beginner??
    By hawghauler in forum C++ Programming
    Replies: 6
    Last Post: 06-01-2003, 05:33 AM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM