Process.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <Windows.h>
  3. #include <stdio.h>
  4. #include "QuadEq.h"
  5. #define FILE_OUT "C:\\Users\\òàðàíàà\\source\\repos\\TaskSystem\\x64\\Debug\\result.txt"
  6. QuadraticEquation* readFile(char* nameFile)
  7. {
  8. QuadraticEquation* equation = malloc(sizeof(QuadraticEquation));
  9. FILE* file = fopen(nameFile, "r");
  10. if (file == NULL)
  11. {
  12. printf("Ôàéëà ñ èõîäíûìè äàííûìè íå ñóùåñòâóåò. Ñîçäàéòå ôàéë %s \n", nameFile);
  13. exit(ERROR_OPEN_FILE);
  14. }
  15. int resultRead = fscanf(file, "%lf %lf %lf", &equation->a, &equation->b, &equation->c);
  16. if (resultRead == 0)
  17. {
  18. printf("Íå óäàëîñü êîððåêòíî ïðî÷èòàòü ôàéë.\n");
  19. exit(ERROR_READ_DATA);
  20. }
  21. if (equation->a == 0 && equation->b == 0 && equation->c == 0)
  22. {
  23. printf("Âñå êîýôèöèåíòû ðàâíû 0. Ïðîâåðüòå èñõîäíûé ôàéë.\n");
  24. exit(ERROR_READ_DATA);
  25. }
  26. if (equation->a == 0)
  27. {
  28. printf("Êîýôôèöèåíò à ðàâåí 0. Ýòî íå êâàäðàòíîå óðàâíåíèå.\n");
  29. exit(ERROR_READ_DATA);
  30. }
  31. fclose(file);
  32. return equation;
  33. }
  34. int startWorkWithProcess()
  35. {
  36. QuadraticEquation* myEq = readFile(FILE_IN);
  37. LPSTR inputKoef = calloc(BUFFER, sizeof(char));
  38. sprintf(inputKoef, "%lf %lf %lf\0", myEq->a, myEq->b, myEq->c);
  39. /*
  40. * Äàííûå äëÿ ïåðåäà÷è â äî÷åðíèé ïðîöåññ:
  41. * childName - èìÿ äî÷åðíåãî èñïîëÿåìîãî ôàéëà
  42. * commandLine - àðãóìåíòû êîìàíäíîé ñòðîêè äëÿ ïåðåäà÷è â íîâûé ïðîöåññ
  43. * si - áëîê íà÷àëüíûõ ïàðàìåòðîâ
  44. * pi - ñòðóêòóðà, îïèñûâàþùàÿ ïîðîæäåííûé ïðîöåññ
  45. */
  46. LPCSTR childName = "C:\\Users\\òàðàíàà\\source\\repos\\TaskSystem\\x64\\Debug\\ChildProcess.exe";
  47. //LPCSTR childName = "ChildProcess.exe";
  48. LPSTR commandLine = inputKoef;
  49. STARTUPINFO si;
  50. /*Çàïîëíåíèå äàííûõ ñòðóêòóðû ñòàíäàðòíûìè äàííûìè*/
  51. ZeroMemory(&si, sizeof(si));
  52. si.cb = sizeof(si);
  53. PROCESS_INFORMATION pi;
  54. ZeroMemory(&pi, sizeof(pi));
  55. //TRUE / FALSE îòâå÷àåò çà íàñëåäîâàíèå äåñêðèïòîðîâ.  äàííîì ñëó÷àå ðàáîòàåò è áåç ýòîãî.
  56. if (!CreateProcessA(childName, commandLine, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi))
  57. {
  58. printf("Íå óäàëîñü ñîçäàòü ïðîöåññ. Êîä îøèáêè: %d.\n", GetLastError());
  59. }
  60. // Îæèäàíèå çàâåðøåíèÿ äî÷åðíåãî ïðîöåññà
  61. WaitForSingleObject(pi.hProcess, INFINITE);
  62. DWORD childexitcode;
  63. GetExitCodeProcess(pi.hProcess, &childexitcode);
  64. CloseHandle(pi.hProcess);
  65. CloseHandle(pi.hThread);
  66. printf("Êîä çàâåðøåíèÿ äî÷åðíåãî ïðîöåññà %d\n", childexitcode);
  67. if (childexitcode >= 0 && childexitcode <= 2)
  68. {
  69. FILE* resFile = fopen(FILE_OUT, "r");
  70. if (resFile == NULL)
  71. {
  72. printf("×òî-òî ïîøëî íå òàê ïðè ïîëó÷åíèè ðåçóëüòàòîâ äî÷åðíåãî ïðîöåññà\n");
  73. exit(-1);
  74. }
  75. while (!feof(resFile))
  76. {
  77. char* str = calloc(BUFFER, sizeof(char));
  78. fgets(str, BUFFER, resFile);
  79. printf("%s", str);
  80. }
  81. }
  82. else
  83. {
  84. printf("×òî-òî ïîøëî íå òàê ïðè ðåøåíèè óðàâíåíèÿ\n");
  85. exit(-1);
  86. }
  87. system("pause");
  88. return 0;
  89. }