高手请进!高手请进 !!救命~ 关于匿名管道连接窗口程序!!!

朱韦刚
博客专家认证
2011-09-01 10:46:49
我想做一个窗口 ,其间有个edit 控件 通过匿名管道 把底层数据实时的刷新出来 显示在控件上 ,匿名管道的两个控制台应用程序的简单连接稍微懂点,我把程序贴一下,请教高手怎么样才能连接在一起!!!

#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>

#define IDE_1 100

HWND hedit;
HINSTANCE hinst;

LRESULT CALLBACK Wndproc(HWND ,UINT ,WPARAM ,LPARAM); //窗口函数说明

void Receive()
{
if (OpenClipboard(NULL))
{
if (IsClipboardFormatAvailable(CF_TEXT))//判断剪贴板中的数据是否是文本
{
HANDLE hClip;
char *pBuf = NULL;
hClip = GetClipboardData(CF_TEXT); //
pBuf = (char *)GlobalLock(hClip);
GlobalUnlock(hClip);
MessageBox(NULL,pBuf,NULL,MB_OK); //显示出来
}
CloseClipboard();
}
}

LRESULT CALLBACK Wndproc(HWND hwnd,UINT message ,WPARAM wparam,LPARAM lparam)
{
switch(message)
{
case WM_CREATE:
hedit = CreateWindow("EDIT",NULL,WS_CHILD| WS_VISIBLE |ES_LEFT | WS_BORDER,
130,20,300,30,hwnd,(HMENU)IDE_1,hinst ,NULL);
break;
case WM_COMMAND:
switch(LOWORD(wparam))
{
case IDE_1:
if (HIWORD(wparam) == EN_SETFOCUS)
{
////////////////////////////////////////////////////////

//此处添加 代码


////////////////////////////////////////////////////////
//SetFocus(hedit);
Receive();

}
break;
}
break;
case WM_DESTROY: PostQuitMessage(0);
default: //缺省时采用系统消息缺省处理函数
return DefWindowProc(hwnd,message,wparam,lparam);
}
return 0;
}

int WINAPI WinMain(HINSTANCE hlnstance ,HINSTANCE hprevlnst ,LPSTR lpszcomdine ,int nCmdShow)
{
HWND hwnd ; //窗口句柄
MSG msg ; //消息
WNDCLASS wndclass ; //窗口对象
char lpszclassname[] = "窗口"; //窗口类名
char lpsztitle[] = "MY_Windows"; //窗口标题名

//窗口类定义
wndclass.style = 0; //窗口类型为缺省类型
wndclass.lpfnWndProc =Wndproc; //定义窗口处理函数
wndclass.cbClsExtra = 0; //窗口类无扩展
wndclass.cbWndExtra = 0; //窗口实例无扩展
wndclass.hInstance = hlnstance ; // 当前实例句柄
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); //窗口的最小图标位缺省图标
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW); //窗口采用箭头光标
wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH) ; //窗口背景为白色
wndclass.lpszMenuName = NULL ; //窗口中无菜单
wndclass.lpszClassName = lpszclassname ; //窗口类名为“窗口”

//窗口类注册
if (!RegisterClass(&wndclass))
{
MessageBeep(0);
return FALSE;
}

//创建窗口
hwnd = CreateWindow(lpszclassname,lpsztitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hlnstance,NULL);

//显示窗口
ShowWindow(hwnd,nCmdShow);

//绘制用户区
UpdateWindow(hwnd);

//消息循环
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam ; //消息循环结束即程序终止时将信息返回系统
}



下面是两个控制台程序匿名管道的简单连接

#include "stdio.h"
#include "windows.h"
#include "process.h"
#include "tchar.h"

const int BUF_SIZE=1000;

