private unsafe static object[] GetVariantsFromPtr(Oleaut32.VARIANT *ptr, uint cVariants) { var objects = new object[cVariants]; for (int i = 0; i < cVariants; i++) { try { using Oleaut32.VARIANT variant = ptr[i]; objects[i] = variant.ToObject(); } catch (Exception ex) { Debug.Fail("Failed to marshal component attribute VARIANT " + i, ex.ToString()); } } try { Marshal.FreeCoTaskMem((IntPtr)ptr); } catch (Exception ex) { Debug.Fail("Failed to free VARIANT array memory", ex.ToString()); } return(objects); }
private unsafe void EnsureArrays() { if (arraysFetched) { return; } arraysFetched = true; try { // marshal the items. object[] nameItems = nameMarshaller.Items; object[] cookieItems = valueMarshaller.Items; Oleaut32.IPerPropertyBrowsing ppb = (Oleaut32.IPerPropertyBrowsing)owner.GetPerPropertyBrowsing(); int itemCount = 0; Debug.Assert(cookieItems != null && nameItems != null, "An item array is null"); if (nameItems.Length > 0) { object[] valueItems = new object[cookieItems.Length]; int cookie; Debug.Assert(cookieItems.Length == nameItems.Length, "Got uneven names and cookies"); // for each name item, we ask the object for it's corresponding value. for (int i = 0; i < nameItems.Length; i++) { cookie = (int)cookieItems[i]; if (nameItems[i] == null || !(nameItems[i] is string)) { Debug.Fail("Bad IPerPropertyBrowsing item [" + i.ToString(CultureInfo.InvariantCulture) + "], name=" + (nameItems == null ? "(unknown)" : nameItems[i].ToString())); continue; } using var var = new Oleaut32.VARIANT(); HRESULT hr = ppb.GetPredefinedValue(target.Dispid, (uint)cookie, &var); if (hr == HRESULT.S_OK && var.vt != Ole32.VARENUM.EMPTY) { valueItems[i] = var.ToObject(); } itemCount++; } // pass this data down to the base Com2Enum object... if (itemCount > 0) { string[] strings = new string[itemCount]; Array.Copy(nameItems, 0, strings, 0, itemCount); base.PopulateArrays(strings, valueItems); } } } catch (Exception ex) { Debug.Fail("Failed to build IPerPropertyBrowsing editor. " + ex.GetType().Name + ", " + ex.Message); } }
private unsafe void EnsureArrays() { if (_arraysFetched) { return; } _arraysFetched = true; try { // Marshal the items. Oleaut32.IPerPropertyBrowsing ppb = _owner.GetPerPropertyBrowsing(); int itemCount = 0; Debug.Assert(_cookies is not null && _names is not null, "An item array is null"); if (_names.Length > 0) { object[] values = new object[_cookies.Length]; uint cookie; Debug.Assert(_cookies.Length == _names.Length, "Got uneven names and cookies"); // For each name item, we ask the object for it's corresponding value. for (int i = 0; i < _names.Length; i++) { cookie = _cookies[i]; if (_names[i] is null || !(_names[i] is string)) { Debug.Fail($"Bad IPerPropertyBrowsing item [{i}], name={_names?[i] ?? "(unknown)"}"); continue; } using var var = new Oleaut32.VARIANT(); HRESULT hr = ppb.GetPredefinedValue(_target.Dispid, cookie, &var); if (hr == HRESULT.S_OK && var.vt != Ole32.VARENUM.EMPTY) { values[i] = var.ToObject(); } itemCount++; } // Pass the data to the base Com2Enum object. if (itemCount > 0) { string[] strings = new string[itemCount]; Array.Copy(_names, 0, strings, 0, itemCount); PopulateArrays(strings, values); } } } catch (Exception ex) { Debug.Fail($"Failed to build IPerPropertyBrowsing editor. {ex.GetType().Name}, {ex.Message}"); } }
private void EnsureArrays() { if (arraysFetched) { return; } arraysFetched = true; try { // marshal the items. object[] nameItems = nameMarshaller.Items; object[] cookieItems = valueMarshaller.Items; Oleaut32.IPerPropertyBrowsing ppb = (Oleaut32.IPerPropertyBrowsing)target.TargetObject; int itemCount = 0; Debug.Assert(cookieItems != null && nameItems != null, "An item array is null"); if (nameItems.Length > 0) { object[] valueItems = new object[cookieItems.Length]; var var = new Oleaut32.VARIANT(); int cookie; Debug.Assert(cookieItems.Length == nameItems.Length, "Got uneven names and cookies"); // for each name item, we ask the object for it's corresponding value. // Type targetType = target.PropertyType; for (int i = nameItems.Length - 1; i >= 0; i--) { cookie = (int)cookieItems[i]; if (nameItems[i] == null || !(nameItems[i] is string)) { Debug.Fail("Bad IPerPropertyBrowsing item [" + i.ToString(CultureInfo.InvariantCulture) + "], name=" + (nameItems == null ? "(unknown)" : nameItems[i].ToString())); continue; } var.vt = VARENUM.EMPTY; HRESULT hr = ppb.GetPredefinedValue(target.DISPID, (uint)cookie, var); if (hr == HRESULT.S_OK && var.vt != VARENUM.EMPTY) { valueItems[i] = var.ToObject(); if (valueItems[i].GetType() != targetType) { if (targetType.IsEnum) { valueItems[i] = Enum.ToObject(targetType, valueItems[i]); } else { try { valueItems[i] = Convert.ChangeType(valueItems[i], targetType, CultureInfo.InvariantCulture); } catch { // oh well... } } } } var.Clear(); if (hr == HRESULT.S_OK) { itemCount++; continue; } if (itemCount > 0) { // shorten the arrays to ignore the failed ones. this isn't terribly // efficient but shouldn't happen very often. It's rare for these to fail. // Array.Copy(nameItems, i, nameItems, i + 1, itemCount); Array.Copy(valueItems, i, valueItems, i + 1, itemCount); } } // pass this data down to the base Com2Enum object... string[] strings = new string[itemCount]; Array.Copy(nameItems, 0, strings, 0, itemCount); base.PopulateArrays(strings, valueItems); } } catch (Exception ex) { base.PopulateArrays(Array.Empty <string>(), Array.Empty <object>()); Debug.Fail("Failed to build IPerPropertyBrowsing editor. " + ex.GetType().Name + ", " + ex.Message); } }
private unsafe void EnsureArrays() { if (_arraysFetched) { return; } _arraysFetched = true; try { // Marshal the items. Oleaut32.IPerPropertyBrowsing ppb = (Oleaut32.IPerPropertyBrowsing)_target.TargetObject; int itemCount = 0; Debug.Assert(_cookies is not null && _names is not null, "An item array is null"); if (_names.Length > 0) { object[] valueItems = new object[_names.Length]; uint cookie; Debug.Assert(_cookies.Length == _names.Length, "Got uneven names and cookies"); // For each name item, we ask the object for it's corresponding value. Type targetType = _target.PropertyType; for (int i = _names.Length - 1; i >= 0; i--) { cookie = _cookies[i]; if (_names[i] is null) { Debug.Fail($"Bad IPerPropertyBrowsing item [{i}], name={_names?[i] ?? "(unknown)"}"); continue; } using var variant = new Oleaut32.VARIANT(); HRESULT hr = ppb.GetPredefinedValue(_target.DISPID, cookie, &variant); if (hr == HRESULT.S_OK && variant.vt != Ole32.VARENUM.EMPTY) { valueItems[i] = variant.ToObject(); if (valueItems[i].GetType() != targetType) { if (targetType.IsEnum) { valueItems[i] = Enum.ToObject(targetType, valueItems[i]); } else { try { valueItems[i] = Convert.ChangeType(valueItems[i], targetType, CultureInfo.InvariantCulture); } catch { // oh well... } } } } if (hr == HRESULT.S_OK) { itemCount++; continue; } if (itemCount > 0) { // Shorten the arrays to ignore the failed ones. This isn't terribly // efficient but shouldn't happen very often. It's rare for these to fail. Array.Copy(_names, i, _names, i + 1, itemCount); Array.Copy(valueItems, i, valueItems, i + 1, itemCount); } } // Pass the data to the base Com2Enum object. string[] strings = new string[itemCount]; Array.Copy(_names, 0, strings, 0, itemCount); PopulateArrays(strings, valueItems); } } catch (Exception ex) { PopulateArrays(Array.Empty <string>(), Array.Empty <object>()); Debug.Fail($"Failed to build IPerPropertyBrowsing editor. {ex.GetType().Name}, {ex.Message}"); } }