Solved

Using CreateProcess to launch games

Posted on 2000-03-11
8
362 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 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