home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / winnt / dbmon / dbmon.c next >
C/C++ Source or Header  |  1996-04-22  |  4KB  |  166 lines

  1. /*++
  2.  
  3. Copyright (c) 1990  Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     dbmon.c
  8.  
  9. Abstract:
  10.  
  11.     A simple program to print strings passed to OutputDebugString when
  12.     the app printing the strings is not being debugged.
  13.  
  14. Author:
  15.  
  16.     Kent Forschmiedt (kentf) 30-Sep-1994
  17.  
  18. Revision History:
  19.  
  20. --*/
  21.  
  22. #include <windows.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. int _cdecl
  27. main(
  28.     int argc,
  29.     char ** argv
  30.     )
  31. /*++
  32.  
  33. Routine Description:
  34.  
  35.  
  36. Arguments:
  37.  
  38.  
  39. Return Value:
  40.  
  41.  
  42. --*/
  43. {
  44.     HANDLE AckEvent;
  45.     HANDLE ReadyEvent;
  46.     HANDLE SharedFile;
  47.     LPVOID SharedMem;
  48.     LPSTR  String;
  49.     DWORD  ret;
  50.     DWORD  LastPid;
  51.     LPDWORD pThisPid;
  52.     BOOL    DidCR;
  53.  
  54.     SECURITY_ATTRIBUTES sa;
  55.     SECURITY_DESCRIPTOR sd;
  56.  
  57.     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  58.     sa.bInheritHandle = TRUE;
  59.     sa.lpSecurityDescriptor = &sd;
  60.  
  61.     if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) {
  62.         fprintf(stderr,"unable to InitializeSecurityDescriptor, err == %d\n",
  63.             GetLastError());
  64.         exit(1);
  65.     }
  66.  
  67.     if(!SetSecurityDescriptorDacl(&sd, TRUE, (PACL)NULL, FALSE)) {
  68.         fprintf(stderr,"unable to SetSecurityDescriptorDacl, err == %d\n",
  69.             GetLastError());
  70.         exit(1);
  71.     }
  72.  
  73.     AckEvent = CreateEvent(&sa, FALSE, FALSE, "DBWIN_BUFFER_READY");
  74.  
  75.     if (!AckEvent) {
  76.         fprintf(stderr,
  77.                 "dbmon: Unable to create synchronization object, err == %d\n",
  78.                 GetLastError());
  79.         exit(1);
  80.     }
  81.  
  82.     if (GetLastError() == ERROR_ALREADY_EXISTS) {
  83.         fprintf(stderr, "dbmon: already running\n");
  84.         exit(1);
  85.     }
  86.  
  87.     ReadyEvent = CreateEvent(&sa, FALSE, FALSE, "DBWIN_DATA_READY");
  88.  
  89.     if (!ReadyEvent) {
  90.         fprintf(stderr,
  91.                 "dbmon: Unable to create synchronization object, err == %d\n",
  92.                 GetLastError());
  93.         exit(1);
  94.     }
  95.  
  96.     SharedFile = CreateFileMapping(
  97.                         (HANDLE)-1,
  98.                         &sa,
  99.                         PAGE_READWRITE,
  100.                         0,
  101.                         4096,
  102.                         "DBWIN_BUFFER");
  103.  
  104.     if (!SharedFile) {
  105.         fprintf(stderr,
  106.                 "dbmon: Unable to create file mapping object, err == %d\n",
  107.                 GetLastError());
  108.         exit(1);
  109.     }
  110.  
  111.     SharedMem = MapViewOfFile(
  112.                         SharedFile,
  113.                         FILE_MAP_READ,
  114.                         0,
  115.                         0,
  116.                         512);
  117.  
  118.     if (!SharedMem) {
  119.         fprintf(stderr,
  120.                 "dbmon: Unable to map shared memory, err == %d\n",
  121.                 GetLastError());
  122.         exit(1);
  123.     }
  124.  
  125.     String = (LPSTR)SharedMem + sizeof(DWORD);
  126.     pThisPid = SharedMem;
  127.  
  128.     LastPid = 0xffffffff;
  129.     DidCR = TRUE;
  130.  
  131.     SetEvent(AckEvent);
  132.  
  133.     for (;;) {
  134.  
  135.         ret = WaitForSingleObject(ReadyEvent, INFINITE);
  136.  
  137.         if (ret != WAIT_OBJECT_0) {
  138.  
  139.             fprintf(stderr, "dbmon: wait failed; err == %d\n", GetLastError());
  140.             exit(1);
  141.  
  142.         } else {
  143.  
  144.             if (LastPid != *pThisPid) {
  145.                 LastPid = *pThisPid;
  146.                 if (!DidCR) {
  147.                     putchar('\n');
  148.                     DidCR = TRUE;
  149.                 }
  150.             }
  151.  
  152.             if (DidCR) {
  153.                 printf("%3u: ", LastPid);
  154.             }
  155.  
  156.             printf("%s", String);
  157.             DidCR = (*String && (String[strlen(String) - 1] == '\n'));
  158.             SetEvent(AckEvent);
  159.  
  160.         }
  161.  
  162.     }
  163.  
  164.     return 0;
  165. }
  166.