Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Windows API:WM_COPYDATA无限循环_C++_Visual Studio_Api_Winapi - Fatal编程技术网

C++ Windows API:WM_COPYDATA无限循环

C++ Windows API:WM_COPYDATA无限循环,c++,visual-studio,api,winapi,C++,Visual Studio,Api,Winapi,我有一个学校的项目,我得到了一个错误,我不知道如何修复。 我目前正在使用Visual Studio 2015,并使用控制台应用程序。 我的项目是:我需要制作一个应用程序,其中一个进程向另一个进程发送文本文件的内容,一行接一行。在5到5秒内发送一行。接收者将获得该行35秒,并将其写入一个文件中,然后接收者将向发送者发送一条消息,说明该行已收到。 我曾尝试使用PCOPYDATASTRUCT和WM_COPYDATA创建这个项目,并且我使用fgets和fput从文件中读写。 我的问题是它进入了一个无限循

我有一个学校的项目,我得到了一个错误,我不知道如何修复。 我目前正在使用Visual Studio 2015,并使用控制台应用程序。 我的项目是:我需要制作一个应用程序,其中一个进程向另一个进程发送文本文件的内容,一行接一行。在5到5秒内发送一行。接收者将获得该行35秒,并将其写入一个文件中,然后接收者将向发送者发送一条消息,说明该行已收到。 我曾尝试使用PCOPYDATASTRUCT和WM_COPYDATA创建这个项目,并且我使用fgets和fput从文件中读写。 我的问题是它进入了一个无限循环,在几秒钟内创建了一个1gb的文本文件,并且只复制了第一行。我试着尽我所能解决问题,但什么都没有改变。 希望你能帮助我,谢谢。 以下是发送应用程序:

    #undef UNICODE
#pragma warning(disable:4996)
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <windows.h>
using namespace std;
#define MAXLEN 100

