process.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. ** Command & Conquer Generals(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. #ifdef USE_GAMEDIR_FROM_LCF
  44. retval=CreateProcess(NULL,cmdargs,NULL,NULL,TRUE, 0 ,NULL, process.directory,&si,&piProcess);
  45. #else
  46. retval=CreateProcess(NULL,cmdargs,NULL,NULL,TRUE, 0 ,NULL, NULL/*process.directory*/,&si,&piProcess);
  47. #endif
  48. #endif
  49. DBGMSG("("<<retval<<") New process: HANDLE " << (void *)piProcess.hProcess << " ID "
  50. << (DWORD)piProcess.dwProcessId);
  51. DBGMSG("("<<retval<<") New thread: HANDLE " << (void *)piProcess.hThread << " ID "
  52. << (DWORD)piProcess.dwThreadId);
  53. if (retval==0)
  54. {
  55. char message_buffer[256];
  56. FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &message_buffer[0], 256, NULL );
  57. DBGMSG("ERR: "<<message_buffer);
  58. }
  59. process.hProcess=piProcess.hProcess;
  60. process.dwProcessID = piProcess.dwProcessId;
  61. process.hThread=piProcess.hThread;
  62. process.dwThreadID = piProcess.dwThreadId;
  63. return(TRUE);
  64. }
  65. //
  66. // Wait for a process to complete, and fill in the exit code
  67. //
  68. bit8 Wait_Process(Process &process, DWORD *exit_code)
  69. {
  70. DWORD retval;
  71. retval=WaitForSingleObject(process.hProcess,INFINITE);
  72. if (exit_code != NULL)
  73. *exit_code=-1;
  74. if (retval==WAIT_OBJECT_0) // process exited
  75. {
  76. // MDC 1/10/2003 Inserting sleep here to let game exit before applying patch
  77. Sleep(3000);
  78. if (exit_code != NULL)
  79. GetExitCodeProcess(process.hProcess,exit_code);
  80. return(TRUE);
  81. }
  82. else // can this happen?
  83. return(FALSE);
  84. }
  85. //
  86. // Get the process to run from the config object
  87. //
  88. bit8 Read_Process_Info(ConfigFile &config,OUT Process &info, IN char *key)
  89. {
  90. Wstring keyStr = "RUN";
  91. if (key)
  92. keyStr = key;
  93. Wstring procinfo;
  94. if (config.getString(keyStr.get(), procinfo)==FALSE)
  95. {
  96. DBGMSG("Couldn't read the RUN line");
  97. return(FALSE);
  98. }
  99. int offset=0;
  100. Wstring dir;
  101. Wstring executable;
  102. Wstring args;
  103. offset=procinfo.getToken(offset," ",dir);
  104. offset=procinfo.getToken(offset," ",executable);
  105. args=procinfo;
  106. args.remove(0,offset);
  107. ///
  108. ///
  109. DBGMSG("RUN: EXE = "<<executable.get()<<" DIR = "<<dir.get()<<
  110. " ARGS = "<<args.get());
  111. strcpy(info.command,executable.get());
  112. strcpy(info.directory,dir.get());
  113. strcpy(info.args,args.get());
  114. return(TRUE);
  115. /*********************************************************
  116. FILE *in;
  117. char string[256];
  118. bit8 found_space;
  119. int i;
  120. if ((in=fopen(config,"r"))==NULL)
  121. return(FALSE);
  122. while(fgets(string,256,in))
  123. {
  124. i=0;
  125. while ((isspace(string[i]))&&(i<int(strlen(string))))
  126. i++;
  127. // do nothing with empty line or commented line
  128. if ((string[i]=='#')||(i==int(strlen(string)-1)))
  129. continue;
  130. strcpy(info.directory,string);
  131. found_space=FALSE;
  132. for (; i<int(strlen(string)); i++)
  133. {
  134. if (isspace(info.directory[i]))
  135. {
  136. info.directory[i]=0;
  137. found_space=TRUE;
  138. }
  139. else if (found_space)
  140. break;
  141. }
  142. strcpy(info.command,string+i);
  143. for (i=0; i<int(strlen(info.command)); i++)
  144. if ((info.command[i]=='\n')||(info.command[i]=='\r'))
  145. info.command[i]=0;
  146. //printf("DIR = '%s' CMD = '%s'\n",info.directory,info.command);
  147. // We only have 1 process for this
  148. break;
  149. }
  150. fclose(in);
  151. return(TRUE);
  152. **********************************************/
  153. }