]> git.meshlink.io Git - catta/commitdiff
* add byte[] overloads for EntryGroup methods
authorJames Willcox <snopr@snorp.net>
Thu, 17 Nov 2005 21:11:13 +0000 (21:11 +0000)
committerJames Willcox <snopr@snorp.net>
Thu, 17 Nov 2005 21:11:13 +0000 (21:11 +0000)
* cleanup string marshalling

git-svn-id: file:///home/lennart/svn/public/avahi/trunk@999 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

avahi-sharp/AddressResolver.cs
avahi-sharp/AvahiTest.cs
avahi-sharp/DomainBrowser.cs
avahi-sharp/EntryGroup.cs
avahi-sharp/HostNameResolver.cs
avahi-sharp/RecordBrowser.cs
avahi-sharp/ServiceBrowser.cs
avahi-sharp/ServiceResolver.cs
avahi-sharp/ServiceTypeBrowser.cs
avahi-sharp/Utility.cs

index 63dc33e74228932a6ae1ac8d88bdebe82cde7911..3025102c4cce16cf06338203756b9bb5a7d34b90 100644 (file)
@@ -151,6 +151,9 @@ namespace Avahi
             lock (client) {
                 handle = avahi_address_resolver_new (client.Handle, iface, proto, addrPtr, flags,
                                                      cb, IntPtr.Zero);
+
+                if (handle == IntPtr.Zero)
+                    client.ThrowError ();
             }
             
             Utility.Free (addrPtr);
index df95b906c6406ddea36e76649cadb6974cc135fa..99cd6e26a4966a3e24e062de1390878f2d923d9a 100644 (file)
@@ -37,7 +37,7 @@ public class AvahiTest {
         EntryGroup eg = new EntryGroup (client);
         eg.StateChanged += OnEntryGroupChanged;
         eg.AddService ("foobar2", "_daap._tcp", client.DomainName,
-                       444, new string[] { "foo", "bar", "baz" });
+                       444, new string[] { "foo=stuff", "bar=stuff2", "baz=stuff3" });
         eg.Commit ();
         Console.WriteLine ("Press enter to quit");
         Console.ReadLine ();
@@ -99,10 +99,17 @@ public class AvahiTest {
         foreach (byte[] bytes in args.Service.Text) {
             Console.WriteLine ("Text: " + Encoding.UTF8.GetString (bytes));
         }
+
         AddressResolver ar = new AddressResolver (client, args.Service.Address);
         objects.Add (ar);
-        
+
         ar.Found += OnAddressResolved;
+        ar.Failed += OnAddressResolverFailed;
+    }
+
+    private static void OnAddressResolverFailed (object o, ErrorCodeArgs args)
+    {
+        Console.WriteLine ("Failed to resolve '{0}': {1}", (o as AddressResolver).Address, args.ErrorCode);
     }
 
     private static void OnAddressResolved (object o, HostAddressArgs args)
index 50ac75778db363ac347cac0881b4add0aaaf4ee4..50da54e7306fb0b1e03f8ccf3c85285ee31e165a 100644 (file)
@@ -22,6 +22,7 @@
 using System;
 using System.Collections;
 using System.Runtime.InteropServices;
