469,927 Members | 1,672 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Call a C dll from C#

Hi

how do I call functions in a DLL written in C from C#.
Eg. in Java it is necessary to use jni - is there something similar in C#?

Thanks,
Peter
Apr 19 '06 #1
4 1522
"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Hi

how do I call functions in a DLL written in C from C#.
Eg. in Java it is necessary to use jni - is there something similar in C#?
Here's an example

using System.Runtime.InteropServices;

[DllImport("kernel32", EntryPoint= "GetComputerNameA")]
private static extern int GetComputerName(String lpBuffer, ref int nSize);

Thanks,
Peter

Apr 19 '06 #2
Use interop for this. Details are here
http://samples.gotdotnet.com/quickst..._Overview.aspx
how do I call functions in a DLL written in C from C#.
Eg. in Java it is necessary to use jni - is there something similar in C#?


--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Apr 19 '06 #3
"Michael C" <no****@nospam.com> wrote in message
news:e2**************@TK2MSFTNGP04.phx.gbl...
"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Hi

how do I call functions in a DLL written in C from C#.
Eg. in Java it is necessary to use jni - is there something similar in
C#?
Here's an example

using System.Runtime.InteropServices;

[DllImport("kernel32", EntryPoint= "GetComputerNameA")]
private static extern int GetComputerName(String lpBuffer, ref int nSize);


Oops, that wouldn't actually work because you can't pass a string in like
that but substitute a byte array or string builder and it would.


Thanks,
Peter


Apr 19 '06 #4
If you are using .Net 2.0 and VS2005 its a lot easier to deal with
interfacing managed code with unmanaged code. It supports pragmas for
switching back and forth.
#pragma managed
#pragma unmanaged

This way you can write an managed C++ interface dll and just include the
..h files from the C code. Inside the the dll you can pretty much write
managed code while using the unmanged classes and methods. It magically
figures out how to do the marshaling for you.
From C# you can just use the dll and call the methods directly since
they are managed and you don't need to use pInvoke. There is also a
speed benefit for doing this because the compiler optimizes the marshaling.

I figured a lot about this by looking into the "#pragma managed" help
documentation and other links that VS2005 provided.

Hope this helps.
Leon Lambert

Michael Nemtsev wrote:
Use interop for this. Details are here
http://samples.gotdotnet.com/quickst..._Overview.aspx
how do I call functions in a DLL written in C from C#.
Eg. in Java it is necessary to use jni - is there something similar in C#?

Apr 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Fabian Müller | last post: by
35 posts views Thread by hasho | last post: by
13 posts views Thread by mitchellpal | last post: by
3 posts views Thread by cberthu | last post: by
9 posts views Thread by CryptiqueGuy | last post: by
12 posts views Thread by Rahul | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.