5 // Jonathan Chambers <jonathan.chambers@ansys.com>
7 // Copyright (C) 2006 Novell (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Runtime.InteropServices;
34 [StructLayout(LayoutKind.Explicit)]
35 internal unsafe struct Variant
41 public ushort wReserved1;
44 public ushort wReserved2;
47 public ushort wReserved3;
71 public IntPtr bstrVal;
92 public IntPtr pdispVal;
95 public BRECORD bRecord;
97 public void SetValue(object obj) {
98 vt = (short)VarEnum.VT_EMPTY;
102 Type t = obj.GetType();
104 t = Enum.GetUnderlyingType (t);
106 if (t == typeof(sbyte))
108 vt = (short)VarEnum.VT_I1;
111 else if (t == typeof(byte))
113 vt = (short)VarEnum.VT_UI1;
116 else if (t == typeof(short))
118 vt = (short)VarEnum.VT_I2;
121 else if (t == typeof(ushort))
123 vt = (short)VarEnum.VT_UI2;
126 else if (t == typeof(int))
128 vt = (short)VarEnum.VT_I4;
131 else if (t == typeof(uint))
133 vt = (short)VarEnum.VT_UI4;
136 else if (t == typeof(long))
138 vt = (short)VarEnum.VT_I8;
141 else if (t == typeof(ulong))
143 vt = (short)VarEnum.VT_UI8;
146 else if (t == typeof(float))
148 vt = (short)VarEnum.VT_R4;
151 else if (t == typeof(double))
153 vt = (short)VarEnum.VT_R8;
154 dblVal = (double)obj;
156 else if (t == typeof(string))
158 vt = (short)VarEnum.VT_BSTR;
159 bstrVal = Marshal.StringToBSTR((string)obj);
161 else if (t == typeof(bool))
163 vt = (short)VarEnum.VT_BOOL;
164 lVal = ((bool)obj) ? -1 : 0;
166 else if (t == typeof (BStrWrapper))
168 vt = (short)VarEnum.VT_BSTR;
169 bstrVal = Marshal.StringToBSTR(((BStrWrapper)obj).WrappedObject);
171 #if !FULL_AOT_RUNTIME
172 else if (t == typeof (UnknownWrapper))
174 vt = (short)VarEnum.VT_UNKNOWN;
175 pdispVal = Marshal.GetIUnknownForObject(((UnknownWrapper)obj).WrappedObject);
177 else if (t == typeof (DispatchWrapper))
179 vt = (short)VarEnum.VT_DISPATCH;
180 pdispVal = Marshal.GetIDispatchForObject(((DispatchWrapper)obj).WrappedObject);
186 throw new NotImplementedException(string.Format("Variant couldn't handle object of type {0}", obj.GetType()));
190 pdispVal = Marshal.GetIDispatchForObject(obj);
191 vt = (short)VarEnum.VT_DISPATCH;
197 vt = (short)VarEnum.VT_UNKNOWN;
198 pdispVal = Marshal.GetIUnknownForObject(obj);
202 throw new NotImplementedException(string.Format("Variant couldn't handle object of type {0}", obj.GetType()), ex);
208 public static object GetValueAt(int vt, IntPtr addr)
214 obj = (sbyte)Marshal.ReadByte(addr);
217 obj = Marshal.ReadByte(addr);
220 obj = Marshal.ReadInt16(addr);
223 obj = (ushort)Marshal.ReadInt16(addr);
226 obj = Marshal.ReadInt32(addr);
229 obj = (uint)Marshal.ReadInt32(addr);
232 obj = Marshal.ReadInt64(addr);
235 obj = (ulong)Marshal.ReadInt64(addr);
238 obj = Marshal.PtrToStructure(addr, typeof(float));
241 obj = Marshal.PtrToStructure(addr, typeof(double));
243 case VarEnum.VT_BOOL:
244 obj = !(Marshal.ReadInt16(addr) == 0);
246 case VarEnum.VT_BSTR:
247 obj = Marshal.PtrToStringBSTR(Marshal.ReadIntPtr(addr));
249 // GetObjectForIUnknown is excluded from Marshal using FULL_AOT_RUNTIME
251 case VarEnum.VT_UNKNOWN:
252 case VarEnum.VT_DISPATCH:
254 IntPtr ifaceaddr = Marshal.ReadIntPtr(addr);
255 if (ifaceaddr != IntPtr.Zero)
256 obj = Marshal.GetObjectForIUnknown(ifaceaddr);
264 public object GetValue() {
298 case VarEnum.VT_BOOL:
299 obj = !(boolVal == 0);
301 case VarEnum.VT_BSTR:
302 obj = Marshal.PtrToStringBSTR(bstrVal);
304 #if !FULL_AOT_RUNTIME
305 case VarEnum.VT_UNKNOWN:
306 case VarEnum.VT_DISPATCH:
307 if (pdispVal != IntPtr.Zero)
308 obj = Marshal.GetObjectForIUnknown(pdispVal);
312 if (((VarEnum)vt & VarEnum.VT_BYREF) == VarEnum.VT_BYREF &&
313 pdispVal != IntPtr.Zero)
315 obj = GetValueAt(vt & ~(short)VarEnum.VT_BYREF, pdispVal);
324 if ((VarEnum)vt == VarEnum.VT_BSTR) {
325 Marshal.FreeBSTR (bstrVal);
327 else if ((VarEnum)vt == VarEnum.VT_DISPATCH || (VarEnum)vt == VarEnum.VT_UNKNOWN) {
328 if (pdispVal != IntPtr.Zero)
329 Marshal.Release (pdispVal);
334 [StructLayout(LayoutKind.Sequential)]
335 internal unsafe struct BRECORD
337 #pragma warning disable 169
340 #pragma warning restore 169