+using System.Text;
 
 namespace Avahi
 {
@@ -78,7 +79,7 @@ namespace Avahi
         
         [DllImport ("avahi-client")]
         private static extern IntPtr avahi_domain_browser_new (IntPtr client, int iface, int proto,
-                                                               IntPtr domain, int btype, LookupFlags flags,
+                                                               byte[] domain, int btype, LookupFlags flags,
                                                                DomainBrowserCallback cb,
                                                                IntPtr userdata);
 
@@ -147,10 +148,12 @@ namespace Avahi
                 return;
 
             lock (client) {
-                IntPtr domainPtr = Utility.StringToPtr (domain);
-                handle = avahi_domain_browser_new (client.Handle, iface, (int) proto, domainPtr, (int) btype, flags,
+                handle = avahi_domain_browser_new (client.Handle, iface, (int) proto,
+                                                   Utility.StringToBytes (domain), (int) btype, flags,
                                                    cb, IntPtr.Zero);
-                Utility.Free (domainPtr);
+
+                if (handle == IntPtr.Zero)
+                    client.ThrowError ();
             }
         }
 
index eb7d9e2725ebf5a35731dad73630181011512cfb..2ebecc2afee1b690192531a0a5afd3ce4b8e43d6 100644 (file)
@@ -22,6 +22,7 @@
 using System;
 using System.Net;
 using System.Runtime.InteropServices;
+using System.Text;
 using Mono.Unix;
 
 namespace Avahi
@@ -90,28 +91,28 @@ namespace Avahi
 
         [DllImport ("avahi-client")]
         private static extern int avahi_entry_group_add_service_strlst (IntPtr group, int iface, Protocol proto,
-                                                                        PublishFlags flags, IntPtr name, IntPtr type,
-                                                                        IntPtr domain, IntPtr host, UInt16 port,
+                                                                        PublishFlags flags, byte[] name, byte[] type,
+                                                                        byte[] domain, byte[] host, UInt16 port,
                                                                         IntPtr strlst);
 
         [DllImport ("avahi-client")]
         private static extern int avahi_entry_group_update_service_strlst (IntPtr group, int iface, Protocol proto,
-                                                                           PublishFlags flags, IntPtr name,
-                                                                           IntPtr type, IntPtr domain, IntPtr strlst);
+                                                                           PublishFlags flags, byte[] name,
+                                                                           byte[] type, byte[] domain, IntPtr strlst);
 
         [DllImport ("avahi-client")]
         private static extern int avahi_entry_group_add_service_subtype (IntPtr group, int iface, Protocol proto,
-                                                                         PublishFlags flags, IntPtr name, IntPtr type,
-                                                                         IntPtr domain, IntPtr subtype);
+                                                                         PublishFlags flags, byte[] name, byte[] type,
+                                                                         byte[] domain, byte[] subtype);
 
         [DllImport ("avahi-client")]
         private static extern int avahi_entry_group_add_address (IntPtr group, int iface, Protocol proto,
-                                                                 PublishFlags flags, IntPtr name, IntPtr address);
+                                                                 PublishFlags flags, byte[] name, IntPtr address);
 
 
         [DllImport ("avahi-client")]
         private static extern int avahi_entry_group_add_record (IntPtr group, int iface, Protocol proto,
-                                                                PublishFlags flags, IntPtr name, RecordClass clazz,
+                                                                PublishFlags flags, byte[] name, RecordClass clazz,
                                                                 RecordType type, uint ttl, byte[] rdata, int size);
         
         [DllImport ("avahi-client")]
@@ -121,23 +122,23 @@ namespace Avahi
         private static extern IntPtr avahi_string_list_new (IntPtr txt);
 
         [DllImport ("avahi-common")]
-        private static extern IntPtr avahi_string_list_add (IntPtr list, IntPtr txt);
+        private static extern IntPtr avahi_string_list_add (IntPtr list, byte[] txt);
 
         [DllImport ("avahi-common")]
         private static extern void avahi_string_list_free (IntPtr list);
 
         [DllImport ("avahi-common")]
-        private static extern int avahi_service_name_join (IntPtr buf, int len, IntPtr name, IntPtr type,
-                                                           IntPtr domain);
+        private static extern int avahi_service_name_join (IntPtr buf, int len, byte[] name, byte[] type,
+                                                           byte[] domain);
 
         [DllImport ("avahi-common")]
