process.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "process.h"
  19. Process::Process()
  20. {
  21. directory[0]=0;
  22. command[0]=0;
  23. args[0]=0;
  24. hProcess=NULL;
  25. hThread=NULL;
  26. }
  27. // Create a process
  28. bit8 Create_Process(Process &process)
  29. {
  30. int retval;
  31. STARTUPINFO si;
  32. PROCESS_INFORMATION piProcess;
  33. ZeroMemory(&si,sizeof(si));
  34. si.cb=sizeof(si);
  35. char cmdargs[513];
  36. memset(cmdargs,0,513);
  37. strcpy(cmdargs,process.command);
  38. strcat(cmdargs,process.args);
  39. DBGMSG("PROCESS CMD="<<cmdargs<<" DIR="<<process.directory);
  40. #ifndef COPY_PROTECT
  41. retval=CreateProcess(NULL,cmdargs,NULL,NULL,FALSE, 0 ,NULL, NULL/*process.directory*/,&si,&piProcess);
  42. #else
  43. retval=CreateProcess(NULL,cmdargs,NULL,NULL,TRUE, 0 ,NULL, NULL/*process.directory*/,&si,&piProcess);
  44. #endif
  45. DBGMSG("("<<retval<<") New process: HANDLE " << (void *)piProcess.hProcess << " ID "
  46. << (DWORD)piProcess.dwProcessId);
  47. DBGMSG("("<<retval<<") New thread: HANDLE " << (void *)piProcess.hThread << " ID "
  48. << (DWORD)piProcess.dwThreadId);
  49. if (retval==0)
  50. {
  51. char message_buffer[256];
  52. FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &message_buffer[0], 256, NULL );
  53. DBGMSG("ERR: "<<message_buffer);
  54. }
  55. process.hProcess=piProcess.hProcess;
  56. process.dwProcessID = piProcess.dwProcessId;
  57. process.hThread=piProcess.hThread;
  58. process.dwThreadID = piProcess.dwThreadId;
  59. return(TRUE);
  60. }
  61. //
  62. // Wait for a process to complete, and fill in the exit code
  63. //
  64. bit8 Wait_Process(Process &process, DWORD *exit_code)
  65. {
  66. DWORD retval;
  67. retval=WaitForSingleObject(process.hProcess,INFINITE);
  68. if (exit_code != NULL)
  69. *exit_code=-1;
  70. if (retval==WAIT_OBJECT_0) // process exited
  71. {
  72. if (exit_code != NULL)
  73. GetExitCodeProcess(process.hProcess,exit_code);
  74. return(TRUE);
  75. }
  76. else // can this happen?
  77. return(FALSE);
  78. }
  79. //
  80. // Get the process to run from the config object
  81. //
  82. bit8 Read_Process_Info(ConfigFile &config,OUT Process &info, IN char *key)
  83. {
  84. Wstring keyStr = "RUN";
  85. if (key)
  86. keyStr = key;
  87. Wstring procinfo;
  88. if (config.getString(keyStr.get(), procinfo)==FALSE)
  89. {
  90. DBGMSG("Couldn't read the RUN line");
  91. return(FALSE);
  92. }
  93. int offset=0;
  94. Wstring dir;
  95. Wstring executable;
  96. Wstring args;
  97. offset=procinfo.getToken(offset," ",dir);
  98. offset=procinfo.getToken(offset," ",executable);
  99. args=procinfo;
  100. args.remove(0,offset);
  101. ///
  102. ///
  103. DBGMSG("RUN: EXE = "<<executable.get()<<" DIR = "<<dir.get()<<
  104. " ARGS = "<<args.get());
  105. strcpy(info.command,executable.get());
  106. strcpy(info.directory,dir.get());
  107. strcpy(info.args,args.get());
  108. return(TRUE);
  109. /*********************************************************
  110. FILE *in;
  111. char string[256];
  112. bit8 found_space;
  113. int i;
  114. if ((in=fopen(config,"r"))==NULL)
  115. return(FALSE);
  116. while(fgets(string,256,in))
  117. {
  118. i=0;
  119. while ((isspace(string[i]))&&(i<int(strlen(string))))
  120. i++;
  121. // do nothing with empty line or commented line
  122. if ((string[i]=='#')||(i==int(strlen(string)-1)))
  123. continue;
  124. strcpy(info.directory,string);
  125. found_space=FALSE;
  126. for (; i<int(strlen(string)); i++)
  127. {
  128. if (isspace(info.directory[i]))
  129. {
  130. info.directory[i]=0;
  131. found_space=TRUE;
  132. }
  133. else if (found_space)
  134. break;
  135. }
  136. strcpy(info.command,string+i);
  137. for (i=0; i<int(strlen(info.command)); i++)
  138. if ((info.command[i]=='\n')||(info.command[i]=='\r'))
  139. info.command[i]=0;
  140. //printf("DIR = '%s' CMD = '%s'\n",info.directory,info.command);
  141. // We only have 1 process for this
  142. break;
  143. }
  144. fclose(in);
  145. return(TRUE);
  146. **********************************************/
  147. }