|
|
1.1 ! root 1: /*++ ! 2: ! 3: Copyright (c) 1990 Microsoft Corporation ! 4: ! 5: Module Name: ! 6: ! 7: util.c ! 8: ! 9: Abstract: ! 10: ! 11: This module provides all the utility functions for the Routing Layer and ! 12: the local Print Providor ! 13: ! 14: Author: ! 15: ! 16: Dave Snipp (DaveSn) 15-Mar-1991 ! 17: ! 18: Revision History: ! 19: ! 20: --*/ ! 21: #define NOMINMAX ! 22: #include <windows.h> ! 23: #include <winspool.h> ! 24: #include <spltypes.h> ! 25: #include <local.h> ! 26: #include <string.h> ! 27: #include <stdlib.h> ! 28: #include <stdio.h> ! 29: #include <stdarg.h> ! 30: ! 31: VOID ! 32: SplInSem( ! 33: VOID ! 34: ) ! 35: { ! 36: if ((DWORD)SpoolerSection.OwningThread != GetCurrentThreadId()) { ! 37: DBGMSG(DBG_ERROR, ("Not in spooler semaphore\n")); ! 38: } ! 39: } ! 40: ! 41: VOID ! 42: SplOutSem( ! 43: VOID ! 44: ) ! 45: { ! 46: if ((DWORD)SpoolerSection.OwningThread == GetCurrentThreadId()) { ! 47: DBGMSG(DBG_ERROR, ("Inside spooler semaphore !!\n")); ! 48: } ! 49: } ! 50: ! 51: VOID ! 52: EnterSplSem( ! 53: VOID ! 54: ) ! 55: { ! 56: EnterCriticalSection(&SpoolerSection); ! 57: ! 58: // WaitForSingleObject(HeapSemaphore, -1); ! 59: } ! 60: ! 61: VOID ! 62: LeaveSplSem( ! 63: VOID ! 64: ) ! 65: { ! 66: LeaveCriticalSection(&SpoolerSection); ! 67: ! 68: // ReleaseSemaphore(HeapSemaphore, 1, NULL); ! 69: } ! 70: ! 71: LPVOID ! 72: AllocSplMem( ! 73: DWORD cb ! 74: ) ! 75: /*++ ! 76: ! 77: Routine Description: ! 78: ! 79: This function will allocate local memory. It will possibly allocate extra ! 80: memory and fill this with debugging information for the debugging version. ! 81: ! 82: Arguments: ! 83: ! 84: cb - The amount of memory to allocate ! 85: ! 86: Return Value: ! 87: ! 88: NON-NULL - A pointer to the allocated memory ! 89: ! 90: FALSE/NULL - The operation failed. Extended error status is available ! 91: using GetLastError. ! 92: ! 93: --*/ ! 94: { ! 95: LPDWORD pMem; ! 96: DWORD cbNew; ! 97: ! 98: SplInSem(); ! 99: ! 100: cbNew = cb+2*sizeof(DWORD); ! 101: if (cbNew & 3) ! 102: cbNew += sizeof(DWORD) - (cbNew & 3); ! 103: ! 104: pMem=(LPDWORD)LocalAlloc(LPTR, cbNew); ! 105: ! 106: if (!pMem) { ! 107: DBGMSG(DBG_ERROR, ("LocalMon: Heap Allocation failed for %d bytes\n", cbNew)); ! 108: return 0; ! 109: } ! 110: ! 111: memset(pMem, 0, cbNew); // This might go later if done in NT ! 112: *pMem=cb; ! 113: *(LPDWORD)((LPBYTE)pMem+cbNew-sizeof(DWORD))=0xdeadbeef; ! 114: ! 115: return (LPVOID)(pMem+1); ! 116: } ! 117: ! 118: BOOL ! 119: FreeSplMem( ! 120: LPVOID pMem, ! 121: DWORD cb ! 122: ) ! 123: { ! 124: DWORD cbNew; ! 125: LPDWORD pNewMem; ! 126: ! 127: SplInSem(); ! 128: ! 129: pNewMem = pMem; ! 130: pNewMem--; ! 131: ! 132: cbNew = cb+2*sizeof(DWORD); ! 133: if (cbNew & 3) ! 134: cbNew += sizeof(DWORD) - (cbNew & 3); ! 135: ! 136: if ((*pNewMem != cb) || ! 137: (*(LPDWORD)((LPBYTE)pNewMem + cbNew - sizeof(DWORD)) != 0xdeadbeef)) { ! 138: DBGMSG(DBG_ERROR, ("Corrupt Memory in spooler : %0lx\n", pNewMem)); ! 139: } ! 140: ! 141: LocalFree((HLOCAL)pNewMem); ! 142: ! 143: return TRUE; ! 144: } ! 145: ! 146: LPVOID ! 147: ReallocSplMem( ! 148: LPVOID pOldMem, ! 149: DWORD cbOld, ! 150: DWORD cbNew ! 151: ) ! 152: { ! 153: LPVOID pNewMem; ! 154: ! 155: pNewMem=AllocSplMem(cbNew); ! 156: ! 157: if (pOldMem) { ! 158: memcpy(pNewMem, pOldMem, min(cbNew, cbOld)); ! 159: FreeSplMem(pOldMem, cbOld); ! 160: } ! 161: ! 162: return pNewMem; ! 163: } ! 164: ! 165: LPWSTR ! 166: AllocSplStr( ! 167: LPWSTR pStr ! 168: ) ! 169: /*++ ! 170: ! 171: Routine Description: ! 172: ! 173: This function will allocate enough local memory to store the specified ! 174: string, and copy that string to the allocated memory ! 175: ! 176: Arguments: ! 177: ! 178: pStr - Pointer to the string that needs to be allocated and stored ! 179: ! 180: Return Value: ! 181: ! 182: NON-NULL - A pointer to the allocated memory containing the string ! 183: ! 184: FALSE/NULL - The operation failed. Extended error status is available ! 185: using GetLastError. ! 186: ! 187: --*/ ! 188: { ! 189: LPWSTR pMem; ! 190: ! 191: if (!pStr) ! 192: return 0; ! 193: ! 194: if (pMem = AllocSplMem( wcslen(pStr)*sizeof(WCHAR) + sizeof(WCHAR) )) ! 195: wcscpy(pMem, pStr); ! 196: ! 197: return pMem; ! 198: } ! 199: ! 200: BOOL ! 201: FreeSplStr( ! 202: LPWSTR pStr ! 203: ) ! 204: { ! 205: return pStr ? FreeSplMem(pStr, wcslen(pStr)*sizeof(WCHAR)+sizeof(WCHAR)) ! 206: : FALSE; ! 207: } ! 208: ! 209: BOOL ! 210: ReallocSplStr( ! 211: LPWSTR *ppStr, ! 212: LPWSTR pStr ! 213: ) ! 214: { ! 215: FreeSplStr(*ppStr); ! 216: *ppStr=AllocSplStr(pStr); ! 217: ! 218: return TRUE; ! 219: } ! 220: ! 221: PINIENTRY ! 222: FindName( ! 223: PINIENTRY pIniKey, ! 224: LPWSTR pName ! 225: ) ! 226: { ! 227: if (pName) { ! 228: while (pIniKey) { ! 229: ! 230: if (!lstrcmpi(pIniKey->pName, pName)) { ! 231: return pIniKey; ! 232: } ! 233: ! 234: pIniKey=pIniKey->pNext; ! 235: } ! 236: } ! 237: ! 238: return FALSE; ! 239: } ! 240: ! 241: PINIENTRY ! 242: FindIniKey( ! 243: PINIENTRY pIniEntry, ! 244: LPWSTR pName ! 245: ) ! 246: { ! 247: if (!pName) ! 248: return NULL; ! 249: ! 250: SplInSem(); ! 251: ! 252: while (pIniEntry && lstrcmpi(pName, pIniEntry->pName)) ! 253: pIniEntry = pIniEntry->pNext; ! 254: ! 255: return pIniEntry; ! 256: } ! 257: ! 258: LPBYTE ! 259: PackStrings( ! 260: LPWSTR *pSource, ! 261: LPBYTE pDest, ! 262: DWORD *DestOffsets, ! 263: LPBYTE pEnd ! 264: ) ! 265: { ! 266: while (*DestOffsets != -1) { ! 267: if (*pSource) { ! 268: pEnd-=wcslen(*pSource)*sizeof(WCHAR) + sizeof(WCHAR); ! 269: *(LPWSTR *)(pDest+*DestOffsets)=wcscpy((LPWSTR)pEnd, *pSource); ! 270: } else ! 271: *(LPWSTR *)(pDest+*DestOffsets)=0; ! 272: pSource++; ! 273: DestOffsets++; ! 274: } ! 275: ! 276: return pEnd; ! 277: } ! 278: ! 279: ! 280: /* Message ! 281: * ! 282: * Displays a message by loading the strings whose IDs are passed into ! 283: * the function, and substituting the supplied variable argument list ! 284: * using the varargs macros. ! 285: * ! 286: */ ! 287: int Message(HWND hwnd, DWORD Type, int CaptionID, int TextID, ...) ! 288: { ! 289: WCHAR MsgText[256]; ! 290: WCHAR MsgFormat[256]; ! 291: WCHAR MsgCaption[40]; ! 292: va_list vargs; ! 293: ! 294: if( ( LoadString( hInst, TextID, MsgFormat, ! 295: sizeof MsgFormat / sizeof *MsgFormat ) > 0 ) ! 296: && ( LoadString( hInst, CaptionID, MsgCaption, ! 297: sizeof MsgCaption / sizeof *MsgCaption ) > 0 ) ) ! 298: { ! 299: va_start( vargs, TextID ); ! 300: wvsprintf( MsgText, MsgFormat, vargs ); ! 301: va_end( vargs ); ! 302: ! 303: return MessageBox(hwnd, MsgText, MsgCaption, Type); ! 304: } ! 305: else ! 306: return 0; ! 307: } ! 308: ! 309: ! 310: /* ! 311: * ! 312: */ ! 313: LPTSTR ! 314: GetErrorString( ! 315: DWORD Error ! 316: ) ! 317: { ! 318: TCHAR Buffer[1024]; ! 319: LPTSTR pErrorString = NULL; ! 320: ! 321: if( FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, ! 322: NULL, Error, 0, Buffer, ! 323: sizeof(Buffer), NULL ) ! 324: == 0 ) ! 325: ! 326: LoadString( hInst, IDS_UNKNOWN_ERROR, Buffer, ! 327: sizeof(Buffer) / sizeof(*Buffer) ); ! 328: ! 329: pErrorString = AllocSplStr(Buffer); ! 330: ! 331: return pErrorString; ! 332: } ! 333: ! 334: ! 335: ! 336: ! 337: DWORD ReportError( HWND hwndParent, ! 338: DWORD idTitle, ! 339: DWORD idDefaultError ) ! 340: { ! 341: DWORD ErrorID; ! 342: DWORD MsgType; ! 343: LPTSTR pErrorString; ! 344: ! 345: ErrorID = GetLastError( ); ! 346: ! 347: if( ErrorID == ERROR_ACCESS_DENIED ) ! 348: MsgType = MSG_INFORMATION; ! 349: else ! 350: MsgType = MSG_ERROR; ! 351: ! 352: ! 353: pErrorString = GetErrorString( ErrorID ); ! 354: ! 355: Message( hwndParent, MsgType, idTitle, ! 356: idDefaultError, pErrorString ); ! 357: ! 358: FreeSplStr( pErrorString ); ! 359: ! 360: ! 361: return ErrorID; ! 362: } ! 363:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.