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. .NET (Core and Framework)
  4. P/Invoke blues: htons & htonl issue

P/Invoke blues: htons & htonl issue

Scheduled Pinned Locked Moved .NET (Core and Framework)
sysadmincsharphelpquestion
4 Posts 2 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.
  • C Offline
    C Offline
    Chen Venkataraman
    wrote on last edited by
    #1

    Ran into this while trying to interoperate with an existing socket based server... i need to construct a buffer that contains mixture of strings & ints - the shorts/ints are stored in network byte order. So, i came with the following P/Invoke declaration in C#. #region P/Invoke stuff [DllImport("ws2_32.dll")] private static extern short htons(short value); [DllImport("ws2_32.dll")] private static extern long htonl(short long); #endregion Unfortunately, the compiler barfs - says "expected class, delegate, enum, interface or struct". It looks like it doesn't like the return value specified as short or int. They are blittable types & i'm not sure what it doesn't like about them. Any pointers? TIA Chen Venkataraman

    C 1 Reply Last reply
    0
    • C Chen Venkataraman

      Ran into this while trying to interoperate with an existing socket based server... i need to construct a buffer that contains mixture of strings & ints - the shorts/ints are stored in network byte order. So, i came with the following P/Invoke declaration in C#. #region P/Invoke stuff [DllImport("ws2_32.dll")] private static extern short htons(short value); [DllImport("ws2_32.dll")] private static extern long htonl(short long); #endregion Unfortunately, the compiler barfs - says "expected class, delegate, enum, interface or struct". It looks like it doesn't like the return value specified as short or int. They are blittable types & i'm not sure what it doesn't like about them. Any pointers? TIA Chen Venkataraman

      C Offline
      C Offline
      Chen Venkataraman
      wrote on last edited by
      #2

      Sorry i had a typo... the code should have been #region P/Invoke stuff [DllImport("ws2_32.dll")] private static extern short htons(short val); [DllImport("ws2_32.dll")] private static extern long htonl(long val); #endregion Chen Venkataraman

      R 1 Reply Last reply
      0
      • C Chen Venkataraman

        Sorry i had a typo... the code should have been #region P/Invoke stuff [DllImport("ws2_32.dll")] private static extern short htons(short val); [DllImport("ws2_32.dll")] private static extern long htonl(long val); #endregion Chen Venkataraman

        R Offline
        R Offline
        Russell Morris
        wrote on last edited by
        #3

        You have to declare them as a part of a class:

        namespace MyNamespace
        {
          public class MyClass
          {
                #region P/Invoke stuff
                [System.Runtime.InteropServices.DllImport("ws2_32.dll")]
                private static extern short htons(short val);
        
                [System.Runtime.InteropServices.DllImport("ws2_32.dll")]
                private static extern long htonl(long val);
                #endregion
        
          }
        }
        

        They cannot be a member of the namespace directly (ie the following code will make the compiler barf in the way that you described)

        namespace MyNamespace
        {
                #region P/Invoke stuff
                [System.Runtime.InteropServices.DllImport("ws2_32.dll")]
                private static extern short htons(short val);
        
                [System.Runtime.InteropServices.DllImport("ws2_32.dll")]
                private static extern long htonl(long val);
                #endregion
        
        }
        

        -- Russell Morris "So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy

        C 1 Reply Last reply
        0
        • R Russell Morris

          You have to declare them as a part of a class:

          namespace MyNamespace
          {
            public class MyClass
            {
                  #region P/Invoke stuff
                  [System.Runtime.InteropServices.DllImport("ws2_32.dll")]
                  private static extern short htons(short val);
          
                  [System.Runtime.InteropServices.DllImport("ws2_32.dll")]
                  private static extern long htonl(long val);
                  #endregion
          
            }
          }
          

          They cannot be a member of the namespace directly (ie the following code will make the compiler barf in the way that you described)

          namespace MyNamespace
          {
                  #region P/Invoke stuff
                  [System.Runtime.InteropServices.DllImport("ws2_32.dll")]
                  private static extern short htons(short val);
          
                  [System.Runtime.InteropServices.DllImport("ws2_32.dll")]
                  private static extern long htonl(long val);
                  #endregion
          
          }
          

          -- Russell Morris "So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy

          C Offline
          C Offline
          Chen Venkataraman
          wrote on last edited by
          #4

          duh... i gave up after struggling with it for a day. And i've done P/Invoke stuff before & usually could make sense of what the compiler thought was wrong - with this one, i just lost it! Silly me! Thanks. Chen Venkataraman

          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