Solved

Using CreateProcess to launch games

Posted on 2000-03-11
8
351 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now