P/Invoke blues: htons & htonl issue
-
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 -
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 VenkataramanSorry 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 -
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 VenkataramanYou 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
-
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
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