-        private static extern int avahi_service_name_split (IntPtr service, IntPtr name, int name_len,
+        private static extern int avahi_service_name_split (byte[] service, IntPtr name, int name_len,
                                                             IntPtr type, int type_len,
                                                             IntPtr domain, int domain_len);
 
         
         [DllImport ("avahi-common")]
-        private static extern IntPtr avahi_alternative_service_name (IntPtr name);
+        private static extern IntPtr avahi_alternative_service_name (byte[] name);
 
         public event EntryGroupStateHandler StateChanged;
         
@@ -221,32 +222,41 @@ namespace Avahi
 
             if (txt != null) {
                 foreach (string item in txt) {
-                    IntPtr itemPtr = Utility.StringToPtr (item);
-                    list = avahi_string_list_add (list, itemPtr);
-                    Utility.Free (itemPtr);
+                    list = avahi_string_list_add (list, Utility.StringToBytes (item));
                 }
             }
 
-            IntPtr namePtr = Utility.StringToPtr (name);
-            IntPtr typePtr = Utility.StringToPtr (type);
-            IntPtr domainPtr = Utility.StringToPtr (domain);
-            IntPtr hostPtr = Utility.StringToPtr (host);
-
-            lock (client) {
-                int ret = avahi_entry_group_add_service_strlst (handle, iface, proto, flags, namePtr, typePtr, domainPtr,
-                                                                hostPtr, port, list);
+            AddService (iface, proto, flags, name, type, domain, host, port, list);
+        }
 
-                Utility.Free (namePtr);
-                Utility.Free (typePtr);
-                Utility.Free (domainPtr);
-                Utility.Free (hostPtr);
-                avahi_string_list_free (list);
+        public void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type, string domain,
+                                string host, UInt16 port, params byte[][] txt)
+        {
+            IntPtr list = avahi_string_list_new (IntPtr.Zero);
 
-                if (ret < 0) {
-                    client.ThrowError ();
+            if (txt != null) {
+                foreach (byte[] item in txt) {
+                    list = avahi_string_list_add (list, item);
                 }
             }
+
+            AddService (iface, proto, flags, name, type, domain, host, port, list);
+        }
+
+        private void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type,
+                                 string domain, string host, UInt16 port, IntPtr list)
+        {
+            int ret = avahi_entry_group_add_service_strlst (handle, iface, proto, flags,
+                                                            Utility.StringToBytes (name),
+                                                            Utility.StringToBytes (type),
+                                                            Utility.StringToBytes (domain),
+                                                            Utility.StringToBytes (host), port, list);
+            
+            avahi_string_list_free (list);
             
+            if (ret < 0) {
+                client.ThrowError ();
+            }
         }
 
         public void UpdateService (string name, string type, string domain, params string[] txt)
@@ -261,23 +271,17 @@ namespace Avahi
 
             if (txt != null) {
                 foreach (string item in txt) {
-                    IntPtr itemPtr = Utility.StringToPtr (item);
-                    list = avahi_string_list_add (list, itemPtr);
-                    Utility.Free (itemPtr);
+                    list = avahi_string_list_add (list, Utility.StringToBytes (item));
                 }
             }
 
