Interfacing with C from C#
-
hello, is there any way that we can use C language code in C#? What are the alternates of header files in C#? and How to convert a header file to a namespace if namespaces are the alternatives to header files? regards
-
hello, is there any way that we can use C language code in C#? What are the alternates of header files in C#? and How to convert a header file to a namespace if namespaces are the alternatives to header files? regards
You can declare C functions in C# using the extern keyword along with the DllImport attribute. Once the function is declared, you just call into it. For example:
using System.Runtime.InteropServices;
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);MessageBox (0,"API Message Box","API Demo",0);
See the following and other articles on the topic: http://www.thecodeproject.com/csharp/c__and_api.asp There is no need for headers in C#. You just put all your code in the .cs class. You generally wrap your code within a namespace as a way of organizing things and to avoid name conflicts with other code -- it doesn't really correspond to a header.
-
You can declare C functions in C# using the extern keyword along with the DllImport attribute. Once the function is declared, you just call into it. For example:
using System.Runtime.InteropServices;
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);MessageBox (0,"API Message Box","API Demo",0);
See the following and other articles on the topic: http://www.thecodeproject.com/csharp/c__and_api.asp There is no need for headers in C#. You just put all your code in the .cs class. You generally wrap your code within a namespace as a way of organizing things and to avoid name conflicts with other code -- it doesn't really correspond to a header.
I am not using any C functions from any dll. Actually i have a packet driver library (which is completely written in C). I want to use this library from a C# file. I have the source code of the library, so, I have to supply header files definitions of 'structs' used in function calls and these function calls are linked statically. Simply i want to use the library from a C# file as i use it from any other C file. Tell me if i m unable to explain my question or some extra details are required. regards.
-
I am not using any C functions from any dll. Actually i have a packet driver library (which is completely written in C). I want to use this library from a C# file. I have the source code of the library, so, I have to supply header files definitions of 'structs' used in function calls and these function calls are linked statically. Simply i want to use the library from a C# file as i use it from any other C file. Tell me if i m unable to explain my question or some extra details are required. regards.
If this library has only a few entry points, then it's worth using P/Invoke to use it from C#. If the library has many entry points, or requires a significant amount of structure marshaling, then use MC++ instead. With MC++, you can mix managed code and native code without reexposing signatures.
-
If this library has only a few entry points, then it's worth using P/Invoke to use it from C#. If the library has many entry points, or requires a significant amount of structure marshaling, then use MC++ instead. With MC++, you can mix managed code and native code without reexposing signatures.
thanks as it came to me, i switched to visual C++ .net. I am now able to mix the unmanaged code of the library and the managed code of .net. It is working nice. But can you give me some favour by telling that whether there is any "Network packet capture" support in .net library? Any namespace, classes etc.
-
thanks as it came to me, i switched to visual C++ .net. I am now able to mix the unmanaged code of the library and the managed code of .net. It is working nice. But can you give me some favour by telling that whether there is any "Network packet capture" support in .net library? Any namespace, classes etc.
hashimsaleem wrote: But can you give me some favour by telling that whether there is any "Network packet capture" support in .net library? Although the System.Net.Sockets.TcpClient wraps sockets to read/write packets, I believe this is not what you are looking for. I believe that what you need is at a lower-level, much like "Network adapter" and, at this point, there is no better substitute than the Windows DDK (plain WIN32 C code, as you may know).