const int ID_BUTTON = 1;
const int ID_EDIT = 2;
FILE *pFile;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR szCmdLine, int iCmdShow)
{
    static char szAppName[] = "Send";
    HWND        hwnd;
    MSG         msg;
    WNDCLASSEX  wndclass; // obiectul instantiat din structura WNDCLASSEX

    wndclass.cbSize = sizeof(wndclass);//Dimensiunea structurii
    wndclass.style = CS_HREDRAW | CS_VREDRAW; //Stilul ferestrei
    wndclass.lpfnWndProc = WndProc; // Procedura de fereastra care trateaza mesajele
                                    // Doua câmpuri care permit rezervarea de spatiu suplimentar in structura class, respectiv structura window.
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance; //Identificatorul instantei ferestrei
                                    //Stabileste o pictograma pentru toate ferestrele create pe baza clasei window
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    //Stabileste o pictograma pentru cursor
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    //Specifica culoarea de fundal a ferestrei
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL; // Specifica meniul feresterei
    wndclass.lpszClassName = szAppName; // Specifica numele feresterei
    wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    //Inregistrarea clasei de fereastra
    RegisterClassEx(&wndclass);

    // Crearea ferestrei pe baza clasei de fereastra
    hwnd = CreateWindow(szAppName,         // window class name
        "Aplicatia Send",     // window caption
        WS_OVERLAPPEDWINDOW,     // window style
        100,           // initial x position
        100,           // initial y position
        1200,           // initial x size
        800,           // initial y size
        NULL,                    // parent window handle
        NULL,                    // window menu handle
        hInstance,               // program instance handle
        NULL);                   // creation parameters

    ShowWindow(hwnd, iCmdShow); // afiseaza fereastra pe ecran
                                //transmite catre WndProc un mesaj de tip WM_PAINT, care are ca rezultat
                                //redesenarea ferestrei, adica reactualizarea zonei client a ferestrei
    UpdateWindow(hwnd); // 

                        // preia si distribuie mesaje pana se primeste WM_QUIT
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg); // traduce mesajul
        DispatchMessage(&msg); // transmite mesajul catre procedura de fereastra
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{

    HDC hdc; //contextul grafic
    PAINTSTRUCT ps; // o variabila la structura de desenare
    RECT rect; //obiect dreptunghi
    TEXTMETRIC tm;
    HWND hwndReceive;
    static int cxChar, cyChar;
    static HWND hEdit, hButtonOk;
    COPYDATASTRUCT cs;
    char mystring[100];
    SYSTEMTIME LocalTime;
    hwndReceive = FindWindow("Recieve", "RecieveMessage");

    switch (iMsg)
    {
    case WM_CREATE: //operatii ce se executa la crearea ferestrei
    {
        pFile = fopen("fis.txt", "r");
        if (pFile == NULL)

        {
            MessageBox(hwnd, "Nu am gasit fisierul sursa!", "Eroare SEND!", MB_OK);
            return 0;
        }

        hdc = GetDC(hwnd);
        SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
        GetTextMetrics(hdc, &tm);
        cxChar = tm.tmAveCharWidth;
        cyChar = tm.tmHeight + tm.tmExternalLeading;
        ReleaseDC(hwnd, hdc);
        hButtonOk = CreateWindow("button", "Trimite date", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
            10 * cxChar, cyChar * 8, 50 * cxChar, 2 * cyChar,
            hwnd, (HMENU)ID_BUTTON, ((LPCREATESTRUCT)lParam)->hInstance, NULL);

        hEdit = CreateWindow("edit", "", WS_CHILD | WS_VISIBLE | ES_READONLY | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_LEFT | WS_BORDER,
            40 * cxChar, cyChar * 25, 60 * cxChar, 10 * cyChar,
            hwnd, (HMENU)ID_EDIT, ((LPCREATESTRUCT)lParam)->hInstance, NULL);

    }
    return 0;

    case WM_COMMAND:
    {

        if (LOWORD(wParam) == ID_BUTTON)
        {

            if (hwndReceive != NULL)
            {

                GetLocalTime(&LocalTime);
                printf("Timpul sistem este: %02d:%02d:%02d\n", LocalTime.wHour, LocalTime.wMinute, LocalTime.wSecond);

                int TimpPornire = LocalTime.wHour * 3600 + LocalTime.wMinute * 60 + LocalTime.wSecond;

                GetLocalTime(&LocalTime);
                while ((fgets(mystring, 100, pFile) != NULL))
                {

                    cs.cbData = strlen(mystring);
                    cs.lpData = mystring;
                    SendMessage(hwndReceive, WM_COPYDATA, (WPARAM)hwnd, (LPARAM)&cs);
                    SetDlgItemText(hwnd, ID_EDIT, "Se trimit datele...");
                    Sleep(5000);
                    GetLocalTime(&LocalTime);
                    int TimpCurent = LocalTime.wHour * 3600 + LocalTime.wMinute * 60 + LocalTime.wSecond;
                    if (TimpCurent - TimpPornire >= 35)
                    {
                        SetDlgItemText(hwnd, ID_EDIT, "Au trecut 35 de secunde!");
                        break;
                    }

                }

                fclose(pFile);
                CloseHandle(hwnd);

            }

            else

            {
                MessageBox(hwnd, "Nu am gasit procesul fiu(Recieve)!", "Eroare SEND!", MB_OK);
                return 0;
            }


        }

    }
        return 0;



    case WM_DESTROY://operatii ce se executa la distrugerea ferestrei
        PostQuitMessage(0); // insereaza un mesaj de incheiere in coada de mesaje
        return 0;
    }
    // Alte mesaje sunt trimise catre functia implicita de tratare
    return DefWindowProc(hwnd, iMsg, wParam, lParam);
    }
