Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Using CreateProcess to launch games

Posted on 2000-03-11
8
361 Views
Last Modified: 2013-12-03
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
Comment
Question by:alexandram
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 200 total points
ID: 2608544
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
 
LVL 22

Expert Comment

by:nietod
ID: 2608689
And close the thread handle too.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2611658
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:alexandram
ID: 2612278
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
 
LVL 20

Expert Comment

by:Madshi
ID: 2612302
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
 
LVL 3

Expert Comment

by:SamHobbs
ID: 2626427
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
 
LVL 32

Expert Comment

by:jhance
ID: 2634338
alexandram,

What's up?  Six days without any response.....  Are you still alive?
0
 

Author Comment

by:alexandram
ID: 2634728
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question