
АСУТП-кодер
  
Профиль
Группа: Комодератор
Сообщений: 1460
Регистрация: 5.3.2007
Где: Москва
Репутация: 16 Всего: 95
|
ИванМ, в продолжение совета Цитата(maxim1000 @ 20.12.2008, 23:06 ) | | смотреть перенаправление стандартного вывода в pipe |
накидал пример на коленке (не без огрехов, но лень извращаться, идея должна быть понятна): | Код | #include <windows.h>
/* Declare Windows procedure */ LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */ char szClassName[ ] = "CodeBlocksWindowsApp"; //char app_param[] = "ping 127.0.0.1 -t"; char app_param[] = "ping"; #define BUFFER_SIZE 1024 #define EDIT_BUFFER_SIZE 65535 #define bzero(a) memset(a,0,sizeof(a)) char buf[BUFFER_SIZE]; char ebuf[EDIT_BUFFER_SIZE]; unsigned long bread; //кол-во прочитанных байт unsigned long avail; //кол-во доступных байт
const int ID_MYBUTTON = 0x111; const int ID_MYEDIT = 0x112;
STARTUPINFO si; PROCESS_INFORMATION pi;
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) { HWND hwnd; /* This is the handle for our window */ MSG messages; /* Here messages to the application are saved */ WNDCLASSEX wincl; /* Data structure for the windowclass */ HWND hButton; HWND hEdit;
SECURITY_ATTRIBUTES sa; SECURITY_DESCRIPTOR sd; //структура security для пайпов
HANDLE newstdout,read_stdout; //дескрипторы // пайпов
InitializeSecurityDescriptor(&sd,SECURITY_DESCRIPTOR_REVISION); SetSecurityDescriptorDacl(&sd, true, NULL, false); sa.lpSecurityDescriptor = &sd;
sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.bInheritHandle = true; //разрешаем наследование дескрипторов if (!CreatePipe(&read_stdout,&newstdout,&sa,0)) return 0;
GetStartupInfo(&si); //создаем startupinfo для // дочернего процесса
si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; si.hStdOutput = newstdout; si.hStdError = newstdout; si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
/* The Window structure */ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */ wincl.style = CS_DBLCLKS; /* Catch double-clicks */ wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */ wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC_ARROW); wincl.lpszMenuName = NULL; /* No menu */ wincl.cbClsExtra = 0; /* No extra bytes after the window class */ wincl.cbWndExtra = 0; /* structure or the window instance */ /* Use Windows's default colour as the background of the window */ wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */ if (!RegisterClassEx (&wincl)) return 0;
/* The class is registered, let's create the program*/ hwnd = CreateWindowEx ( 0, /* Extended possibilites for variation */ szClassName, /* Classname */ "Code::Blocks Template Windows App", /* Title Text */ WS_OVERLAPPEDWINDOW, /* default window */ CW_USEDEFAULT, /* Windows decides the position */ CW_USEDEFAULT, /* where the window ends up on the screen */ 450, /* The programs width */ 440, /* and height in pixels */ HWND_DESKTOP, /* The window is a child-window to desktop */ NULL, /* No menu */ hThisInstance, /* Program Instance handler */ NULL /* No Window Creation data */ );
hButton = CreateWindowEx ( 0, /* Extended possibilites for variation */ "BUTTON", /* Classname */ "Test Button", /* Title Text */ WS_CHILD | WS_VISIBLE, /* default window */ 10, /* Windows decides the position */ 360, /* where the window ends up on the screen */ 430, /* The programs width */ 40, /* and height in pixels */ hwnd, /* The window is a child-window to desktop */ NULL, /* No menu */ hThisInstance, /* Program Instance handler */ NULL /* No Window Creation data */ ); SetWindowLong(hButton,GWL_ID,ID_MYBUTTON);
hEdit = CreateWindowEx ( 0, /* Extended possibilites for variation */ "EDIT", /* Classname */ "Test Edit", /* Title Text */ WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE |WS_VSCROLL, /* default window */ 10, /* Windows decides the position */ 10, /* where the window ends up on the screen */ 430, /* The programs width */ 350, /* and height in pixels */ hwnd, /* The window is a child-window to desktop */ NULL, /* No menu */ hThisInstance, /* Program Instance handler */ NULL /* No Window Creation data */ ); SetWindowLong(hEdit,GWL_ID,ID_MYEDIT);
/* Make the window visible on the screen */ ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */ while (GetMessage (&messages, NULL, 0, 0)) { PeekNamedPipe(read_stdout,buf,BUFFER_SIZE,&bread,&avail,NULL); //Проверяем, есть ли данные для чтения в stdout if (bread != 0) { bzero(buf); if (avail > BUFFER_SIZE-1) { while (bread >= BUFFER_SIZE -1) { ReadFile(read_stdout,buf,BUFFER_SIZE -1,&bread,NULL); //читаем из // пайпа stdout MessageBox(0,buf,"123",0); bzero(buf); } } else { bzero(ebuf); ReadFile(read_stdout,buf,BUFFER_SIZE-1,&bread,NULL); SendMessage(hEdit,WM_GETTEXT,EDIT_BUFFER_SIZE,(LPARAM)ebuf); strcat(ebuf,buf); SendMessage(hEdit,WM_SETTEXT,0,(LPARAM)ebuf); } }
/* Translate virtual-key messages into character messages */ TranslateMessage(&messages); /* Send message to WindowProcedure */ DispatchMessage(&messages); }
CloseHandle(newstdout); CloseHandle(read_stdout); /* The program return-value is 0 - The value that PostQuitMessage() gave */ return messages.wParam; }
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) /* handle the messages */ { case WM_COMMAND: if ((LOWORD(wParam)==ID_MYBUTTON)&&(HIWORD(wParam)==BN_CLICKED)) {
//создаем дочерний процесс SendDlgItemMessage(hwnd,ID_MYEDIT, WM_SETTEXT,0,(LPARAM)&("")); if (!CreateProcess(NULL,app_param,NULL,NULL,TRUE,CREATE_NEW_CONSOLE, NULL,NULL,&si,&pi)) { MessageBox(0,"Failed to start process","Error",0); return 0; } Sleep(200); // Чтобы успело начать отрабатывать SendMessage(hwnd,WM_PAINT,0,0); // Для того, чтобы обновился цикл GetMessage } break; case WM_DESTROY: PostQuitMessage (0); /* send a WM_QUIT to the message queue */ break; default: /* for messages that we don't deal with */ return DefWindowProc (hwnd, message, wParam, lParam); }
return 0; } |
--------------------
самурай без меча подобен самураю с мечом, но только без меча
|