使用管道实现重定向
VC++6.0
报错:
error C2143: syntax error : missing ';' before '__stdcall'
error C2501: 'LRESULF' : missing storage-class or type specifiers
这两个错误看不懂
error C2440: '=' : cannot convert from 'int (stdcall *)(struct HWND *,unsigned int,unsigned int,long)' to 'long (stdcall *)(struct HWND *,unsigned int,unsigned int,long)'
wndClass.lpfnWndProc=myWndProc;赋值错误 long与int型转换,我看我们书上就是这么写的啊
error C2086: 'LRESULF' : redefinition
LRESULF重复定义,可是LRESULF不是一个数据类型吗
#include "stdafx.h"
#include <windows.h>
#define BUF_SIZE 1000
TCHAR PipeData[BUF_SIZE]="\0";
LRESULF CALLBACK myWndProc(HWND hWnd,UINT uMsgId,WPARAM wParam,LPARAM lParam);
void AppendText(HWND hwnd);
void OnPing(HWND hwnd);
void PeekAndPump();
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPreInst,LPSTR pszCmdLine,int nCmdShow)
{
static char szAppName[]="输出重定向!";
WNDCLASS wndClass;
HWND hWnd;
MSG msg;
wndClass.style=CS_VREDRAW|CS_HREDRAW;
wndClass.lpfnWndProc=myWndProc;
wndClass.cbClsExtra=0;
wndClass.cbWndExtra=0;
wndClass.hInstance=hInst;
wndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndClass.hCursor=LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName=NULL;
wndClass.lpszClassName=szAppName;
if(0==RegisterClass(&wndClass))
return 0;
hWnd=CreateWindow(
szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInst,
NULL
);
if(hWnd==0) return 0;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULF CALLBACK myWndProc(HWND hWnd,UINT uMsgId,WPARAM wParam,LPARAM lParam)
{
switch(uMsgId)
{
case WM_CREATE:
return 0;
case WM_PAINT:
AppendText(hWnd);
return 0;
case WM_CHAR:
if(wParam=='s' || wParam=='S')
OnPing(hWnd);
case WM_CLOSE:
DestroyWindow(hWnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd,uMsgId,wParam,lParam);
}
}
void AppendText(HWND hwnd)
{
static TCHAR Data[10*BUF_SIZE]="\0";
HDC hDc;
PAINTSTRUCT paintStruct;
RECT clientRect;
strcat(Data,PipeData);
PipeData[0]='\0';
hDc=BeginPaint(hwnd,&paintStruct);
if(hDc!=NULL)
{
TextOut(hDc,0,0,"Press s or S to start ping",25);
GetClientRect(hwnd,&clientRect);
DrawText(hDc,Data,-1,&clientRect,DT_LEFT|DT_VCENTER);
EndPaint(hwnd,&paintStruct);
}
}
void OnPing(HWND hwnd)
{
LPCTSTR szCommand="ping.exe 127.0.0.1";
LPCTSTR szCurrentDirectory="c:\\windows\\system32\\";
DWORD BytesLeftThisMessage=0;
DWORD NumBytesRead;
DWORD TotalBytesAvailable=0;
HANDLE PipeReadHandle;
HANDLE PipeWriteHandle;
PROCESS_INFORMATION ProcessInfo;
SECURITY_ATTRIBUTES SecurityAttributes;
STARTUPINFO StartupInfo;
BOOL Success;
STARTUPINFO Startupinfo;
ZeroMemory(&StartupInfo,sizeof(StartupInfo));
ZeroMemory(&ProcessInfo,sizeof(ProcessInfo));
ZeroMemory(&SecurityAttributes,sizeof(SecurityAttributes));
SecurityAttributes.nLength=sizeof(SECURITY_ATTRIBUTES);
SecurityAttributes.bInheritHandle=TRUE;
SecurityAttributes.lpSecurityDescriptor=NULL;
Success=CreatePipe(&PipeReadHandle,&PipeWriteHandle,&SecurityAttributes,0);
if(!Success) return;
StartupInfo.cb=sizeof(STARTUPINFO);
StartupInfo.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
StartupInfo.wShowWindow=SW_HIDE;
StartupInfo.hStdOutput=PipeWriteHandle;
StartupInfo.hStdError=PipeWriteHandle;
Success=CreateProcess(NULL,(LPTSTR)szCommand, NULL, NULL, TRUE,0,NULL,szCurrentDirectory,&StartupInfo,&ProcessInfo);
if(!Success) return;
for(;;)
{
NumBytesRead=0;
Success=PeekNamedPipe(PipeReadHandle,PipeData,1,&NumBytesRead,&TotalBytesAvailable,&BytesLeftThisMessage);
if(!Success) break;
if(NumBytesRead)
{
Success=ReadFile(PipeReadHandle,PipeData,BUF_SIZE-1,&NumBytesRead,NULL);
if(!Success) break;
PipeData[NumBytesRead]='\0';
InvalidateRect(hwnd,NULL, TRUE);
PeekAndPump();
}
else
{
if(WaitForSingleObject(ProcessInfo.hProcess,0)==WAIT_OBJECT_0) break;
PeekAndPump();
Sleep(100);
}
}
CloseHandle (ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(PipeReadHandle);
CloseHandle(PipeWriteHandle);
}
void PeekAndPump()
{
MSG Msg;
while(PeekMessage(&Msg,NULL,0,0,PM_NOREMOVE))
if(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}