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, Size = 16)]
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;
94 public void SetValue(object obj) {
95 vt = (short)VarEnum.VT_EMPTY;
99 Type t = obj.GetType();
100 if (t == typeof(sbyte))
102 vt = (short)VarEnum.VT_I1;
105 else if (t == typeof(byte))
107 vt = (short)VarEnum.VT_UI1;
110 else if (t == typeof(short))
112 vt = (short)VarEnum.VT_I2;
115 else if (t == typeof(ushort))
117 vt = (short)VarEnum.VT_UI2;
120 else if (t == typeof(int))
122 vt = (short)VarEnum.VT_I4;
125 else if (t == typeof(uint))
127 vt = (short)VarEnum.VT_UI4;
130 else if (t == typeof(long))
132 vt = (short)VarEnum.VT_I8;
135 else if (t == typeof(ulong))
137 vt = (short)VarEnum.VT_UI8;
140 else if (t == typeof(float))
142 vt = (short)VarEnum.VT_R4;
145 else if (t == typeof(double))
147 vt = (short)VarEnum.VT_R8;
148 dblVal = (double)obj;
150 else if (t == typeof(string))
152 vt = (short)VarEnum.VT_BSTR;
153 bstrVal = Marshal.StringToBSTR((string)obj);
155 else if (t == typeof(bool))
157 vt = (short)VarEnum.VT_BOOL;
158 lVal = ((bool)obj) ? -1 : 0;
164 vt = (short)VarEnum.VT_DISPATCH;
165 pdispVal = Marshal.GetIUnknownForObject(obj);
169 throw new NotImplementedException(string.Format("Variant couldn't handle object of type {0}", obj.GetType()), ex);
174 public object GetValue() {
208 case VarEnum.VT_BOOL:
211 case VarEnum.VT_BSTR:
212 obj = Marshal.PtrToStringBSTR(bstrVal);
214 case VarEnum.VT_UNKNOWN:
215 case VarEnum.VT_DISPATCH:
216 obj = Marshal.GetObjectForIUnknown(pdispVal);
222 public void Clear ()
\r
224 if ((VarEnum)vt == VarEnum.VT_BSTR) {
\r
225 Marshal.FreeBSTR (bstrVal);
\r
227 else if ((VarEnum)vt == VarEnum.VT_DISPATCH || (VarEnum)vt == VarEnum.VT_UNKNOWN) {
\r
228 if (pdispVal != IntPtr.Zero)
\r
229 Marshal.Release (pdispVal);