-            IntPtr namePtr = Utility.StringToPtr (name);
-            IntPtr typePtr = Utility.StringToPtr (type);
-            IntPtr domainPtr = Utility.StringToPtr (domain);
-
             lock (client) {
-                int ret = avahi_entry_group_update_service_strlst (handle, iface, proto, flags, namePtr, typePtr, domainPtr, list);
+                int ret = avahi_entry_group_update_service_strlst (handle, iface, proto, flags,
+                                                                   Utility.StringToBytes (name),
+                                                                   Utility.StringToBytes (type),
+                                                                   Utility.StringToBytes (domain),
+                                                                   list);
 
-                Utility.Free (namePtr);
-                Utility.Free (typePtr);
-                Utility.Free (domainPtr);
-                
                 avahi_string_list_free (list);
 
                 if (ret < 0) {
@@ -294,20 +298,13 @@ namespace Avahi
         public void AddServiceSubtype (int iface, Protocol proto, PublishFlags flags, string name,
                                        string type, string domain, string subtype)
         {
-            IntPtr namePtr = Utility.StringToPtr (name);
-            IntPtr typePtr = Utility.StringToPtr (type);
-            IntPtr domainPtr = Utility.StringToPtr (domain);
-            IntPtr subtypePtr = Utility.StringToPtr (subtype);
-
             lock (client) {
-                int ret = avahi_entry_group_add_service_subtype (handle, iface, proto, flags, namePtr,
-                                                                 typePtr, domainPtr, subtypePtr);
+                int ret = avahi_entry_group_add_service_subtype (handle, iface, proto, flags,
+                                                                 Utility.StringToBytes (name),
+                                                                 Utility.StringToBytes (type),
+                                                                 Utility.StringToBytes (domain),
+                                                                 Utility.StringToBytes (subtype));
 
-                Utility.Free (namePtr);
-                Utility.Free (typePtr);
-                Utility.Free (domainPtr);
-                Utility.Free (subtypePtr);
-                
                 if (ret < 0) {
                     client.ThrowError ();
                 }
@@ -321,13 +318,12 @@ namespace Avahi
 
         public void AddAddress (int iface, Protocol proto, PublishFlags flags, string name, IPAddress address)
         {
-            IntPtr namePtr = Utility.StringToPtr (name);
             IntPtr addressPtr = Utility.AddressToPtr (address);
 
             lock (client) {
-                int ret = avahi_entry_group_add_address (handle, iface, proto, flags, namePtr, addressPtr);
+                int ret = avahi_entry_group_add_address (handle, iface, proto, flags,
+                                                         Utility.StringToBytes (name), addressPtr);
 
-                Utility.Free (namePtr);
                 Utility.Free (addressPtr);
 
                 if (ret < 0) {
@@ -344,14 +340,11 @@ namespace Avahi
         public void AddRecord (int iface, Protocol proto, PublishFlags flags, string name,
                                RecordClass clazz, RecordType type, uint ttl, byte[] rdata, int length)
         {
-            IntPtr namePtr = Utility.StringToPtr (name);
-
             lock (client) {
-                int ret = avahi_entry_group_add_record (handle, iface, proto, flags, namePtr,
+                int ret = avahi_entry_group_add_record (handle, iface, proto, flags,
+                                                        Utility.StringToBytes (name),
                                                         clazz, type, ttl, rdata, length);
 
-                Utility.Free (namePtr);
-                
                 if (ret < 0) {
                     client.ThrowError ();
                 }
@@ -363,15 +356,10 @@ namespace Avahi
             int len = 4 * (name.Length + type.Length + domain.Length) + 4;
             IntPtr buf = Stdlib.malloc ((ulong) len);
 
-            IntPtr namePtr = Utility.StringToPtr (name);
-            IntPtr typePtr = Utility.StringToPtr (type);
-            IntPtr domainPtr = Utility.StringToPtr (domain);
-
-            int ret = avahi_service_name_join (buf, len, namePtr, typePtr, domainPtr);
-
-            Utility.Free (namePtr);
-            Utility.Free (typePtr);
-            Utility.Free (domainPtr);
+            int ret = avahi_service_name_join (buf, len,
+                                               Utility.StringToBytes (name),
+                                               Utility.StringToBytes (type),
+                                               Utility.StringToBytes (domain));
 
             if (ret < 0) {
                 Utility.Free (buf);
@@ -388,15 +376,13 @@ namespace Avahi
         {
             int len = 1024;
 
-            IntPtr servicePtr = Utility.StringToPtr (service);
             IntPtr namePtr = Stdlib.malloc ((ulong) len);
             IntPtr typePtr = Stdlib.malloc ((ulong) len);
             IntPtr domainPtr = Stdlib.malloc ((ulong) len);
             
-            int ret = avahi_service_name_split (servicePtr, namePtr, len, typePtr, len, domainPtr, len);
+            int ret = avahi_service_name_split (Utility.StringToBytes (service), namePtr, len, typePtr, len,
+                                                domainPtr, len);
 
-            Utility.Free (servicePtr);
-            
             if (ret < 0) {
                 Utility.Free (namePtr);
                 Utility.Free (typePtr);
@@ -418,11 +404,7 @@ namespace Avahi
         }
 
         public static string GetAlternativeServiceName (string name) {
-            IntPtr namePtr = Utility.StringToPtr (name);
-            IntPtr result = avahi_alternative_service_name (namePtr);
-            Utility.Free (namePtr);
-
-            return Utility.PtrToStringFree (result);
+            return Utility.PtrToStringFree (avahi_alternative_service_name (Utility.StringToBytes (name)));
         }
 
         private void OnEntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata)
index 0922aa787c1339b34c9f1014fd1c3703b6413904..81305fd778efd9c35b4ee3bdc7591b2e802d8de8 100644 (file)
@@ -23,6 +23,7 @@ using System;
 using System.Collections;
 using System.Net;
 using System.Runtime.InteropServices;
+using System.Text;
 using Mono.Unix;
 
 namespace Avahi
@@ -51,7 +52,7 @@ namespace Avahi
         
         [DllImport ("avahi-client")]
         private static extern IntPtr avahi_host_name_resolver_new (IntPtr client, int iface, Protocol proto,
-                                                                   IntPtr hostname, Protocol aproto, LookupFlags flags,
+                                                                   byte[] hostname, Protocol aproto, LookupFlags flags,
                                                                    HostNameResolverCallback cb, IntPtr userdata);
 
         [DllImport ("avahi-client")]
@@ -125,14 +126,14 @@ namespace Avahi
                 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
                 return;
 
-            IntPtr hostPtr = Utility.StringToPtr (hostname);
-
             lock (client) {
-                handle = avahi_host_name_resolver_new (client.Handle, iface, proto, hostPtr, aproto, flags,
+                handle = avahi_host_name_resolver_new (client.Handle, iface, proto,
+                                                       Utility.StringToBytes (hostname), aproto, flags,
                                                        cb, IntPtr.Zero);
+
+                if (handle == IntPtr.Zero)
+                    client.ThrowError ();
             }
-            
-            Utility.Free (hostPtr);
         }
 
         private void Stop (bool force)
index 5a736bd3d13b284198e8b08cd693369b716da603..f65f8ac17b0f89fa14f45bedf501cc811b5feefe 100644 (file)
@@ -23,6 +23,7 @@ using System;
 using System.Net;
 using System.Collections;
 using System.Runtime.InteropServices;
+using System.Text;
 
 namespace Avahi
 {
@@ -93,7 +94,7 @@ namespace Avahi
         
         [DllImport ("avahi-client")]
         private static extern IntPtr avahi_record_browser_new (IntPtr client, int iface, Protocol proto,
-                                                               IntPtr name, ushort clazz, ushort type,
+                                                               byte[] name, ushort clazz, ushort type,
                                                                LookupFlags flags, RecordBrowserCallback cb,
                                                                IntPtr userdata);
 
@@ -164,14 +165,13 @@ namespace Avahi
                 (addListeners.Count == 0 && removeListeners.Count == 0))
                 return;
 
-            IntPtr namePtr = Utility.StringToPtr (name);
-
             lock (client) {
-                handle = avahi_record_browser_new (client.Handle, iface, proto, namePtr, (ushort) clazz, (ushort) type,
-                                                   flags, cb, IntPtr.Zero);
-            }
+                handle = avahi_record_browser_new (client.Handle, iface, proto, Utility.StringToBytes (name),
+                                                   (ushort) clazz, (ushort) type, flags, cb, IntPtr.Zero);
 
-            Utility.Free (namePtr);
+                if (handle == IntPtr.Zero)
+                    client.ThrowError ();
+            }
         }
 
         private void Stop (bool force)
index 639dd0a3bd5e466a988e542e6ae1b9d9d4008ae8..520e339582182ef3d17d951736f512b7ae0ce7dd 100644 (file)
@@ -23,6 +23,7 @@ using System;
 using System.Net;
 using System.Collections;
 using System.Runtime.InteropServices;
+using System.Text;
 
 namespace Avahi
 {
@@ -79,8 +80,8 @@ namespace Avahi
         private ArrayList removeListeners = new ArrayList ();
         
         [DllImport ("avahi-client")]
-        private static extern IntPtr avahi_service_browser_new (IntPtr client, int iface, int proto, IntPtr type,
-                                                                IntPtr domain, LookupFlags flags,
+        private static extern IntPtr avahi_service_browser_new (IntPtr client, int iface, int proto, byte[] type,
+                                                                byte[] domain, LookupFlags flags,
                                                                 ServiceBrowserCallback cb,
                                                                 IntPtr userdata);
 
@@ -152,15 +153,14 @@ namespace Avahi
                 (addListeners.Count == 0 && removeListeners.Count == 0))
                 return;
 
-            IntPtr domainPtr = Utility.StringToPtr (domain);
-            IntPtr typePtr = Utility.StringToPtr (type);
-
             lock (client) {
-                handle = avahi_service_browser_new (client.Handle, iface, (int) proto, typePtr, domainPtr, flags,
-                                                    cb, IntPtr.Zero);
+                handle = avahi_service_browser_new (client.Handle, iface, (int) proto,
+                                                    Utility.StringToBytes (type), Utility.StringToBytes (domain),
+                                                    flags, cb, IntPtr.Zero);
+
+                if (handle == IntPtr.Zero)
+                    client.ThrowError ();
             }
-            Utility.Free (domainPtr);
-            Utility.Free (typePtr);
         }
 
         private void Stop (bool force)
index 8dfe5209ee5b7779550071862cef52bb1608b982..945372beb0bee363d54b486230cfaad0e2a36d03 100644 (file)
@@ -23,6 +23,7 @@ using System;
 using System.Collections;
 using System.Net;
 using System.Runtime.InteropServices;
+using System.Text;
 using Mono.Unix;
 
 namespace Avahi
@@ -53,7 +54,7 @@ namespace Avahi
         
         [DllImport ("avahi-client")]
         private static extern IntPtr avahi_service_resolver_new (IntPtr client, int iface, Protocol proto,
-                                                                 IntPtr name, IntPtr type, IntPtr domain,
+                                                                 byte[] name, byte[] type, byte[] domain,
                                                                  Protocol aproto, LookupFlags flags,
                                                                  ServiceResolverCallback cb,
                                                                  IntPtr userdata);
@@ -145,18 +146,14 @@ namespace Avahi
                 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
                 return;
 
-            IntPtr namePtr = Utility.StringToPtr (name);
-            IntPtr typePtr = Utility.StringToPtr (type);
-            IntPtr domainPtr = Utility.StringToPtr (domain);
-
             lock (client) {
-                handle = avahi_service_resolver_new (client.Handle, iface, proto, namePtr, typePtr, domainPtr,
-                                                     aproto, flags, cb, IntPtr.Zero);
+                handle = avahi_service_resolver_new (client.Handle, iface, proto,
+                                                     Utility.StringToBytes (name), Utility.StringToBytes (type),
+                                                     Utility.StringToBytes (domain), aproto, flags, cb, IntPtr.Zero);
+
+                if (handle == IntPtr.Zero)
+                    client.ThrowError ();
             }
-            
-            Utility.Free (namePtr);
-            Utility.Free (typePtr);
-            Utility.Free (domainPtr);
         }
 
         private void Stop (bool force)
index dbfff7378058dd9fd12247ff05fd03e78def0426..3796d602ace36118d281a6c1bd6bd8eeaa529786 100644 (file)
@@ -22,6 +22,7 @@
 using System;
 using System.Collections;
 using System.Runtime.InteropServices;
+using System.Text;
 
 namespace Avahi
 {
@@ -71,7 +72,7 @@ namespace Avahi
         
         [DllImport ("avahi-client")]
         private static extern IntPtr avahi_service_type_browser_new (IntPtr client, int iface, int proto,
-                                                                     IntPtr domain, LookupFlags flags,
+                                                                     byte[] domain, LookupFlags flags,
                                                                      ServiceTypeBrowserCallback cb,
                                                                      IntPtr userdata);
 
@@ -143,10 +144,12 @@ namespace Avahi
                 return;
 
             lock (client) {
-                IntPtr domainPtr = Utility.StringToPtr (domain);
-                handle = avahi_service_type_browser_new (client.Handle, iface, (int) proto, domainPtr, flags,
+                handle = avahi_service_type_browser_new (client.Handle, iface, (int) proto,
+                                                         Utility.StringToBytes (domain), flags,
                                                          cb, IntPtr.Zero);
-                Utility.Free (domainPtr);
+
+                if (handle == IntPtr.Zero)
+                    client.ThrowError ();
             }
         }
 
index f8b4ffd8982857d55b65f603f839efb666cfa6fd..b15161c19b035b38edcf145581240c0fc1d46846 100644 (file)
@@ -60,12 +60,20 @@ namespace Avahi
             return ret;
         }
 
-        public static IntPtr StringToPtr (string str)
+        public static byte[] StringToBytes (string str)
+        {
+            if (str == null)
+                return null;
+
+            return Encoding.UTF8.GetBytes (str);
+        }
+
+        private static IntPtr StringToPtr (string str)
         {
             if (str == null)
                 return IntPtr.Zero;
 
-            byte[] bytes = Encoding.UTF8.GetBytes (str);
+            byte[] bytes = Utility.StringToBytes (str);
             IntPtr buf = Stdlib.malloc ((uint) bytes.Length + 1);
             Marshal.Copy (bytes, 0, buf, bytes.Length);
             Marshal.WriteByte (buf, bytes.Length, 0);