Annotation of mstools/samples/sdktools/spy/app/misc.c, revision 1.1.1.1

1.1       root        1: 
                      2: /******************************************************************************\
                      3: *       This is a part of the Microsoft Source Code Samples. 
                      4: *       Copyright (C) 1993 Microsoft Corporation.
                      5: *       All rights reserved. 
                      6: *       This source code is only intended as a supplement to 
                      7: *       Microsoft Development Tools and/or WinHelp documentation.
                      8: *       See these sources for detailed information regarding the 
                      9: *       Microsoft samples programs.
                     10: \******************************************************************************/
                     11: 
                     12: /*****************************************************************************\
                     13: *
                     14: * Module: misc.c
                     15: *
                     16: *   Contains miscellaneous routines for the Windows debugging Spy SDK applet.
                     17: *
                     18: * Functions:
                     19: *
                     20: *   ReadRegistry()
                     21: *   WriteRegistry()
                     22: *   Message()
                     23: *   SetSpyCaption()
                     24: *   GetWindowName()
                     25: *   StripExtension()
                     26: *
                     27: * Comments:
                     28: *
                     29: \*****************************************************************************/
                     30: 
                     31: #include "spy.h"
                     32: #include <string.h>
                     33: 
                     34: 
                     35: //
                     36: // Registry flags for the "Flags" value.
                     37: //
                     38: #define REGFLAG_OUTPUTWIN           0x00000001
                     39: #define REGFLAG_OUTPUTCOM1          0x00000002
                     40: #define REGFLAG_OUTPUTFILE          0x00000004
                     41: #define REGFLAG_MSGSUSER            0x00000010
                     42: #define REGFLAG_MSGSUNKNOWN         0x00000020
                     43: 
                     44: 
                     45: PRIVATE HKEY ghkeySpy = NULL;
                     46: PRIVATE CHAR gszSpyAppKey[] = "Software\\Microsoft\\Spy";
                     47: PRIVATE CHAR gszKeyPosition[] = "Position";
                     48: PRIVATE CHAR gszKeyFont[] = "Font";
                     49: PRIVATE CHAR gszKeyMessages[] = "Messages";
                     50: PRIVATE CHAR gszKeyFileName[] = "FileName";
                     51: PRIVATE CHAR gszKeyLines[] = "Lines";
                     52: PRIVATE CHAR gszKeyFlags[] = "Flags";
                     53: PRIVATE CHAR gszDefFileName[] = "spy.log";
                     54: PRIVATE BYTE BitTable[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
                     55: 
                     56: 
                     57: PRIVATE VOID GetWindowName(HWND hwnd, PSTR sz);
                     58: PRIVATE LPSTR StripExtension(LPSTR pszFileName);
                     59: 
                     60: 
                     61: 
                     62: 
                     63: /*****************************************************************************\
                     64: * ReadRegistry
                     65: *
                     66: * Opens (creates if necessary) the registry key for spy preferences and then
                     67: * reads the last saved values.
                     68: *
                     69: * Arguments:
                     70: *    none
                     71: *
                     72: * Returns:
                     73: *    VOID
                     74: \*****************************************************************************/
                     75: 
                     76: VOID
                     77: ReadRegistry(
                     78:     VOID
                     79:     )
                     80: {
                     81:     LOGFONT lf;
                     82:     BYTE abMsgs[128];
                     83:     DWORD fFlags;
                     84:     HDC hdc;
                     85:     INT i;
                     86:     DWORD dwType;
                     87:     DWORD cbData;
                     88: 
                     89:     RegCreateKey(HKEY_CURRENT_USER, gszSpyAppKey, &ghkeySpy);
                     90: 
                     91:     cbData = sizeof(gwndpl);
                     92:     if (!ghkeySpy || RegQueryValueEx(ghkeySpy, gszKeyPosition, NULL, &dwType,
                     93:         (LPVOID)&gwndpl, &cbData) != ERROR_SUCCESS)
                     94:     {
                     95:         gwndpl.length = sizeof(gwndpl);
                     96:         gwndpl.flags = 0;
                     97:         gwndpl.showCmd = SW_SHOWNORMAL;
                     98:         gwndpl.ptMinPosition.x = 0;
                     99:         gwndpl.ptMinPosition.y = 0;
                    100:         gwndpl.ptMaxPosition.x = 0;
                    101:         gwndpl.ptMaxPosition.y = 0;
                    102:         gwndpl.rcNormalPosition.left = 10;
                    103:         gwndpl.rcNormalPosition.top = 10;
                    104:         gwndpl.rcNormalPosition.right =
                    105:             10 + (GetSystemMetrics(SM_CXSCREEN) / 3);
                    106:         gwndpl.rcNormalPosition.bottom =
                    107:             10 + (GetSystemMetrics(SM_CYSCREEN) / 3);
                    108:     }
                    109: 
                    110:     cbData = sizeof(lf);
                    111:     if (!ghkeySpy || RegQueryValueEx(ghkeySpy, gszKeyFont, NULL, &dwType,
                    112:         (LPVOID)&lf, &cbData) != ERROR_SUCCESS)
                    113:     {
                    114:         hdc = GetDC(NULL);
                    115:         GetObject(GetStockObject(SYSTEM_FONT), sizeof(lf), &lf);
                    116:         ReleaseDC(NULL, hdc);
                    117:     }
                    118: 
                    119:     ghfontPrintf = CreateFontIndirect(&lf);
                    120: 
                    121:     cbData = sizeof(abMsgs);
                    122:     if (!ghkeySpy || RegQueryValueEx(ghkeySpy, gszKeyMessages, NULL, &dwType,
                    123:         (LPVOID)abMsgs, &cbData) != ERROR_SUCCESS)
                    124:     {
                    125:         //
                    126:         // Select all messages by default
                    127:         //
                    128:         for (i = 0; i < gcMessages; i++)
                    129:         {
                    130:             gaMsgs[i].Flags |= MTF_SELECTED;
                    131:         }
                    132:     }
                    133:     else
                    134:     {
                    135:         for (i = 0; i < gcMessages; i++)
                    136:         {
                    137:             if (abMsgs[gaMsgs[i].msg >> 3] & BitTable[gaMsgs[i].msg & 0x07])
                    138:                 gaMsgs[i].Flags |= MTF_SELECTED;
                    139:         }
                    140:     }
                    141: 
                    142:     cbData = MAXSTRING * sizeof(TCHAR);
                    143:     if (!ghkeySpy || RegQueryValueEx(ghkeySpy, gszKeyFileName, NULL, &dwType,
                    144:         (LPVOID)gszFile, &cbData) != ERROR_SUCCESS)
                    145:     {
                    146:         lstrcpy(gszFile, gszDefFileName);
                    147:     }
                    148: 
                    149:     cbData = sizeof(DWORD);
                    150:     if (!ghkeySpy || RegQueryValueEx(ghkeySpy, gszKeyLines, NULL, &dwType,
                    151:         (LPVOID)&gnLines, &cbData) != ERROR_SUCCESS ||
                    152:         gnLines > LINES_MAX)
                    153:     {
                    154:         gnLines = LINES_MAX;
                    155:     }
                    156: 
                    157:     cbData = sizeof(DWORD);
                    158:     if (!ghkeySpy || RegQueryValueEx(ghkeySpy, gszKeyFlags, NULL, &dwType,
                    159:         (LPVOID)&fFlags, &cbData) != ERROR_SUCCESS)
                    160:     {
                    161:         gfOutputWin = TRUE;
                    162:         gfOutputCom1 = FALSE;
                    163:         gfOutputFile = FALSE;
                    164:         gfMsgsUser = TRUE;
                    165:         gfMsgsUnknown = TRUE;
                    166:     }
                    167:     else
                    168:     {
                    169:         if (fFlags & REGFLAG_OUTPUTWIN)
                    170:             gfOutputWin = TRUE;
                    171: 
                    172:         if (fFlags & REGFLAG_OUTPUTCOM1)
                    173:             gfOutputCom1 = TRUE;
                    174: 
                    175:         if (fFlags & REGFLAG_OUTPUTFILE)
                    176:             gfOutputFile = TRUE;
                    177: 
                    178:         if (fFlags & REGFLAG_MSGSUSER)
                    179:             gfMsgsUser = TRUE;
                    180: 
                    181:         if (fFlags & REGFLAG_MSGSUNKNOWN)
                    182:             gfMsgsUnknown = TRUE;
                    183:     }
                    184: 
                    185:     if (gfOutputFile)
                    186:     {
                    187:         gfhFile = _lcreat(gszFile, 0);
                    188:         if (gfhFile == (HFILE)-1)        //BUGBUG put up a message here.
                    189:             gfhFile = 0;
                    190:     }
                    191: }
                    192: 
                    193: 
                    194: 
                    195: /*****************************************************************************\
                    196: * WriteRegistry
                    197: *
                    198: * Writes out preference data to the registry when the app exits, then
                    199: * closes the registry key.
                    200: *
                    201: * Arguments:
                    202: *    none
                    203: *
                    204: * Returns:
                    205: *    VOID
                    206: \*****************************************************************************/
                    207: 
                    208: VOID
                    209: WriteRegistry(
                    210:     VOID
                    211:     )
                    212: {
                    213:     LOGFONT lf;
                    214:     BYTE abMsgs[128];
                    215:     INT i;
                    216:     DWORD fFlags;
                    217:     WINDOWPLACEMENT wndpl;
                    218: 
                    219:     if (ghkeySpy)
                    220:     {
                    221:         GetWindowPlacement(ghwndSpyApp, &wndpl);
                    222:         RegSetValueEx(ghkeySpy, gszKeyPosition, 0, REG_BINARY,
                    223:             (LPBYTE)&wndpl, sizeof(wndpl));
                    224: 
                    225:         GetObject(ghfontPrintf, sizeof(lf), &lf);
                    226:         RegSetValueEx(ghkeySpy, gszKeyFont, 0, REG_BINARY,
                    227:             (LPBYTE)&lf, sizeof(lf));
                    228: 
                    229:         memset(abMsgs, 0, sizeof(abMsgs));
                    230:         for (i = 0; i < gcMessages; i++)
                    231:         {
                    232:             if (gaMsgs[i].Flags & MTF_SELECTED)
                    233:                 abMsgs[gaMsgs[i].msg >> 3] |= BitTable[gaMsgs[i].msg & 0x07];
                    234:         }
                    235: 
                    236:         RegSetValueEx(ghkeySpy, gszKeyMessages, 0, REG_BINARY,
                    237:             (LPBYTE)&abMsgs, sizeof(abMsgs));
                    238: 
                    239:         RegSetValueEx(ghkeySpy, gszKeyFileName, 0, REG_SZ,
                    240:             (LPBYTE)gszFile, (lstrlen(gszFile) + 1) * sizeof(TCHAR));
                    241: 
                    242:         RegSetValueEx(ghkeySpy, gszKeyLines, 0, REG_DWORD,
                    243:             (LPBYTE)&gnLines, sizeof(DWORD));
                    244: 
                    245:         fFlags = 0;
                    246:         if (gfOutputWin)
                    247:             fFlags |= REGFLAG_OUTPUTWIN;
                    248: 
                    249:         if (gfOutputCom1)
                    250:             fFlags |= REGFLAG_OUTPUTCOM1;
                    251: 
                    252:         if (gfOutputFile)
                    253:             fFlags |= REGFLAG_OUTPUTFILE;
                    254: 
                    255:         if (gfMsgsUser)
                    256:             fFlags |= REGFLAG_MSGSUSER;
                    257: 
                    258:         if (gfMsgsUnknown)
                    259:             fFlags |= REGFLAG_MSGSUNKNOWN;
                    260: 
                    261:         RegSetValueEx(ghkeySpy, gszKeyFlags, 0, REG_DWORD,
                    262:             (LPBYTE)&fFlags, sizeof(DWORD));
                    263: 
                    264:         RegCloseKey(ghkeySpy);
                    265:     }
                    266: }
                    267: 
                    268: 
                    269: 
                    270: /*****************************************************************************\
                    271: * Message
                    272: *
                    273: * Puts up a message box.
                    274: *
                    275: * Arguments:
                    276: *   UINT fuStyle    - Flags for MessageBox (MB_YESNOCANCEL, etc).
                    277: *   LPSTR pszFormat - Format string for the message.
                    278: *
                    279: * Returns:
                    280: *   Whatever MessageBox returns.
                    281: *
                    282: \*****************************************************************************/
                    283: 
                    284: INT
                    285: Message(
                    286:     UINT fuStyle,
                    287:     LPSTR pszFormat,
                    288:     ...
                    289:     )
                    290: {
                    291:     va_list marker;
                    292:     INT RetCode;
                    293:     TCHAR szT[MAXSTRING];
                    294: 
                    295:     va_start(marker, pszFormat);
                    296:     wvsprintf(szT, pszFormat, marker);
                    297:     RetCode = MessageBox(ghwndSpyApp, szT, gszAppName, fuStyle | MB_TASKMODAL);
                    298:     va_end(marker);
                    299: 
                    300:     return RetCode;
                    301: }
                    302: 
                    303: 
                    304: 
                    305: /*****************************************************************************\
                    306: * SetSpyCaption
                    307: *
                    308: * This routine sets the Spy app's caption bar to display info on the window
                    309: * that is currently being spy'ed upon.
                    310: *
                    311: * Arguments:
                    312: *    none
                    313: *
                    314: * Returns:
                    315: *    VOID
                    316: \*****************************************************************************/
                    317: 
                    318: VOID
                    319: SetSpyCaption(
                    320:     VOID
                    321:     )
                    322: {
                    323:     CHAR szText[MAXSTRING];
                    324:     CHAR szTemp[MAXSTRING];
                    325: 
                    326:     if (ghwndSpyingOn != NULL && ghwndSpyingOn != HWND_ALL)
                    327:     {
                    328:         GetWindowName(ghwndSpyingOn, szTemp);
                    329: 
                    330:         if (lstrlen(gszAppName) + lstrlen(szTemp) + 3 > MAXSTRING)
                    331:             szTemp[MAXSTRING - 3 - lstrlen(szTemp)] = 0;
                    332: 
                    333:         if (gfSpyOn)
                    334:             wsprintf(szText, "%s - %s", gszAppName, szTemp);
                    335:         else
                    336:             wsprintf(szText, "<%s - %s>", gszAppName, szTemp);
                    337:     }
                    338:     else
                    339:     {
                    340:         lstrcpy(szText, gszAppName);
                    341:     }
                    342: 
                    343:     SetWindowText(ghwndSpyApp, szText);
                    344: }
                    345: 
                    346: 
                    347: 
                    348: /*****************************************************************************\
                    349: * GetWindowName
                    350: *
                    351: * Builds the name of the window being spy'd on in the specified buffer.
                    352: * This will be something like "EXENAME!WindowText" or "EXENAME!Class".
                    353: *
                    354: * Arguments:
                    355: *
                    356: *    HWND hwnd - handle to the window being spy'd on.
                    357: *    PSTR pstr - pointer to string to return.
                    358: *
                    359: * Returns:
                    360: *    VOID
                    361: \*****************************************************************************/
                    362: 
                    363: PRIVATE VOID
                    364: GetWindowName(
                    365:     HWND hwnd,
                    366:     PSTR sz
                    367:     )
                    368: {
                    369:     PSTR szSave = sz;
                    370: 
                    371:     if (hwnd != NULL && IsWindow(hwnd))
                    372:     {
                    373:         /*
                    374:          * Get the module name
                    375:          */
                    376:         GetModuleFileName((HANDLE)GetWindowLong(hwnd, GWL_HINSTANCE),
                    377:             sz, MAXSTRING);
                    378:         lstrcpy(sz, StripExtension(sz));
                    379:         sz += lstrlen(sz);
                    380:         *sz++ = '!';
                    381:         *sz = 0;
                    382: 
                    383:         GetWindowText(hwnd, sz, MAXSTRING - (sz - szSave));
                    384: 
                    385:         /*
                    386:          * If the window has no caption string then use the Class name
                    387:          */
                    388:         if (*sz == 0)
                    389:             GetClassName(hwnd, sz, MAXSTRING - (sz - szSave));
                    390:     }
                    391:     else
                    392:     {
                    393:         *sz = 0;
                    394:     }
                    395: }
                    396: 
                    397: 
                    398: 
                    399: /*****************************************************************************\
                    400: * StripExtension
                    401: *
                    402: *   Strips the extension off of a filename.
                    403: *
                    404: * Arguments:
                    405: *   LPSTR pszFileName - File name to process.
                    406: *
                    407: * Returns:
                    408: *   Returns a pointer to the beginning of the filename.  The extension
                    409: *   will have been stripped off.
                    410: *
                    411: \*****************************************************************************/
                    412: 
                    413: PRIVATE LPSTR
                    414: StripExtension(
                    415:     LPSTR pszFileName
                    416:     )
                    417: {
                    418:     LPSTR p = pszFileName;
                    419: 
                    420:     while (*p)
                    421:         p++;
                    422: 
                    423:     while (p >= pszFileName && *p != '\\')
                    424:     {
                    425:         if (*p == '.')
                    426:             *p = 0;
                    427: 
                    428:         p--;
                    429:     }
                    430: 
                    431:     return ++p;
                    432: }
                    433: 
                    434: 
                    435: 
                    436: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.