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. include a file

include a file

Scheduled Pinned Locked Moved C#
csharpquestion
8 Posts 6 Posters 0 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.
  • _ Offline
    _ Offline
    __John_
    wrote on last edited by
    #1

    Hi, I am working on a c# app that talks to an external device via a comm port. The external device comes with a header file that defines the commands that can be sent and the possible return values, all as #define's. Is there a way to get thease #define's in to my c# app, other than copying and pasting? Thanks.

    “If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford

    _ V P 3 Replies Last reply
    0
    • _ __John_

      Hi, I am working on a c# app that talks to an external device via a comm port. The external device comes with a header file that defines the commands that can be sent and the possible return values, all as #define's. Is there a way to get thease #define's in to my c# app, other than copying and pasting? Thanks.

      “If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford

      _ Offline
      _ Offline
      __John_
      wrote on last edited by
      #2

      Ok, even if i get the header included some how, its still not going to work... MSDN: The #define directive cannot be used to declare constant values as is typically done in C and C++. What a crock.

      “If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford

      D 1 Reply Last reply
      0
      • _ __John_

        Hi, I am working on a c# app that talks to an external device via a comm port. The external device comes with a header file that defines the commands that can be sent and the possible return values, all as #define's. Is there a way to get thease #define's in to my c# app, other than copying and pasting? Thanks.

        “If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford

        V Offline
        V Offline
        VJ Reddy
        wrote on last edited by
        #3

        I think if the requirement is to call a function from an unmanaged DLL then DllImportAttribute class explained here http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx[^] may be helpful. #define being a preprocessor directive as explained here http://msdn.microsoft.com/en-us/library/yt3yck0x(v=vs.90).aspx[^] may not be suitable for the specified purpose.

        1 Reply Last reply
        0
        • _ __John_

          Hi, I am working on a c# app that talks to an external device via a comm port. The external device comes with a header file that defines the commands that can be sent and the possible return values, all as #define's. Is there a way to get thease #define's in to my c# app, other than copying and pasting? Thanks.

          “If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Well... the quick and dirty way would be to pass it through a C pre-processor. However, you may want to limit the scope of what you do that way. Without seeing what's actually in the header file and which parts you need, I'll show a simple example of what can be done: Example.h

          # define OPEN 1

          define CLOSE 2

          define READ 3

          define WRITE 4

          Example.csi (choose an extension)

          namespace Support
          {
          public static class Command
          {
          public const int Open = OPEN ;
          public const int Close = CLOSE ;
          public const int Read = READ ;
          public const int Write = WRITE ;
          }

          // or

          public enum Command
          {
          Open = OPEN
          ,
          Close = CLOSE
          ,
          Read = READ
          ,
          Write = WRITE
          }
          }

          Pass them through a C pre-processor, this uses the Visual C/C++ one: cl /nologo /C /EP /P /FIC:Example.h /FiExample.cs Example.csi Results in: Example.cs

          namespace Support
          {
          public static class Command
          {
          public const int Open = 1 ;
          public const int Close = 2 ;
          public const int Read = 3 ;
          public const int Write = 4 ;
          }

          // or

          public enum Command
          {
          Open = 1
          ,
          Close = 2
          ,
          Read = 3
          ,
          Write = 4
          }
          }

          You can then include this file in your project. If needed, you can have a pre-build step that performs the above pre-processing. Notes: 0) The pre-C# file (csi) should not contain any directives 1) You should include a notice that it was generated 2) You could also set the file to read-only 3) Use a partial class if you want this to be part of a larger class so you don't pass the whole thing through the pre-processor See also my Implanting Common Code in Unrelated Classes[^]

          J 1 Reply Last reply
          0
          • _ __John_

            Ok, even if i get the header included some how, its still not going to work... MSDN: The #define directive cannot be used to declare constant values as is typically done in C and C++. What a crock.

            “If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            What's a crock is that in the original C/C++ files, the original programmer made the mistake of depending on a compiler-specific feature to write code. I know full well that everyone used to use #define to declare their constants in C/C++, even when they had a perfectly good "const" keyword, but in doing so, the made the code specific to the C/C++ compiler. Sorry, but don't blame C# for the failings of some other programmer in a previoud language.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            P 1 Reply Last reply
            0
            • D Dave Kreskowiak

              What's a crock is that in the original C/C++ files, the original programmer made the mistake of depending on a compiler-specific feature to write code. I know full well that everyone used to use #define to declare their constants in C/C++, even when they had a perfectly good "const" keyword, but in doing so, the made the code specific to the C/C++ compiler. Sorry, but don't blame C# for the failings of some other programmer in a previoud language.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              At one place where we did C, the "guru" specified that we should use defines (rather than consts etc.) as much as possible because they don't use memory. :-D

              B 1 Reply Last reply
              0
              • P PIEBALDconsult

                At one place where we did C, the "guru" specified that we should use defines (rather than consts etc.) as much as possible because they don't use memory. :-D

                B Offline
                B Offline
                Big Daddy Farang
                wrote on last edited by
                #7

                Guess I have a lot code to change. :omg:

                BDF I often make very large prints from unexposed film, and every one of them turns out to be a picture of myself as I once dreamed I would be. -- BillWoodruff

                1 Reply Last reply
                0
                • P PIEBALDconsult

                  Well... the quick and dirty way would be to pass it through a C pre-processor. However, you may want to limit the scope of what you do that way. Without seeing what's actually in the header file and which parts you need, I'll show a simple example of what can be done: Example.h

                  # define OPEN 1

                  define CLOSE 2

                  define READ 3

                  define WRITE 4

                  Example.csi (choose an extension)

                  namespace Support
                  {
                  public static class Command
                  {
                  public const int Open = OPEN ;
                  public const int Close = CLOSE ;
                  public const int Read = READ ;
                  public const int Write = WRITE ;
                  }

                  // or

                  public enum Command
                  {
                  Open = OPEN
                  ,
                  Close = CLOSE
                  ,
                  Read = READ
                  ,
                  Write = WRITE
                  }
                  }

                  Pass them through a C pre-processor, this uses the Visual C/C++ one: cl /nologo /C /EP /P /FIC:Example.h /FiExample.cs Example.csi Results in: Example.cs

                  namespace Support
                  {
                  public static class Command
                  {
                  public const int Open = 1 ;
                  public const int Close = 2 ;
                  public const int Read = 3 ;
                  public const int Write = 4 ;
                  }

                  // or

                  public enum Command
                  {
                  Open = 1
                  ,
                  Close = 2
                  ,
                  Read = 3
                  ,
                  Write = 4
                  }
                  }

                  You can then include this file in your project. If needed, you can have a pre-build step that performs the above pre-processing. Notes: 0) The pre-C# file (csi) should not contain any directives 1) You should include a notice that it was generated 2) You could also set the file to read-only 3) Use a partial class if you want this to be part of a larger class so you don't pass the whole thing through the pre-processor See also my Implanting Common Code in Unrelated Classes[^]

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #8

                  PIEBALDconsult wrote:

                  the quick and dirty way would be to pass it through a C pre-processor.

                  That would seem like the ideal solution to me.

                  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