Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

Using CreateProcess to launch games

I use CreateProcess to launch games like Unreal Tournament, Quake, etc...

Some games do not start properly. They just close before being fully launched. I even change the directory to the game directory. Could someone tell me what the best values for the flags would be for these games ?

I use the following code.

procedure TFormMainMenu.LaunchApplication(pFileName: string; var pThreadId, pThreadHandle: DWORD);
var
  zAppName: array[0..512] of char;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  ButtonQuit.Enabled := False;
  fLaunchingApplication := True;
  try
    StrPCopy(zAppName, pFileName);
    FillChar(StartupInfo, Sizeof(StartupInfo), #0);
    StartupInfo.cb := Sizeof(StartupInfo);
    StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
    StartupInfo.wShowWindow := SW_NORMAL;
    if CreateProcess(nil,
                     zAppName, { pointer to command line string }
                     nil,      { pointer to process security attributes }
                     nil,      { pointer to thread security attributes }
                     True,    { handle inheritance flag }
                     CREATE_NEW_CONSOLE, { creation flags }
                     nil,      { pointer to new environment block }
                     nil,      { pointer to current directory name }
                     StartupInfo,  { pointer to STARTUPINFO }
                     ProcessInfo) then { pointer to PROCESS_INF }
    begin
      WaitForInputIdle(ProcessInfo.hProcess, 300000);
      pThreadHandle := ProcessInfo.hThread;
      pThreadId := ProcessInfo.dwThreadId;
    end
    else
    begin
      pThreadHandle := 0;
      pThreadId := 0;
    end;
    // The handle to the process is needed to see if the application is still
    // active.
    CloseHandle(ProcessInfo.hProcess);
  finally
    ButtonQuit.Enabled := True;
    fLaunchingApplication := False;
  end;
end;
0
alexandram
Asked:
alexandram
  • 2
  • 2
  • 2
  • +2
1 Solution
 
jhanceCommented:
For a Windows GUI application (i.e. non-console), you don't want to set the dwCreationFlags to CREATE_NEW_CONSOLE.  Just make this parameter 0.

When you're done with CreateProcess, don't use WaitForInputIdle to keep tabs on in.  Use WaitForSingleObject and you will get signaled with the program exits.
0
 
nietodCommented:
And close the thread handle too.
0
 
MadshiCommented:
I recommend using ShellExecuteEx to start the original shortCut (*.lnk) that the game setup installed. Perhaps there are some special settings in the shortCut which are needed to start the game correctly. Starting the lnk-file with ShellExecuteEx will be exactly the same as if the user had double-clicked the lnk-file.
With create CreateProcess you can't start lnk-files. And if you start the exe itself, you will perhaps miss some important settings like a special working directory or something like that.
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
alexandramAuthor Commented:
Madshi,

I considered that option but the problem was that I am using PostThreadMessage to close the application and therefore need the thread ID. From what I have experienced, PostThreadMessage with WM_QUIT is the best way to close applications. I can get the process handle with shellexecuteex but not the threadid. I am also using the threadid to bring windows back to the front. Is there a way to get the threadid from the process handle?
0
 
MadshiCommented:
Not with documented means...   :-((
I do have code to get the processID from the process handle and to enumerate threads under all systems, but that's all undocumented and very ugly. I don't recommend using that.
So if you need the threadID I've another suggestion: Use the IShellLink interface to get all infos from the lnk-file. Then use CreateProcess with the parameters you've read.
0
 
SamHobbsCommented:
I have not played around with CreateProcess a lot, but I have enough to say that probably WaitForInputIdle is a good idea and not likely to cause a problem.

The answer to not use CREATE_NEW_CONSOLE is probably the correct answer.

Do you know though what CreateProcess is returning when it does not work?
0
 
jhanceCommented:
alexandram,

What's up?  Six days without any response.....  Are you still alive?
0
 
alexandramAuthor Commented:
Sorry about the delay. The user who were having the problem haven't complained so maybe removing CREATE_NEW_CONSOLE fixed it. Thank you.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now