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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 166 - (hide annotations) (download)
Thu Jul 22 14:24:07 2010 UTC (9 years, 6 months ago) by william
File size: 7435 byte(s)
bulk commit

1 william 70 using System;
2     using System.Collections.Generic;
3     using System.Text;
4    
5     using gr2lib.core.coretypes.native;
6     using System.Runtime.InteropServices;
7 william 96 using System.Diagnostics;
8 william 106 using gr2lib.core.exceptions;
9 william 166 using System.Windows.Forms;
10 william 70
11     namespace gr2lib.core.helpers
12     {
13     internal class Helpers
14     {
15     public static T ReadFromMemory<T>(IntPtr pointer) where T : struct
16     {
17 william 83 try
18 william 70 {
19 william 83 if (pointer == IntPtr.Zero)
20     {
21     throw new InvalidOperationException();
22     }
23    
24 william 96 T _object_implementation_ = (T)Marshal.PtrToStructure(pointer, typeof(T));
25    
26     return _object_implementation_;
27 william 70 }
28 william 106 catch (Exception ex)
29 william 83 {
30 william 96 StackTrace st = new StackTrace(true);
31 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
32 william 106 Granny2ExceptionWriter.WriteToConsole(ex,st);
33 william 102 #endif
34 william 83 return default(T);
35     }
36 william 70 }
37    
38     public static T[] ReadArrayFromMemory<T>(ArrayOfStructures array) where T : struct
39     {
40 william 83 try
41     {
42     return ReadArrayFromMemory<T>(array.Count, array.Pointer);
43     }
44 william 106 catch (Exception ex)
45 william 83 {
46 william 96 StackTrace st = new StackTrace(true);
47 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
48 william 106 Granny2ExceptionWriter.WriteToConsole(ex,st);
49 william 102 #endif
50 william 83 return default(T[]);
51     }
52 william 70 }
53    
54     public static T[] ReadArrayFromMemory<T>(int count, IntPtr pointer) where T : struct
55     {
56 william 83 try
57 william 70 {
58 william 83 T[] rez = new T[count];
59     int size = SizeOfStruct<T>();
60     for (int i = 0; i < count; i++)
61     {
62     rez[i] = ReadFromMemory<T>(new IntPtr(pointer.ToInt32() + (i * size)));
63 william 166 Application.DoEvents();
64 william 83 }
65     return rez;
66 william 70 }
67 william 106 catch (Exception ex)
68 william 83 {
69 william 96 StackTrace st = new StackTrace(true);
70 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
71 william 106 Granny2ExceptionWriter.WriteToConsole(ex,st);
72 william 102 #endif
73 william 83 return default(T[]);
74     }
75 william 70 }
76    
77     public static IntPtr[] ReadPtrArrayFromMemory<T>(ArrayOfStructures array) where T : struct
78     {
79 william 83 try
80     {
81     return ReadPtrArrayFromMemory<T>(array.Count, array.Pointer);
82     }
83 william 106 catch (Exception ex)
84 william 83 {
85 william 96 StackTrace st = new StackTrace(true);
86 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
87 william 106 Granny2ExceptionWriter.WriteToConsole(ex,st);
88 william 102 #endif
89 william 83 return default(IntPtr[]);
90     }
91 william 70 }
92    
93     public static IntPtr[] ReadPtrArrayFromMemory<T>(int count, IntPtr pointer) where T : struct
94     {
95 william 83 try
96 william 70 {
97 william 83 IntPtr[] rez = new IntPtr[count];
98     int size = SizeOfStruct<T>();
99     for (int i = 0; i < count; i++)
100     {
101     rez[i] = new IntPtr(pointer.ToInt32() + (i * size));
102     }
103     return rez;
104 william 70 }
105 william 106 catch (Exception ex)
106 william 83 {
107 william 96 StackTrace st = new StackTrace(true);
108 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
109 william 106 Granny2ExceptionWriter.WriteToConsole(ex,st);
110 william 102 #endif
111 william 83 return default(IntPtr[]);
112     }
113 william 70 }
114    
115     public static IntPtr[] ReadArrayFromMemory(ArrayOfReferences array)
116     {
117 william 83 try
118     {
119     return ReadArrayFromMemory(array.Count, array.Pointer);
120     }
121 william 106 catch (Exception ex)
122 william 83 {
123 william 96 StackTrace st = new StackTrace(true);
124 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
125 william 106 Granny2ExceptionWriter.WriteToConsole(ex,st);
126 william 102 #endif
127 william 83 return default(IntPtr[]);
128     }
129 william 70 }
130    
131     public static IntPtr[] ReadArrayFromMemory(int count, IntPtr pointer)
132     {
133 william 83 try
134 william 70 {
135 william 83 IntPtr[] rez = new IntPtr[count];
136     for (int i = 0; i < count; i++)
137     {
138     rez[i] = new IntPtr(Marshal.ReadInt32(new IntPtr(pointer.ToInt32() + (i * 4))));
139 william 166 Application.DoEvents();
140 william 83 }
141     return rez;
142 william 70 }
143 william 106 catch (Exception ex)
144 william 83 {
145 william 96 StackTrace st = new StackTrace(true);
146 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
147 william 106 Granny2ExceptionWriter.WriteToConsole(ex,st);
148 william 102 #endif
149 william 83 return default(IntPtr[]);
150     }
151 william 70 }
152    
153     //public static VariantReference[] ReadArrayFromMemory(ArrayOfVariants array)
154     //{
155     // Native.VariantReference[] rez = new Native.VariantReference[array.Count];
156     // for (int i = 0; i < array.Count; i++)
157     // {
158     // rez[i] = new Native.VariantReference();
159     // rez[i].Unknown1 = new IntPtr(Marshal.ReadInt32(new IntPtr(array.Pointer.Unknown1.ToInt32() + (i * 8))));
160     // rez[i].Unknown2 = new IntPtr(Marshal.ReadInt32(new IntPtr(array.Pointer.Unknown2.ToInt32() + (i * 8))));
161     // }
162     // return rez;
163     //}
164    
165     //public static StringMember[] ReadStrMemsFromMemory(IntPtr pointer)
166     //{
167     // /*ATTENTION!
168     // In original code, a null reference is StringMember[] sm is used to gather the number of string members:
169     // * for(int i = 0; sm[i].Type; ++i)
170     // * ++nVertexComponents
171     // */
172     // List<Native.StringMember> smList = new List<Native.StringMember>();
173     // int count = 0;
174     // int start = pointer.ToInt32();
175     // while (1 == 1)
176     // {
177     // try
178     // {
179     // Native.StringMember sm = ReadFromMemory<Native.StringMember>(new IntPtr(start + (count * 32)));
180     // if (sm.Type == 0) break;
181     // smList.Add(sm);
182     // ++count;
183     // }
184     // catch (Exception)
185     // {
186     // break;
187     // }
188     // }
189     // return smList.ToArray();
190     //}
191    
192     public static int SizeOfStruct<T>() where T : struct
193     {
194 william 96 try
195 william 70 {
196 william 96
197     //object[] attributes = typeof(T).GetCustomAttributes(false);
198     //if (attributes == null || attributes.Length < 1) return 0;
199     int size = 0;
200     //foreach (object attribute in attributes)
201     //{
202     // if (attribute is SizeAttribute)
203     // {
204     // size = (attribute as SizeAttribute).Size;
205     // break;
206     // }
207     //}
208     size = Marshal.SizeOf(typeof(T));
209     return size;
210 william 70 }
211 william 106 catch (Exception ex)
212 william 96 {
213     StackTrace st = new StackTrace(true);
214 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
215 william 106 Granny2ExceptionWriter.WriteToConsole(ex,st);
216 william 102 #endif
217 william 96 return 0;
218     }
219 william 70 }
220     }
221     }

  ViewVC Help
Powered by ViewVC 1.1.22