Перехват вывода OutputDebigString
От: A.Ryzhov Россия  
Дата: 27.08.03 06:56
Оценка:
Hello ALL.
Возник вопрос: как перехватить вывод, производимый функцией OutputDebugString?
(из win32 приложения на той-же машине)
Заранее спасибо.
Best regards,
A.Ryzhov
Re: Перехват вывода OutputDebigString
От: Holms США  
Дата: 27.08.03 08:11
Оценка: 17 (2)
Здравствуйте, A.Ryzhov, Вы писали:


если не разберешся могу выслать целый проект

#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <stdlib.h>
#include "resource.h"

HWND      hMainWnd, hEdit;
HINSTANCE hInst;

#define QUEUE_SIZE (256)
size_t  iQueues, iQueuee;
char    *szQueue[QUEUE_SIZE];

LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp);
void SetPlacement(HWND hWnd, const char *s);
void GetPlacement(HWND hWnd, char *ach);

HWND OpenWindow()
{
  WNDCLASS wClass;
  HWND     hWnd;
  
  wClass.style         = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  wClass.lpfnWndProc   = WindowProc;
  wClass.cbClsExtra    = 0;
  wClass.cbWndExtra    = 0;
  wClass.hInstance     = hInst;
  wClass.hIcon         = NULL; //LoadIcon(NULL, IDI_APPLICATION);
  wClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wClass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  wClass.lpszMenuName  = "MainMenu";
  wClass.lpszClassName = "DBWin32";
  RegisterClass(&wClass);
  
  hWnd = CreateWindowEx(WS_EX_TOPMOST, //WS_EX_PALETTEWINDOW, 
        "DBWin32",
        "DBWin32", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, (HMENU)NULL, hInst, NULL);
  return hWnd;
}

void _AddToEdit(char *txt)
{
  int   iLength;
  char  szLine[520], *p;
  
  if( (iLength = Edit_GetLineCount(hEdit)) > (512) )
    {
    int ii = iLength - 512;
    ii = Edit_LineIndex(hEdit, ii);
    Edit_SetSel(hEdit, 0, ii);
    Edit_ReplaceSel(hEdit, "");
    }
  iLength = Edit_GetTextLength(hEdit);
  Edit_SetSel(hEdit, iLength, iLength);
  for(p = szLine; *txt; )
    if( *txt == '\n' || (p - szLine) > 512 )
      { 
      *p ++ = '\r'; 
      *p ++ = '\n'; 
      *p = 0;
      Edit_ReplaceSel(hEdit, szLine);
      p = szLine;
      txt ++ ;
      }
    else
      *p ++ = *txt ++;
  *p = 0;
  if( *szLine )
    Edit_ReplaceSel(hEdit, szLine);
}

void AddToEdit(char *txt)
{
  szQueue[iQueuee++] = strdup(txt);
  iQueuee &= QUEUE_SIZE-1;
}

void FillToEdit()
{
  while( iQueues != iQueuee )
    {
    _AddToEdit(szQueue[iQueues]);
    free(szQueue[iQueues]);
    szQueue[iQueues] = NULL;
    iQueues = (iQueues + 1) & (QUEUE_SIZE-1);
    }
}


