Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Interfacing with C from C#

Interfacing with C from C#

Scheduled Pinned Locked Moved C#
csharptutorialquestion
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hashim Saleem
    wrote on last edited by
    #1

    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

    K 1 Reply Last reply
    0
    • H Hashim Saleem

      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

      K Offline
      K Offline
      Katalyst
      wrote on last edited by
      #2

      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.

      H 1 Reply Last reply
      0
      • K Katalyst

        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.

        H Offline
        H Offline
        Hashim Saleem
        wrote on last edited by
        #3

        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.

        S 1 Reply Last reply
        0
        • H Hashim Saleem

          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.

          S Offline
          S Offline
          Stephane Rodriguez
          wrote on last edited by
          #4

          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.

          H 1 Reply Last reply
          0
          • S Stephane Rodriguez

            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.

            H Offline
            H Offline
            Hashim Saleem
            wrote on last edited by
            #5

            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.

            S 1 Reply Last reply
            0
            • H Hashim Saleem

              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.

              S Offline
              S Offline
              Stephane Rodriguez
              wrote on last edited by
              #6

              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).

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups