Calling C functions from C#

Well, as you can see I'm a first timer...
how do i call a C function from a C# class?
can it be done in the same project or do I need to reside the C code in a Lib?
a simple example will be appreciated.
Thanks!
udibsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Razzie_Commented:
You will need to use DLLImport, for example:

[DllImport("MyDLL")] // or the complete dll path
public static extern string MyMethod(int x);

and then you can simply use that function in your code:

string s = MyMethod(55);

HTH,

Razzie
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
udibsAuthor Commented:
Must I use a DLL or can I use a static lib?
0
AlexFMCommented:
C# doesn't work with static libraries. PInvoke requires Dll.
0
tomvergoteCommented:
right now you'll have to either use a dll or add a c and a c# project to your solution and reference one from the other (which results in a dynamically loaded dll behind the scenes).

In vs2k5 you'll have the ability to mix code files from different languages in a single project
http://blogs.msdn.com/frankpr/archive/2004/07/27/198918.aspx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.