qq_51371177 2022-01-05 15:27 采纳率: 0%
浏览 35
已结题

error C2086: 'LRESULF' : redefinition

使用管道实现重定向
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);
        }
}

  • 写回答

1条回答 默认 最新

  • qq_51371177 2022-01-05 15:39
    关注

    img


    这是所有报错信息,这个代码全是书上抄的,为什么会报错呢

    评论

报告相同问题?

问题事件

  • 系统已结题 1月13日
  • 创建了问题 1月5日

悬赏问题

  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题