/[gr2lib]/trunk/gr2lib/core/helpers/Helpers.cs
ViewVC logotype

Diff of /trunk/gr2lib/core/helpers/Helpers.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/gr2lib/core/helpers/Helpers.cs	2010/07/15 12:55:21	70
+++ trunk/gr2lib/core/helpers/Helpers.cs	2010/07/16 21:26:08	83
@@ -11,59 +11,115 @@
     {
         public static T ReadFromMemory<T>(IntPtr pointer) where T : struct
         {
-            if (pointer == IntPtr.Zero)
+            try
             {
-                throw new InvalidOperationException();
-            }
+                if (pointer == IntPtr.Zero)
+                {
+                    throw new InvalidOperationException();
+                }
 
-            return (T)Marshal.PtrToStructure(pointer, typeof(T));
+                return (T)Marshal.PtrToStructure(pointer, typeof(T));
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.ToString());
+                return default(T);
+            }
         }
 
         public static T[] ReadArrayFromMemory<T>(ArrayOfStructures array) where T : struct
         {
-            return ReadArrayFromMemory<T>(array.Count, array.Pointer);
+            try
+            {
+                return ReadArrayFromMemory<T>(array.Count, array.Pointer);
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.ToString());
+                return default(T[]);
+            }
         }
 
         public static T[] ReadArrayFromMemory<T>(int count, IntPtr pointer) where T : struct
         {
-            T[] rez = new T[count];
-            int size = SizeOfStruct<T>();
-            for (int i = 0; i < count; i++)
+            try
             {
-                rez[i] = ReadFromMemory<T>(new IntPtr(pointer.ToInt32() + (i * size)));
+                T[] rez = new T[count];
+                int size = SizeOfStruct<T>();
+                for (int i = 0; i < count; i++)
+                {
+                    rez[i] = ReadFromMemory<T>(new IntPtr(pointer.ToInt32() + (i * size)));
+                }
+                return rez;
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.ToString());
+                return default(T[]);
             }
-            return rez;
         }
 
         public static IntPtr[] ReadPtrArrayFromMemory<T>(ArrayOfStructures array) where T : struct
         {
-            return ReadPtrArrayFromMemory<T>(array.Count, array.Pointer);
+            try
+            {
+                return ReadPtrArrayFromMemory<T>(array.Count, array.Pointer);
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.ToString());
+                return default(IntPtr[]);
+            }
         }
 
         public static IntPtr[] ReadPtrArrayFromMemory<T>(int count, IntPtr pointer) where T : struct
         {
-            IntPtr[] rez = new IntPtr[count];
-            int size = SizeOfStruct<T>();
-            for (int i = 0; i < count; i++)
+            try
             {
-                rez[i] = new IntPtr(pointer.ToInt32() + (i * size));
+                IntPtr[] rez = new IntPtr[count];
+                int size = SizeOfStruct<T>();
+                for (int i = 0; i < count; i++)
+                {
+                    rez[i] = new IntPtr(pointer.ToInt32() + (i * size));
+                }
+                return rez;
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.ToString());
+                return default(IntPtr[]);
             }
-            return rez;
         }
 
         public static IntPtr[] ReadArrayFromMemory(ArrayOfReferences array)
         {
-            return ReadArrayFromMemory(array.Count, array.Pointer);
+            try
+            {
+                return ReadArrayFromMemory(array.Count, array.Pointer);
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.ToString());
+                return default(IntPtr[]);
+            }
         }
 
         public static IntPtr[] ReadArrayFromMemory(int count, IntPtr pointer)
         {
-            IntPtr[] rez = new IntPtr[count];
-            for (int i = 0; i < count; i++)
+            try
+            {
+                IntPtr[] rez = new IntPtr[count];
+                for (int i = 0; i < count; i++)
+                {
+                    rez[i] = new IntPtr(Marshal.ReadInt32(new IntPtr(pointer.ToInt32() + (i * 4))));
+                }
+                return rez;
+            }
+            catch (Exception ex)
             {
-                rez[i] = new IntPtr(Marshal.ReadInt32(new IntPtr(pointer.ToInt32() + (i * 4))));
+                Console.WriteLine(ex.ToString());
+                return default(IntPtr[]);
             }
-            return rez;
         }
 
         //public static VariantReference[] ReadArrayFromMemory(ArrayOfVariants array)

 

  ViewVC Help
Powered by ViewVC 1.1.22