int main()
{
HANDLE PipeReadHandle;
HANDLE PipeWriteHandle;
PROCESS_INFORMATION ProcessInfo;
SECURITY_ATTRIBUTES SecurityAttributes;
STARTUPINFO StartupInfo;
BOOL Success;

//--------------------------------------------------------------------------
// Zero the structures.
//--------------------------------------------------------------------------
ZeroMemory( &StartupInfo, sizeof( StartupInfo ));
ZeroMemory( &ProcessInfo, sizeof( ProcessInfo ));
ZeroMemory( &SecurityAttributes, sizeof( SecurityAttributes ));

//--------------------------------------------------------------------------
// Create a pipe for the child's STDOUT.
//--------------------------------------------------------------------------
SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
SecurityAttributes.bInheritHandle = TRUE;
SecurityAttributes.lpSecurityDescriptor = NULL;

Success = CreatePipe
(
&PipeReadHandle, // address of variable for read handle
&PipeWriteHandle, // address of variable for write handle
&SecurityAttributes, // pointer to security attributes
0 // number of bytes reserved for pipe (use default size)
);

if ( !Success )
{
// ShowLastError(_T("Error creating pipe"));
printf("error creating pipe\n");
}

//--------------------------------------------------------------------------
// Set up members of STARTUPINFO structure.
//--------------------------------------------------------------------------
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
StartupInfo.wShowWindow = SW_HIDE;
StartupInfo.hStdOutput = PipeWriteHandle;
StartupInfo.hStdError = PipeWriteHandle;

//----------------------------------------------------------------------------
// Create the child process.
//----------------------------------------------------------------------------
Success = CreateProcess
(
NULL, // pointer to name of executable module
"C:\\Users\\zhu\\Desktop\\匿名管道简单通信\\Debug\\son.exe", // command line
NULL, // pointer to process security attributes
NULL, // pointer to thread security attributes (use primary thread security attributes)
TRUE, // inherit handles
0, // creation flags
NULL, // pointer to new environment block (use parent's)
NULL, // pointer to current directory name
&StartupInfo, // pointer to STARTUPINFO
&ProcessInfo // pointer to PROCESS_INFORMATION
);

if ( !Success )
{
printf("Error creating process\n");
}

DWORD BytesLeftThisMessage = 0;
DWORD NumBytesRead;
TCHAR PipeData[BUF_SIZE];
DWORD TotalBytesAvailable = 0;

Sleep(200);
Success=ReadFile
(
PipeReadHandle, // handle to pipe to copy from
PipeData, // address of buffer that receives data
BUF_SIZE-1, // number of bytes to read
&NumBytesRead, // address of number of bytes read
NULL // address of structure for data for overlapped I/O
);
while (Success)
{

PipeData[NumBytesRead] = '\0';
printf("%s \nnum:%d\n",PipeData,NumBytesRead);

//判断子进程是否已结束
if ( WaitForSingleObject(ProcessInfo.hProcess, 0) == WAIT_OBJECT_0)
{
printf("sunProcess is end\n");
break;
}
Sleep(100);
Success=ReadFile
(
PipeReadHandle, // handle to pipe to copy from
PipeData, // address of buffer that receives data
BUF_SIZE-1, // number of bytes to read
&NumBytesRead, // address of number of bytes read
NULL // address of structure for data for overlapped I/O
);

}
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(PipeReadHandle);
CloseHandle(PipeWriteHandle);
printf("end\n");

return getchar();
}





#include <stdio.h>

int main()
{
for(int i = 0; i < 10; i++)
{
printf("StdOutput: %d \n",i);
}
return getchar();
}



高手帮忙研究下~!!!!!
...全文
83 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
朱韦刚 2011-09-01
  • 打赏
  • 举报
回复
简单点,一个窗口程序写用户界面,一个控制台应用程序,在窗口程序的edit控件中,刷新显示控制台程序中的数据 ,例如 int i = 0 ; i<100 ; i++;printf("%d",i); 控件中显示 i 的值!!
朱韦刚 2011-09-01
  • 打赏
  • 举报
回复
我这种思路不能实现吗??
  • 打赏
  • 举报
回复
不明白你为啥要两个控制台程序,匿名管道相当于一个管子,主要用于父子进程间的通信,所以你在窗口程序中创建匿名管道并创建个子进程就可以了,类似你下面的两个控制台程序。
朱韦刚 2011-09-01
  • 打赏
  • 举报
回复
高手帮忙研究下!!谢谢了!!

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