int DoEventsWindow(HWND hDlg)
{
  MSG msg;

  while( PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) )
    {
    GetMessage(&msg, 0, 0, 0);
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
  return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nCmdShow)
{
  HANDLE  AckEvent, ReadyEvent;
  HANDLE  SharedFile, SharedMem;
  char    *buffer;
  DWORD   ret, LastPid;
  DWORD   *pThisPid;
  BOOL    DidCR;
  char    txt[256];
  MSG     msg;
  SECURITY_ATTRIBUTES sa;
  SECURITY_DESCRIPTOR sd;

  hInst = hInstance;
  sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  sa.bInheritHandle = TRUE;
  sa.lpSecurityDescriptor = &sd;

  if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) 
    {
    sprintf(txt,"Unable to InitializeSecurityDescriptor, err == %d\n", GetLastError());
    MessageBox(NULL, txt, "DBWin32", MB_OK | MB_ICONSTOP);
    return 0;
    }
  if(!SetSecurityDescriptorDacl(&sd, TRUE, (PACL)NULL, FALSE)) 
    {
    sprintf(txt,"Unable to SetSecurityDescriptorDacl, err == %d\n", GetLastError());
    MessageBox(NULL, txt, "DBWin32", MB_OK | MB_ICONSTOP);
    return 0;
    }

  AckEvent = CreateEvent(&sa, FALSE, FALSE, "DBWIN_BUFFER_READY");
  if (!AckEvent) 
    {
    sprintf(txt, "Unable to create synchronization object, err == %d\n", GetLastError());
    MessageBox(NULL, txt, "DBWin32", MB_OK | MB_ICONSTOP);
    return 0;
    }
  if (GetLastError() == ERROR_ALREADY_EXISTS) 
    {
    sprintf(txt, "Already running\n");
    MessageBox(NULL, txt, "DBWin32", MB_OK | MB_ICONSTOP);
    return 0;
    }
  ReadyEvent = CreateEvent(&sa, FALSE, FALSE, "DBWIN_DATA_READY");
  if (!ReadyEvent) 
    {
    sprintf(txt,"Unable to create synchronization object, err == %d\n", GetLastError());
    MessageBox(NULL, txt, "DBWin32", MB_OK | MB_ICONSTOP);
    return 0;
    }

  SharedFile = CreateFileMapping( (HANDLE)-1, &sa, PAGE_READWRITE, 0, 4096, "DBWIN_BUFFER");
  if (!SharedFile) 
    {
    sprintf(txt, "Unable to create file mapping object, err == %d\n", GetLastError());
    MessageBox(NULL, txt, "DBWin32", MB_OK | MB_ICONSTOP);
    return 0;
    }

  SharedMem = MapViewOfFile(SharedFile, FILE_MAP_READ, 0, 0, 512);

  if (!SharedMem) 
    {
    sprintf(txt, "Unable to map shared memory, err == %d\n", GetLastError());
    MessageBox(NULL, txt, "DBWin32", MB_OK | MB_ICONSTOP);
    return 0;
    }

  hMainWnd = OpenWindow();
  GetPrivateProfileString("Init", "Window", "", txt, 256, "DBWin32.ini");
  SetPlacement(hMainWnd, txt);
  UpdateWindow(hMainWnd);

  buffer = (LPSTR)SharedMem + sizeof(DWORD);
  pThisPid = (DWORD*)SharedMem;
  LastPid = 0xffffffff;
  DidCR = TRUE;
  SetEvent(AckEvent);
  for (;;) 
    {
    ret = MsgWaitForMultipleObjects(1, &ReadyEvent, FALSE, INFINITE, QS_ALLINPUT);
    if( ret == (WAIT_OBJECT_0) ) //
      {
      if (LastPid != *pThisPid) 
        {
        LastPid = *pThisPid;
        sprintf(txt, "---[%3u]------------------------------\r\n", LastPid);
        AddToEdit(txt);
        }
      AddToEdit(buffer);
      SetEvent(AckEvent);
      //PostMessage(hMainWnd, WM_NULL, 0, 0);
      }
    else if( ret == (WAIT_OBJECT_0 + 1) )
      {
      while( PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) )
        {
        GetMessage(&msg, 0, 0, 0);
        if( msg.message == WM_QUIT )
          return 0;
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        }
      //FillToEdit();
      }
    }

  return 0;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
  LRESULT lRet = 0;

  switch ( msg ) {
    case WM_DESTROY:
      {
      char txt[256];

      GetPlacement(hWnd, txt);
      WritePrivateProfileString("Init", "Window", txt, "DBWin32.ini");
      KillTimer(hWnd, 1);
      PostQuitMessage(0);
      }
      break;
    case WM_CREATE:
      hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", 
          WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|ES_MULTILINE, 
          0, 0, 0, 0, hWnd, (HMENU)100, hInst, NULL);
      SetWindowFont(hEdit, GetStockFont(SYSTEM_FIXED_FONT), TRUE);
      SetTimer(hWnd, 1, 100, NULL);
      break;
    case WM_SIZE:
      MoveWindow(hEdit, 0, 0, LOWORD(lp), HIWORD(lp), TRUE);
      break;
    case WM_SETFOCUS:
      SetFocus(hEdit);
      break;
    case WM_TIMER:
      FillToEdit();
      break;
    case WM_COMMAND:
      switch( wp ) {
        case CM_EXIT:
          PostMessage(hWnd, WM_CLOSE, 0, 0);
          break;
        case CM_CLEAR:
          SetWindowText(hEdit, "");
          break;
        }
      break;
    default:
      lRet = DefWindowProc(hWnd, msg, wp, lp);
    }
  return lRet;
}

