69,371
社区成员
发帖
与我相关
我的任务
分享
#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();
}