protected override bool RunDialog(IntPtr hwndOwner) { IntPtr dataObj = IntPtr.Zero; NativeMethods.IDsObjectPicker ipicker = Initialize(); if (ipicker == null) { _selectedObjects = null; return(false); } int hResult = ipicker.InvokeDialog(hwndOwner, ref dataObj); if (dataObj != IntPtr.Zero) { _selectedObjects = ProcessSelections(dataObj); } return(hResult == NativeMethods.HRESULT.S_OK); }
private NativeMethods.IDsObjectPicker Initialize() { NativeMethods.DSObjectPicker picker = new NativeMethods.DSObjectPicker(); NativeMethods.IDsObjectPicker ipicker = (NativeMethods.IDsObjectPicker)picker; NativeMethods.DSOP_SCOPE_INIT_INFO[] scopeInitInfo = new NativeMethods.DSOP_SCOPE_INIT_INFO[2]; // The default and filters are used by all scopes uint defaultFilter = GetDefaultFilter(); uint upLevelFilter = GetUpLevelFilter(); uint downLevelFilter = GetDownLevelFilter(); // Use one scope for the default (starting) locations. uint startingScope = GetStartingScope(); if (startingScope > 0) { scopeInitInfo[0].cbSize = (uint)Marshal.SizeOf(typeof(NativeMethods.DSOP_SCOPE_INIT_INFO)); scopeInitInfo[0].flType = startingScope; scopeInitInfo[0].flScope = NativeMethods.DSOP_SCOPE_INIT_INFO_FLAGS.DSOP_SCOPE_FLAG_STARTING_SCOPE | defaultFilter; scopeInitInfo[0].FilterFlags.Uplevel.flBothModes = upLevelFilter; scopeInitInfo[0].FilterFlags.flDownlevel = downLevelFilter; scopeInitInfo[0].pwzADsPath = null; scopeInitInfo[0].pwzDcName = null; scopeInitInfo[0].hr = 0; } // Allocate memory from the unmananged mem of the process, this should be freed later!!! IntPtr refScopeInitInfo = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.DSOP_SCOPE_INIT_INFO)) * scopeInitInfo.Length); // Marshal structs to pointers for (int index = 0; index < scopeInitInfo.Length; index++) { Marshal.StructureToPtr(scopeInitInfo[index], (IntPtr)((long)refScopeInitInfo + index * Marshal.SizeOf(typeof(NativeMethods.DSOP_SCOPE_INIT_INFO))), false); } // Initialize structure with data to initialize an object picker dialog box. NativeMethods.DSOP_INIT_INFO initInfo = new NativeMethods.DSOP_INIT_INFO(); initInfo.cbSize = (uint)Marshal.SizeOf(initInfo); initInfo.pwzTargetComputer = null; initInfo.cDsScopeInfos = (uint)scopeInitInfo.Length; initInfo.aDsScopeInfos = refScopeInitInfo; initInfo.flOptions = 0; initInfo.cAttributesToFetch = length; if (length > 0) { MarshalStrings strings = new MarshalStrings(attributeNames); initInfo.apwzAttributeNames = strings.ArrayPtr; } else { initInfo.cAttributesToFetch = 0; initInfo.apwzAttributeNames = IntPtr.Zero; } int hresult = ipicker.Initialize(ref initInfo); if (hresult != NativeMethods.HRESULT.S_OK) { return(null); } return(ipicker); }