#未定义UNICODE
#杂注警告(禁用:4996)
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
#定义MAXLEN 100
const int ID_按钮=1;
const int ID_EDIT=2;
文件*pFile;
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
int WINAPI WinMain(HINSTANCE HINSTANCE、HINSTANCE HPPreInstance、,
PSTR szCmdLine,int iCmdShow)
{
静态字符szAppName[]=“发送”;
HWND-HWND;
味精;
WNDCLASSEX wndclass;//对象实例化din structura WNDCLASSEX
wndclass.cbSize=sizeof(wndclass);//维度结构
wndclass.style=CS|HREDRAW | CS|u VREDRAW;//Still ferestrei
wndclass.lpfnWndProc=WndProc;//
//Doua c–mpuri护理许可证位于structura类,分别位于structura窗口。
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;//Identificatorul instantei ferestrei
//Stabileste o pictograma pentru toate ferestrele创建pe baza clasei窗口
wndclass.hIcon=LoadIcon(空,IDI_应用程序);
//稳定的pentru光标象形图
wndclass.hCursor=LoadCursor(空,IDC_箭头);
//费尔斯特里基金会特别会议室
wndclass.hbrBackground=(HBRUSH)GetStockObject(白色画笔);
wndclass.lpszMenuName=NULL;//specifia meniul feresterei
wndclass.lpszClassName=szAppName;//Specifica numele feresterei
wndclass.hIconSm=LoadIcon(空,IDI_应用程序);
//在法国
RegisterClassEx(&wndclass);
//弗雷斯特雷酒店
hwnd=CreateWindow(szAppName,//窗口类名称
“Aplicia Send”,//窗口标题
WS\u重叠窗口,//窗口样式
100,//初始x位置
100,//初始y位置
1200,//初始x尺寸
800,//初始y尺寸
NULL,//父窗口句柄
NULL,//窗口菜单句柄
hInstance,//程序实例句柄
NULL);//创建参数
ShowWindow(hwnd,iCmdShow);//afiseaza fereastra pe ecran
//将油漆、涂料和其他材料输送至现场
//redesenarea ferestrei,adica reactualizarea zonei客户a ferestrei
更新窗口(hwnd);//
//首先,我要分发一份最重要的工作
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);//traduce mesajul
DispatchMessage(&msg);//发送mesajul catre程序
}
返回msg.wParam;
}
LRESULT回调WndProc(HWND HWND、UINT iMsg、WPARAM WPARAM、LPARAM LPARAM)
{
HDC HDC;//上下文图形
PAINTSTRUCT ps;//设计结构的变化
RECT RECT;//对象dreptunghi
textmetricTM;
HWND-hwnreceive;
静态int-cxChar,cyChar;
静态HWND hEdit,hButtonOk;
复制数据结构;
char mystring[100];
系统时间本地时间;
hwnreceive=FindWindow(“接收”、“接收消息”);
开关(iMsg)
{
案例WM_CREATE://operatii ce se executa la Creara ferestrei
{
pFile=fopen(“fis.txt”,“r”);
if(pFile==NULL)
{
消息框(hwnd,“Nu am gasit fisierul sursa!”,“Eroare SEND!”,MB_OK);
返回0;
}
hdc=GetDC(hwnd);
选择对象(hdc、GetStockObject(系统固定字体));
GetTextMetrics(hdc和tm);
cxChar=tm.tmAveCharWidth;
cyChar=tm.tmHeight+tm.tmexternalreading;
释放DC(hwnd、hdc);
hButtonOk=CreateWindow(“按钮”、“Trimite日期”、WS|u CHILD | WS|u VISIBLE | BS|u def按钮、,
10*cxChar,cyChar*8,50*cxChar,2*cyChar,
hwnd,(humenu)ID_按钮,((LPCREATESTRUCT)lpram)->hInstance,NULL);
hEdit=CreateWindow(“编辑”),WS|u CHILD | WS|u VISIBLE | ES|u READONLY | WS|u VSCROLL | ES|u AUTOVSCROLL | ES|u LEFT | WS|u BORDER,
40*cxChar,cyChar*25,60*cxChar,10*cyChar,
hwnd,(humenu)ID_EDIT,((LPCREATESTRUCT)lParam)->hInstance,NULL);
}
返回0;
case WM_命令:
{
如果(LOWORD(wParam)==ID_按钮)
{
如果(hwnreceive!=NULL)
{
GetLocalTime(&LocalTime);
printf(“Timpul系统este:%02d:%02d:%02d\n”,LocalTime.wHour,LocalTime.wMinute,LocalTime.wssecond);
int timpbornire=LocalTime.wHour*3600+LocalTime.wMinute*60+LocalTime.wssecond;
GetLocalTime(&LocalTime);
while((fgets(mystring,100,pFile)!=NULL))
{
cs.cbData=strlen(mystring);
cs.lpData=mystring;
发送消息(HwnReceive、WM_COPYDATA、(WPARAM)hwnd、(LPRAM)和cs);
 #undef UNICODE
#pragma warning(disable:4996)
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <windows.h>
using namespace std;
#define MAXLEN 100


LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR szCmdLine, int iCmdShow)
{
    static char szAppName[] = "Recieve";
    HWND        hwnd;
    MSG         msg;
    WNDCLASSEX  wndclass; // obiectul instantiat din structura WNDCLASSEX

    wndclass.cbSize = sizeof(wndclass);//Dimensiunea structurii
    wndclass.style = CS_HREDRAW | CS_VREDRAW; //Stilul ferestrei
    wndclass.lpfnWndProc = WndProc; // Procedura de fereastra care trateaza mesajele
                                    // Doua câmpuri care permit rezervarea de spatiu suplimentar in structura class, respectiv structura window.
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance; //Identificatorul instantei ferestrei
                                    //Stabileste o pictograma pentru toate ferestrele create pe baza clasei window
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    //Stabileste o pictograma pentru cursor
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    //Specifica culoarea de fundal a ferestrei
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL; // Specifica meniul feresterei
    wndclass.lpszClassName = szAppName; // Specifica numele feresterei
    wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    //Inregistrarea clasei de fereastra
    RegisterClassEx(&wndclass);

    // Crearea ferestrei pe baza clasei de fereastra
    hwnd = CreateWindow(szAppName,         // window class name
        "RecieveMessage",     // window caption
        WS_OVERLAPPEDWINDOW,     // window style
        CW_USEDEFAULT,           // initial x position
        CW_USEDEFAULT,           // initial y position
        CW_USEDEFAULT,           // initial x size
        CW_USEDEFAULT,           // initial y size
        NULL,                    // parent window handle
        NULL,                    // window menu handle
        hInstance,               // program instance handle
        NULL);                   // creation parameters

    ShowWindow(hwnd, iCmdShow); // afiseaza fereastra pe ecran
                                //transmite catre WndProc un mesaj de tip WM_PAINT, care are ca rezultat
                                //redesenarea ferestrei, adica reactualizarea zonei client a ferestrei
    UpdateWindow(hwnd); // 

                        // preia si distribuie mesaje pana se primeste WM_QUIT
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg); // traduce mesajul
        DispatchMessage(&msg); // transmite mesajul catre procedura de fereastra
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{

    HDC hdc; //contextul grafic
    PAINTSTRUCT ps; // o variabila la structura de desenare
    RECT rect; //obiect dreptunghi
    static char szBufferPaint[MAXLEN] = "Astept datele...", szBufferReceive[MAXLEN];
    PCOPYDATASTRUCT pcs;
    FILE *rFile;
    pcs = (PCOPYDATASTRUCT)lParam;

    switch (iMsg)
    {
    case WM_CREATE: //operatii ce se executa la crearea ferestrei

        return 0;

    case WM_PAINT://operatii ce se executa la desenarea ferestrei
        hdc = BeginPaint(hwnd, &ps); //Obtinerea contextului grafic
        GetClientRect(hwnd, &rect); //Obtinerea suprafeţei de desenare
                                    //Scrierea unui text în fereastră
        DrawText(hdc, szBufferPaint, -1, &rect,
            DT_SINGLELINE | DT_CENTER | DT_VCENTER);
        EndPaint(hwnd, &ps); //Eliberarea contextului grafic
        return 0;

    case WM_COPYDATA:
    {

        rFile = fopen("test.txt", "w");
                pcs = (PCOPYDATASTRUCT)lParam;
                strncpy_s(szBufferReceive, (char*)pcs->lpData, pcs->cbData);
                fputs(szBufferReceive, rFile);
                break;
            CloseHandle(hwnd);
            fclose(rFile);
    }
        return 0;

    case WM_DESTROY://operatii ce se executa la distrugerea ferestrei
        PostQuitMessage(0); // insereaza un mesaj de incheiere in coada de mesaj
        return 0;
    }
    // Alte mesaje sunt trimise catre functia implicita de tratare
    return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
diff --git a/sender.c b/sender.c
index a0a29a3..b5caf62 100644
--- a/sender.c
+++ b/sender.c
@@ -3,9 +3,7 @@
 #include<stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <iostream>
 #include <windows.h>
-using namespace std;
 #define MAXLEN 100

 const int ID_BUTTON = 1;
@@ -128,6 +126,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
                 int TimpPornire = LocalTime.wHour * 3600 + LocalTime.wMinute * 60 + LocalTime.wSecond;

                 GetLocalTime(&LocalTime);
+
+                fseek( pFile, 0, SEEK_SET );
                 while ((fgets(mystring, 100, pFile) != NULL))
                 {

@@ -146,7 +146,6 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)

                 }

-                fclose(pFile);
                 CloseHandle(hwnd);

             }
@@ -167,6 +166,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)


     case WM_DESTROY://operatii ce se executa la distrugerea ferestrei
+        fclose(pFile);
         PostQuitMessage(0); // insereaza un mesaj de incheiere in coada de mesaje
         return 0;
     }
diff --git a/receiver.c b/receiver.c
index 74434ba..f068661 100644
--- a/receiver.c
+++ b/receiver.c
@@ -3,9 +3,7 @@
 #include<stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <iostream>
 #include <windows.h>
-using namespace std;
 #define MAXLEN 100


@@ -95,13 +93,12 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
     case WM_COPYDATA:
     {

-        rFile = fopen("test.txt", "w");
-                pcs = (PCOPYDATASTRUCT)lParam;
-                strncpy_s(szBufferReceive, (char*)pcs->lpData, pcs->cbData);
-                fputs(szBufferReceive, rFile);
-                break;
-            CloseHandle(hwnd);
-            fclose(rFile);
+        rFile = fopen("test.txt", "a+");
+        pcs = (PCOPYDATASTRUCT)lParam;
+        fputs((char*)pcs->lpData, rFile);
+        fclose(rFile);
+        CloseHandle(hwnd);
+        break;
     }
         return 0;