ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

misc.c
Go to the documentation of this file.
00001 /*
00002  * ReactOS Access Control List Editor
00003  * Copyright (C) 2004-2005 ReactOS Team
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 /* $Id: misc.c 43790 2009-10-27 10:34:16Z dgorbachev $
00020  *
00021  * PROJECT:         ReactOS Access Control List Editor
00022  * FILE:            lib/aclui/misc.c
00023  * PURPOSE:         Access Control List Editor
00024  * PROGRAMMER:      Thomas Weidenmueller <w3seek@reactos.com>
00025  *
00026  * UPDATE HISTORY:
00027  *      07/01/2005  Created
00028  */
00029 #include <precomp.h>
00030 
00031 #define NDEBUG
00032 #include <debug.h>
00033 
00034 static PCWSTR ObjectPickerAttributes[] =
00035 {
00036     L"ObjectSid",
00037 };
00038 
00039 static INT
00040 LengthOfStrResource(IN HINSTANCE hInst,
00041                     IN UINT uID)
00042 {
00043     HRSRC hrSrc;
00044     HGLOBAL hRes;
00045     LPWSTR lpName, lpStr;
00046 
00047     if (hInst == NULL)
00048     {
00049         return -1;
00050     }
00051 
00052     /* There are always blocks of 16 strings */
00053     lpName = (LPWSTR)MAKEINTRESOURCE((uID >> 4) + 1);
00054 
00055     /* Find the string table block */
00056     if ((hrSrc = FindResourceW(hInst, lpName, (LPWSTR)RT_STRING)) &&
00057         (hRes = LoadResource(hInst, hrSrc)) &&
00058         (lpStr = LockResource(hRes)))
00059     {
00060         UINT x;
00061 
00062         /* Find the string we're looking for */
00063         uID &= 0xF; /* position in the block, same as % 16 */
00064         for (x = 0; x < uID; x++)
00065         {
00066             lpStr += (*lpStr) + 1;
00067         }
00068 
00069         /* Found the string */
00070         return (int)(*lpStr);
00071     }
00072     return -1;
00073 }
00074 
00075 static INT
00076 AllocAndLoadString(OUT LPWSTR *lpTarget,
00077                    IN HINSTANCE hInst,
00078                    IN UINT uID)
00079 {
00080     INT ln;
00081 
00082     ln = LengthOfStrResource(hInst,
00083                              uID);
00084     if (ln++ > 0)
00085     {
00086         (*lpTarget) = (LPWSTR)LocalAlloc(LMEM_FIXED,
00087                                          ln * sizeof(WCHAR));
00088         if ((*lpTarget) != NULL)
00089         {
00090             INT Ret;
00091             if (!(Ret = LoadStringW(hInst, uID, *lpTarget, ln)))
00092             {
00093                 LocalFree((HLOCAL)(*lpTarget));
00094             }
00095             return Ret;
00096         }
00097     }
00098     return 0;
00099 }
00100 
00101 DWORD
00102 LoadAndFormatString(IN HINSTANCE hInstance,
00103                     IN UINT uID,
00104                     OUT LPWSTR *lpTarget,
00105                     ...)
00106 {
00107     DWORD Ret = 0;
00108     LPWSTR lpFormat;
00109     va_list lArgs;
00110 
00111     if (AllocAndLoadString(&lpFormat,
00112                            hInstance,
00113                            uID) > 0)
00114     {
00115         va_start(lArgs, lpTarget);
00116         /* let's use FormatMessage to format it because it has the ability to allocate
00117            memory automatically */
00118         Ret = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
00119                              lpFormat,
00120                              0,
00121                              0,
00122                              (LPWSTR)lpTarget,
00123                              0,
00124                              &lArgs);
00125         va_end(lArgs);
00126 
00127         LocalFree((HLOCAL)lpFormat);
00128     }
00129 
00130     return Ret;
00131 }
00132 
00133 LPARAM
00134 ListViewGetSelectedItemData(IN HWND hwnd)
00135 {
00136     int Index;
00137 
00138     Index = ListView_GetNextItem(hwnd,
00139                                  -1,
00140                                  LVNI_SELECTED);
00141     if (Index != -1)
00142     {
00143         LVITEM li;
00144 
00145         li.mask = LVIF_PARAM;
00146         li.iItem = Index;
00147         li.iSubItem = 0;
00148 
00149         if (ListView_GetItem(hwnd,
00150                              &li))
00151         {
00152             return li.lParam;
00153         }
00154     }
00155 
00156     return 0;
00157 }
00158 
00159 BOOL
00160 ListViewSelectItem(IN HWND hwnd,
00161                    IN INT Index)
00162 {
00163     LVITEM li;
00164 
00165     li.mask = LVIF_STATE;
00166     li.iItem = Index;
00167     li.iSubItem = 0;
00168     li.state = LVIS_SELECTED;
00169     li.stateMask = LVIS_SELECTED;
00170 
00171     return ListView_SetItem(hwnd,
00172                             &li);
00173 }
00174 
00175 HRESULT
00176 InitializeObjectPicker(IN PCWSTR ServerName,
00177                        IN PSI_OBJECT_INFO ObjectInfo,
00178                        OUT IDsObjectPicker **pDsObjectPicker)
00179 {
00180     HRESULT hRet;
00181 
00182     *pDsObjectPicker = NULL;
00183 
00184     hRet = CoCreateInstance(&CLSID_DsObjectPicker,
00185                             NULL,
00186                             CLSCTX_INPROC_SERVER,
00187                             &IID_IDsObjectPicker,
00188                             (LPVOID*)pDsObjectPicker);
00189     if (SUCCEEDED(hRet))
00190     {
00191         DSOP_INIT_INFO InitInfo;
00192         UINT i;
00193         static DSOP_SCOPE_INIT_INFO Scopes[] =
00194         {
00195             {
00196                 sizeof(DSOP_SCOPE_INIT_INFO),
00197                 DSOP_SCOPE_TYPE_TARGET_COMPUTER,
00198                 DSOP_SCOPE_FLAG_DEFAULT_FILTER_USERS | DSOP_SCOPE_FLAG_DEFAULT_FILTER_GROUPS |
00199                     DSOP_SCOPE_FLAG_STARTING_SCOPE,
00200                 {
00201                     {
00202                         0,
00203                         0,
00204                         0
00205                     },
00206                     DSOP_DOWNLEVEL_FILTER_USERS | DSOP_DOWNLEVEL_FILTER_LOCAL_GROUPS |
00207                         DSOP_DOWNLEVEL_FILTER_GLOBAL_GROUPS | DSOP_DOWNLEVEL_FILTER_ALL_WELLKNOWN_SIDS
00208                 },
00209                 NULL,
00210                 NULL,
00211                 S_OK
00212             },
00213         };
00214 
00215         InitInfo.cbSize = sizeof(InitInfo);
00216         InitInfo.pwzTargetComputer = ServerName;
00217         InitInfo.cDsScopeInfos = sizeof(Scopes) / sizeof(Scopes[0]);
00218         InitInfo.aDsScopeInfos = Scopes;
00219         InitInfo.flOptions = DSOP_FLAG_MULTISELECT;
00220         InitInfo.cAttributesToFetch = sizeof(ObjectPickerAttributes) / sizeof(ObjectPickerAttributes[0]);
00221         InitInfo.apwzAttributeNames = ObjectPickerAttributes;
00222 
00223         for (i = 0; i < InitInfo.cDsScopeInfos; i++)
00224         {
00225             if ((ObjectInfo->dwFlags & SI_SERVER_IS_DC) &&
00226                 (InitInfo.aDsScopeInfos[i].flType & DSOP_SCOPE_TYPE_UPLEVEL_JOINED_DOMAIN))
00227             {
00228                 /* only set the domain controller string if we know the target
00229                    computer is a domain controller and the scope type is an
00230                    up-level domain to which the target computer is joined */
00231                 InitInfo.aDsScopeInfos[i].pwzDcName = InitInfo.pwzTargetComputer;
00232             }
00233         }
00234 
00235         hRet = (*pDsObjectPicker)->lpVtbl->Initialize(*pDsObjectPicker,
00236                                                       &InitInfo);
00237 
00238         if (FAILED(hRet))
00239         {
00240             /* delete the object picker in case initialization failed! */
00241             (*pDsObjectPicker)->lpVtbl->Release(*pDsObjectPicker);
00242         }
00243     }
00244 
00245     return hRet;
00246 }
00247 
00248 HRESULT
00249 InvokeObjectPickerDialog(IN IDsObjectPicker *pDsObjectPicker,
00250                          IN HWND hwndParent  OPTIONAL,
00251                          IN POBJPICK_SELECTED_SID SelectedSidCallback,
00252                          IN PVOID Context  OPTIONAL)
00253 {
00254     IDataObject *pdo = NULL;
00255     HRESULT hRet;
00256 
00257     hRet = pDsObjectPicker->lpVtbl->InvokeDialog(pDsObjectPicker,
00258                                                  hwndParent,
00259                                                  &pdo);
00260     if (hRet == S_OK)
00261     {
00262         STGMEDIUM stm;
00263         FORMATETC fe;
00264 
00265         fe.cfFormat = (WORD)RegisterClipboardFormat(CFSTR_DSOP_DS_SELECTION_LIST);
00266         fe.ptd = NULL;
00267         fe.dwAspect = DVASPECT_CONTENT;
00268         fe.lindex = -1;
00269         fe.tymed = TYMED_HGLOBAL;
00270 
00271         hRet = pdo->lpVtbl->GetData(pdo,
00272                                     &fe,
00273                                     &stm);
00274         if (SUCCEEDED(hRet))
00275         {
00276             PDS_SELECTION_LIST SelectionList = (PDS_SELECTION_LIST)GlobalLock(stm.hGlobal);
00277             if (SelectionList != NULL)
00278             {
00279                 LPVARIANT vSid;
00280                 PSID pSid;
00281                 UINT i;
00282                 BOOL contLoop = TRUE;
00283 
00284                 for (i = 0; i < SelectionList->cItems && contLoop; i++)
00285                 {
00286                     vSid = SelectionList->aDsSelection[i].pvarFetchedAttributes;
00287 
00288                     if (vSid != NULL && V_VT(vSid) == (VT_ARRAY | VT_UI1))
00289                     {
00290                         hRet = SafeArrayAccessData(V_ARRAY(vSid),
00291                                                    (void **)&pSid);
00292                         if (FAILED(hRet))
00293                         {
00294                             break;
00295                         }
00296 
00297                         if (pSid != NULL)
00298                         {
00299                             contLoop = SelectedSidCallback(pDsObjectPicker,
00300                                                            hwndParent,
00301                                                            pSid,
00302                                                            Context);
00303                         }
00304 
00305                         SafeArrayUnaccessData(V_ARRAY(vSid));
00306                     }
00307                 }
00308 
00309                 GlobalUnlock(stm.hGlobal);
00310 
00311                 if (SUCCEEDED(hRet))
00312                 {
00313                     /* return S_OK instead of possible other success codes if
00314                        everything went well */
00315                     hRet = S_OK;
00316                 }
00317             }
00318             else
00319             {
00320                 /* unable to translate the selection pointer handle, indicate
00321                    failure */
00322                 hRet = E_FAIL;
00323             }
00324 
00325             ReleaseStgMedium(&stm);
00326         }
00327 
00328         pdo->lpVtbl->Release(pdo);
00329     }
00330 
00331     return hRet;
00332 }
00333 
00334 VOID
00335 FreeObjectPicker(IN IDsObjectPicker *pDsObjectPicker)
00336 {
00337     pDsObjectPicker->lpVtbl->Release(pDsObjectPicker);
00338 }
00339 

Generated on Sun May 27 2012 04:16:46 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.