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  * PROJECT:     ReactOS Device Managment
00003  * LICENSE:     GPL - See COPYING in the top level directory
00004  * FILE:        base/system/devmgmt/misc.c
00005  * PURPOSE:     miscallanous functions
00006  * COPYRIGHT:   Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
00007  *
00008  */
00009 
00010 #include "precomp.h"
00011 
00012 static INT
00013 LengthOfStrResource(IN HINSTANCE hInst,
00014                     IN UINT uID)
00015 {
00016     HRSRC hrSrc;
00017     HGLOBAL hRes;
00018     LPWSTR lpName, lpStr;
00019 
00020     if (hInst == NULL)
00021     {
00022         return -1;
00023     }
00024 
00025     /* There are always blocks of 16 strings */
00026     lpName = (LPWSTR)MAKEINTRESOURCE((uID >> 4) + 1);
00027 
00028     /* Find the string table block */
00029     if ((hrSrc = FindResourceW(hInst, lpName, (LPWSTR)RT_STRING)) &&
00030         (hRes = LoadResource(hInst, hrSrc)) &&
00031         (lpStr = (LPWSTR)LockResource(hRes)))
00032     {
00033         UINT x;
00034 
00035         /* Find the string we're looking for */
00036         uID &= 0xF; /* position in the block, same as % 16 */
00037         for (x = 0; x < uID; x++)
00038         {
00039             lpStr += (*lpStr) + 1;
00040         }
00041 
00042         /* Found the string */
00043         return (int)(*lpStr);
00044     }
00045     return -1;
00046 }
00047 
00048 INT
00049 AllocAndLoadString(OUT LPTSTR *lpTarget,
00050                    IN HINSTANCE hInst,
00051                    IN UINT uID)
00052 {
00053     INT ln;
00054 
00055     ln = LengthOfStrResource(hInst,
00056                              uID);
00057     if (ln++ > 0)
00058     {
00059         (*lpTarget) = (LPTSTR)LocalAlloc(LMEM_FIXED,
00060                                          ln * sizeof(TCHAR));
00061         if ((*lpTarget) != NULL)
00062         {
00063             INT Ret;
00064             if (!(Ret = LoadString(hInst, uID, *lpTarget, ln)))
00065             {
00066                 LocalFree((HLOCAL)(*lpTarget));
00067             }
00068             return Ret;
00069         }
00070     }
00071     return 0;
00072 }
00073 
00074 DWORD
00075 LoadAndFormatString(IN HINSTANCE hInstance,
00076                     IN UINT uID,
00077                     OUT LPTSTR *lpTarget,
00078                     ...)
00079 {
00080     DWORD Ret = 0;
00081     LPTSTR lpFormat;
00082     va_list lArgs;
00083 
00084     if (AllocAndLoadString(&lpFormat,
00085                            hInstance,
00086                            uID) > 0)
00087     {
00088         va_start(lArgs, lpTarget);
00089         /* let's use FormatMessage to format it because it has the ability to allocate
00090            memory automatically */
00091         Ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
00092                             lpFormat,
00093                             0,
00094                             0,
00095                             (LPTSTR)lpTarget,
00096                             0,
00097                             &lArgs);
00098         va_end(lArgs);
00099 
00100         LocalFree((HLOCAL)lpFormat);
00101     }
00102 
00103     return Ret;
00104 }
00105 
00106 BOOL
00107 StatusBarLoadAndFormatString(IN HWND hStatusBar,
00108                              IN INT PartId,
00109                              IN HINSTANCE hInstance,
00110                              IN UINT uID,
00111                              ...)
00112 {
00113     BOOL Ret = FALSE;
00114     LPTSTR lpFormat, lpStr;
00115     va_list lArgs;
00116 
00117     if (AllocAndLoadString(&lpFormat,
00118                            hInstance,
00119                            uID) > 0)
00120     {
00121         va_start(lArgs, uID);
00122         /* let's use FormatMessage to format it because it has the ability to allocate
00123            memory automatically */
00124         Ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
00125                             lpFormat,
00126                             0,
00127                             0,
00128                             (LPTSTR)&lpStr,
00129                             0,
00130                             &lArgs);
00131         va_end(lArgs);
00132 
00133         if (lpStr != NULL)
00134         {
00135             Ret = (BOOL)SendMessage(hStatusBar,
00136                                     SB_SETTEXT,
00137                                     (WPARAM)PartId,
00138                                     (LPARAM)lpStr);
00139             LocalFree((HLOCAL)lpStr);
00140         }
00141 
00142         LocalFree((HLOCAL)lpFormat);
00143     }
00144 
00145     return Ret;
00146 }
00147 
00148 BOOL
00149 StatusBarLoadString(IN HWND hStatusBar,
00150                     IN INT PartId,
00151                     IN HINSTANCE hInstance,
00152                     IN UINT uID)
00153 {
00154     BOOL Ret = FALSE;
00155     LPTSTR lpStr;
00156 
00157     if (AllocAndLoadString(&lpStr,
00158                            hInstance,
00159                            uID) > 0)
00160     {
00161         Ret = (BOOL)SendMessage(hStatusBar,
00162                                 SB_SETTEXT,
00163                                 (WPARAM)PartId,
00164                                 (LPARAM)lpStr);
00165         LocalFree((HLOCAL)lpStr);
00166     }
00167 
00168     return Ret;
00169 }
00170 
00171 
00172 INT
00173 GetTextFromEdit(OUT LPTSTR lpString,
00174                 IN HWND hDlg,
00175                 IN UINT Res)
00176 {
00177     INT len = GetWindowTextLength(GetDlgItem(hDlg, Res));
00178     if(len > 0)
00179     {
00180         GetDlgItemText(hDlg,
00181                        Res,
00182                        lpString,
00183                        len + 1);
00184     }
00185     else
00186         lpString = NULL;
00187 
00188     return len;
00189 }
00190 
00191 
00192 HIMAGELIST
00193 InitImageList(UINT StartResource,
00194               UINT EndResource,
00195               UINT Width,
00196               UINT Height)
00197 {
00198     HBITMAP hBitmap;
00199     HIMAGELIST hImageList;
00200     UINT i;
00201     INT Ret;
00202 
00203     /* Create the toolbar icon image list */
00204     hImageList = ImageList_Create(Width,
00205                                   Height,
00206                                   ILC_MASK | ILC_COLOR24,
00207                                   EndResource - StartResource,
00208                                   0);
00209     if (hImageList == NULL)
00210         return NULL;
00211 
00212     /* Add all icons to the image list */
00213     for (i = StartResource; i <= EndResource; i++)
00214     {
00215         hBitmap = (HBITMAP)LoadImage(hInstance,
00216                                      MAKEINTRESOURCE(i),
00217                                      IMAGE_BITMAP,
00218                                      Width,
00219                                      Height,
00220                                      LR_LOADTRANSPARENT);
00221         if (hBitmap == NULL)
00222             return NULL;
00223 
00224         Ret = ImageList_AddMasked(hImageList,
00225                                   hBitmap,
00226                                   RGB(255, 0, 128));
00227         if (Ret == -1)
00228             return NULL;
00229 
00230         DeleteObject(hBitmap);
00231     }
00232 
00233     return hImageList;
00234 }
00235 
00236 
00237 VOID GetError(VOID)
00238 {
00239     LPVOID lpMsgBuf;
00240 
00241     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00242                   FORMAT_MESSAGE_FROM_SYSTEM |
00243                   FORMAT_MESSAGE_IGNORE_INSERTS,
00244                   NULL,
00245                   GetLastError(),
00246                   MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00247                   (LPTSTR) &lpMsgBuf,
00248                   0,
00249                   NULL );
00250 
00251     MessageBox(NULL, lpMsgBuf, _T("Error!"), MB_OK | MB_ICONERROR);
00252 
00253     LocalFree(lpMsgBuf);
00254 }
00255 
00256 
00257 VOID DisplayString(LPTSTR Msg)
00258 {
00259     MessageBox(NULL, Msg, _T("Note!"), MB_ICONEXCLAMATION|MB_OK);
00260 }

Generated on Fri May 25 2012 04:15:07 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.