1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #define _CRT_SECURE_NO_WARNINGS
- #include <Windows.h>
- #include <stdio.h>
- #include "QuadEq.h"
- #define FILE_OUT "C:\\Users\\òàðàíàà\\source\\repos\\TaskSystem\\x64\\Debug\\result.txt"
- QuadraticEquation* readFile(char* nameFile)
- {
- QuadraticEquation* equation = malloc(sizeof(QuadraticEquation));
- FILE* file = fopen(nameFile, "r");
- if (file == NULL)
- {
- printf("Ôàéëà ñ èõîäíûìè äàííûìè íå ñóùåñòâóåò. Ñîçäàéòå ôàéë %s \n", nameFile);
- exit(ERROR_OPEN_FILE);
- }
- int resultRead = fscanf(file, "%lf %lf %lf", &equation->a, &equation->b, &equation->c);
- if (resultRead == 0)
- {
- printf("Íå óäàëîñü êîððåêòíî ïðî÷èòàòü ôàéë.\n");
- exit(ERROR_READ_DATA);
- }
- if (equation->a == 0 && equation->b == 0 && equation->c == 0)
- {
- printf("Âñå êîýôèöèåíòû ðàâíû 0. Ïðîâåðüòå èñõîäíûé ôàéë.\n");
- exit(ERROR_READ_DATA);
- }
- if (equation->a == 0)
- {
- printf("Êîýôôèöèåíò à ðàâåí 0. Ýòî íå êâàäðàòíîå óðàâíåíèå.\n");
- exit(ERROR_READ_DATA);
- }
- fclose(file);
- return equation;
- }
- int startWorkWithProcess()
- {
- QuadraticEquation* myEq = readFile(FILE_IN);
- LPSTR inputKoef = calloc(BUFFER, sizeof(char));
- sprintf(inputKoef, "%lf %lf %lf\0", myEq->a, myEq->b, myEq->c);
- /*
- * Äàííûå äëÿ ïåðåäà÷è â äî÷åðíèé ïðîöåññ:
- * childName - èìÿ äî÷åðíåãî èñïîëÿåìîãî ôàéëà
- * commandLine - àðãóìåíòû êîìàíäíîé ñòðîêè äëÿ ïåðåäà÷è â íîâûé ïðîöåññ
- * si - áëîê íà÷àëüíûõ ïàðàìåòðîâ
- * pi - ñòðóêòóðà, îïèñûâàþùàÿ ïîðîæäåííûé ïðîöåññ
- */
- LPCSTR childName = "C:\\Users\\òàðàíàà\\source\\repos\\TaskSystem\\x64\\Debug\\ChildProcess.exe";
- //LPCSTR childName = "ChildProcess.exe";
- LPSTR commandLine = inputKoef;
- STARTUPINFO si;
- /*Çàïîëíåíèå äàííûõ ñòðóêòóðû ñòàíäàðòíûìè äàííûìè*/
- ZeroMemory(&si, sizeof(si));
- si.cb = sizeof(si);
- PROCESS_INFORMATION pi;
- ZeroMemory(&pi, sizeof(pi));
- //TRUE / FALSE îòâå÷àåò çà íàñëåäîâàíèå äåñêðèïòîðîâ.  äàííîì ñëó÷àå ðàáîòàåò è áåç ýòîãî.
- if (!CreateProcessA(childName, commandLine, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi))
- {
- printf("Íå óäàëîñü ñîçäàòü ïðîöåññ. Êîä îøèáêè: %d.\n", GetLastError());
- }
- // Îæèäàíèå çàâåðøåíèÿ äî÷åðíåãî ïðîöåññà
- WaitForSingleObject(pi.hProcess, INFINITE);
- DWORD childexitcode;
- GetExitCodeProcess(pi.hProcess, &childexitcode);
- CloseHandle(pi.hProcess);
- CloseHandle(pi.hThread);
- printf("Êîä çàâåðøåíèÿ äî÷åðíåãî ïðîöåññà %d\n", childexitcode);
- if (childexitcode >= 0 && childexitcode <= 2)
- {
- FILE* resFile = fopen(FILE_OUT, "r");
- if (resFile == NULL)
- {
- printf("×òî-òî ïîøëî íå òàê ïðè ïîëó÷åíèè ðåçóëüòàòîâ äî÷åðíåãî ïðîöåññà\n");
- exit(-1);
- }
- while (!feof(resFile))
- {
- char* str = calloc(BUFFER, sizeof(char));
- fgets(str, BUFFER, resFile);
- printf("%s", str);
- }
- }
- else
- {
- printf("×òî-òî ïîøëî íå òàê ïðè ðåøåíèè óðàâíåíèÿ\n");
- exit(-1);
- }
- system("pause");
- return 0;
- }
|