void GetPlacement(HWND hWnd, char *ach)
{
  WINDOWPLACEMENT wpl;

  memset(ach, 0, sizeof(ach));
  wpl.length = sizeof(wpl);
  GetWindowPlacement(hWnd, &wpl);
  wsprintf(ach, "%d %d %d %d %d %d %d %d %d %d %d ",
          10,
          wpl.flags,
          wpl.showCmd,
          wpl.ptMinPosition.x,
          wpl.ptMinPosition.y,
          wpl.ptMaxPosition.x,
          wpl.ptMaxPosition.y,
          wpl.rcNormalPosition.left,
          wpl.rcNormalPosition.top,
          wpl.rcNormalPosition.right,
          wpl.rcNormalPosition.bottom);
}

static int IntFromString(LPSTR * lplpsz)
{
  LPSTR lpsz = *lplpsz;
  int i = 0;
  char ch;
  BOOL fNeg;

  while (*lpsz == ' ')
    lpsz++;

  fNeg = FALSE;
  while ( (ch = *lpsz++) != 0 )
    {
    if (ch == '-')
      {
      fNeg = !fNeg;
      continue;
      }
    if (ch < '0' || ch > '9')
      break;
    i = (i * 10) + (ch - '0');
    }
  *lplpsz = lpsz;

  return (fNeg ? -i : i);
}

void SetPlacement(HWND hWnd, const char *s)
{
  WINDOWPLACEMENT wpl;
  char *ach = (char*)s;
  LPSTR lpsz;
  int cch = strlen(ach);

  lpsz = ach;
  if (cch == 0 || IntFromString(&lpsz) != 10)
    {
    // Default pozicija
    ::ShowWindow(hWnd, SW_SHOWNORMAL);
    }
  else
    {
    wpl.length = sizeof(wpl);
    wpl.flags = (UINT)IntFromString(&lpsz);
    wpl.flags &= ~WPF_SETMINPOSITION;
    wpl.showCmd = (UINT)IntFromString(&lpsz);
    wpl.ptMinPosition.x = IntFromString(&lpsz);
    wpl.ptMinPosition.y = IntFromString(&lpsz);
    wpl.ptMaxPosition.x = IntFromString(&lpsz);
    wpl.ptMaxPosition.y = IntFromString(&lpsz);
    wpl.rcNormalPosition.left = IntFromString(&lpsz);
    wpl.rcNormalPosition.top = IntFromString(&lpsz);
    wpl.rcNormalPosition.right = IntFromString(&lpsz);
    wpl.rcNormalPosition.bottom = IntFromString(&lpsz);

    if( GetWindowStyle(hWnd) & WS_CHILD )
      {
      wpl.showCmd = SW_SHOW;
      }
    SetWindowPlacement(hWnd, &wpl);
    }
}
... << RSDN@Home 1.1 beta 1 >>
The life is relative and reversible.
Re[2]: Перехват вывода OutputDebigString
От: A.Ryzhov Россия  
Дата: 27.08.03 10:08
Оценка:
Здравствуйте, Holms,

спасибо.
Best regards,
A.Ryzhov
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.