Hide process window with 'CreateProcess'












6















I'm using a procedure provided to me that'll run a process but I want the process to be run in the background without the window showing up. The call is



 ExecProcess(ProgPath, '', False);


and the function is



function ExecProcess(ProgramName, WorkDir: string; Wait: boolean): integer;
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
CreateOK: boolean;
ExitCode: integer;
dwExitCode: DWORD;
begin
ExitCode := -1;

FillChar(StartInfo, SizeOf(TStartupInfo), #0);
FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
StartInfo.cb := SizeOf(TStartupInfo);

if WorkDir <> '' then
begin
CreateOK := CreateProcess(nil, Addr(ProgramName[1]), nil, Addr(WorkDir[1]),
false, CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS, nil, nil,
StartInfo, ProcInfo);
end
else
begin
CreateOK := CreateProcess(nil, Addr(ProgramName[1]), nil, nil, false,
CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS, nil, Addr(WorkDir[1]),
StartInfo, ProcInfo);
end;

{ check to see if successful }

if CreateOK then
begin
// may or may not be needed. Usually wait for child processes
if Wait then
begin
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
GetExitCodeProcess(ProcInfo.hProcess, dwExitCode);
ExitCode := dwExitCode;
end;
end
else
begin
ShowMessage('Unable to run ' + ProgramName);
end;

CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);

Result := ExitCode;

end;


I have tried to use the StartInfo.wShowWindow attribute with SW_MINIMIZE, SW_FORCEMINIMIZE and SW_SHOWMINIMIZED but it ain't working. I can see that the attribute is changing in the debugger but that's as much as I understand right now. Could someone point out what the problem is?



EDIT: If it matters I'm executing a couple of Fortran modules (.exe) with arguments that'll open up a CMD-window.










share|improve this question




















  • 1





    Is the other process console or GUI app? You have issues with signed and unsigned vars on the exit code. The second param of CreateProcess must be writable. Use UniqueString for that.

    – David Heffernan
    Nov 6 '14 at 10:12






  • 1





    Do not use + for merging flags CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, you must use or operator, like CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS. In this case result is same, but in many cases it wont.

    – kibab
    Nov 6 '14 at 10:37













  • In addition to what David said about UniqueString(), you should also use PChar(ProgramName) instead of Addr(ProgramName[1]).

    – Remy Lebeau
    Nov 6 '14 at 18:13


















6















I'm using a procedure provided to me that'll run a process but I want the process to be run in the background without the window showing up. The call is



 ExecProcess(ProgPath, '', False);


and the function is



function ExecProcess(ProgramName, WorkDir: string; Wait: boolean): integer;
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
CreateOK: boolean;
ExitCode: integer;
dwExitCode: DWORD;
begin
ExitCode := -1;

FillChar(StartInfo, SizeOf(TStartupInfo), #0);
FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
StartInfo.cb := SizeOf(TStartupInfo);

if WorkDir <> '' then
begin
CreateOK := CreateProcess(nil, Addr(ProgramName[1]), nil, Addr(WorkDir[1]),
false, CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS, nil, nil,
StartInfo, ProcInfo);
end
else
begin
CreateOK := CreateProcess(nil, Addr(ProgramName[1]), nil, nil, false,
CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS, nil, Addr(WorkDir[1]),
StartInfo, ProcInfo);
end;

{ check to see if successful }

if CreateOK then
begin
// may or may not be needed. Usually wait for child processes
if Wait then
begin
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
GetExitCodeProcess(ProcInfo.hProcess, dwExitCode);
ExitCode := dwExitCode;
end;
end
else
begin
ShowMessage('Unable to run ' + ProgramName);
end;

CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);

Result := ExitCode;

end;


I have tried to use the StartInfo.wShowWindow attribute with SW_MINIMIZE, SW_FORCEMINIMIZE and SW_SHOWMINIMIZED but it ain't working. I can see that the attribute is changing in the debugger but that's as much as I understand right now. Could someone point out what the problem is?



EDIT: If it matters I'm executing a couple of Fortran modules (.exe) with arguments that'll open up a CMD-window.










share|improve this question




















  • 1





    Is the other process console or GUI app? You have issues with signed and unsigned vars on the exit code. The second param of CreateProcess must be writable. Use UniqueString for that.

    – David Heffernan
    Nov 6 '14 at 10:12






  • 1





    Do not use + for merging flags CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, you must use or operator, like CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS. In this case result is same, but in many cases it wont.

    – kibab
    Nov 6 '14 at 10:37













  • In addition to what David said about UniqueString(), you should also use PChar(ProgramName) instead of Addr(ProgramName[1]).

    – Remy Lebeau
    Nov 6 '14 at 18:13
















6












6








6


2






I'm using a procedure provided to me that'll run a process but I want the process to be run in the background without the window showing up. The call is



 ExecProcess(ProgPath, '', False);


and the function is



function ExecProcess(ProgramName, WorkDir: string; Wait: boolean): integer;
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
CreateOK: boolean;
ExitCode: integer;
dwExitCode: DWORD;
begin
ExitCode := -1;

FillChar(StartInfo, SizeOf(TStartupInfo), #0);
FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
StartInfo.cb := SizeOf(TStartupInfo);

if WorkDir <> '' then
begin
CreateOK := CreateProcess(nil, Addr(ProgramName[1]), nil, Addr(WorkDir[1]),
false, CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS, nil, nil,
StartInfo, ProcInfo);
end
else
begin
CreateOK := CreateProcess(nil, Addr(ProgramName[1]), nil, nil, false,
CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS, nil, Addr(WorkDir[1]),
StartInfo, ProcInfo);
end;

{ check to see if successful }

if CreateOK then
begin
// may or may not be needed. Usually wait for child processes
if Wait then
begin
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
GetExitCodeProcess(ProcInfo.hProcess, dwExitCode);
ExitCode := dwExitCode;
end;
end
else
begin
ShowMessage('Unable to run ' + ProgramName);
end;

CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);

Result := ExitCode;

end;


I have tried to use the StartInfo.wShowWindow attribute with SW_MINIMIZE, SW_FORCEMINIMIZE and SW_SHOWMINIMIZED but it ain't working. I can see that the attribute is changing in the debugger but that's as much as I understand right now. Could someone point out what the problem is?



EDIT: If it matters I'm executing a couple of Fortran modules (.exe) with arguments that'll open up a CMD-window.










share|improve this question
















I'm using a procedure provided to me that'll run a process but I want the process to be run in the background without the window showing up. The call is



 ExecProcess(ProgPath, '', False);


and the function is



function ExecProcess(ProgramName, WorkDir: string; Wait: boolean): integer;
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
CreateOK: boolean;
ExitCode: integer;
dwExitCode: DWORD;
begin
ExitCode := -1;

FillChar(StartInfo, SizeOf(TStartupInfo), #0);
FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
StartInfo.cb := SizeOf(TStartupInfo);

if WorkDir <> '' then
begin
CreateOK := CreateProcess(nil, Addr(ProgramName[1]), nil, Addr(WorkDir[1]),
false, CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS, nil, nil,
StartInfo, ProcInfo);
end
else
begin
CreateOK := CreateProcess(nil, Addr(ProgramName[1]), nil, nil, false,
CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS, nil, Addr(WorkDir[1]),
StartInfo, ProcInfo);
end;

{ check to see if successful }

if CreateOK then
begin
// may or may not be needed. Usually wait for child processes
if Wait then
begin
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
GetExitCodeProcess(ProcInfo.hProcess, dwExitCode);
ExitCode := dwExitCode;
end;
end
else
begin
ShowMessage('Unable to run ' + ProgramName);
end;

CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);

Result := ExitCode;

end;


I have tried to use the StartInfo.wShowWindow attribute with SW_MINIMIZE, SW_FORCEMINIMIZE and SW_SHOWMINIMIZED but it ain't working. I can see that the attribute is changing in the debugger but that's as much as I understand right now. Could someone point out what the problem is?



EDIT: If it matters I'm executing a couple of Fortran modules (.exe) with arguments that'll open up a CMD-window.







delphi delphi-xe5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 17:45









Rigel

10.1k14111224




10.1k14111224










asked Nov 6 '14 at 9:26









user3464658user3464658

84312




84312








  • 1





    Is the other process console or GUI app? You have issues with signed and unsigned vars on the exit code. The second param of CreateProcess must be writable. Use UniqueString for that.

    – David Heffernan
    Nov 6 '14 at 10:12






  • 1





    Do not use + for merging flags CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, you must use or operator, like CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS. In this case result is same, but in many cases it wont.

    – kibab
    Nov 6 '14 at 10:37













  • In addition to what David said about UniqueString(), you should also use PChar(ProgramName) instead of Addr(ProgramName[1]).

    – Remy Lebeau
    Nov 6 '14 at 18:13
















  • 1





    Is the other process console or GUI app? You have issues with signed and unsigned vars on the exit code. The second param of CreateProcess must be writable. Use UniqueString for that.

    – David Heffernan
    Nov 6 '14 at 10:12






  • 1





    Do not use + for merging flags CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, you must use or operator, like CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS. In this case result is same, but in many cases it wont.

    – kibab
    Nov 6 '14 at 10:37













  • In addition to what David said about UniqueString(), you should also use PChar(ProgramName) instead of Addr(ProgramName[1]).

    – Remy Lebeau
    Nov 6 '14 at 18:13










1




1





Is the other process console or GUI app? You have issues with signed and unsigned vars on the exit code. The second param of CreateProcess must be writable. Use UniqueString for that.

– David Heffernan
Nov 6 '14 at 10:12





Is the other process console or GUI app? You have issues with signed and unsigned vars on the exit code. The second param of CreateProcess must be writable. Use UniqueString for that.

– David Heffernan
Nov 6 '14 at 10:12




1




1





Do not use + for merging flags CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, you must use or operator, like CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS. In this case result is same, but in many cases it wont.

– kibab
Nov 6 '14 at 10:37







Do not use + for merging flags CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, you must use or operator, like CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS. In this case result is same, but in many cases it wont.

– kibab
Nov 6 '14 at 10:37















In addition to what David said about UniqueString(), you should also use PChar(ProgramName) instead of Addr(ProgramName[1]).

– Remy Lebeau
Nov 6 '14 at 18:13







In addition to what David said about UniqueString(), you should also use PChar(ProgramName) instead of Addr(ProgramName[1]).

– Remy Lebeau
Nov 6 '14 at 18:13














1 Answer
1






active

oldest

votes


















9














Use dwFlags with STARTF_USESHOWWINDOW to forced the usage of wShowWindow



StartInfo.wShowWindow := SW_HIDE;
StartInfo.dwFlags := STARTF_USESHOWWINDOW;





share|improve this answer


























  • Thank you for taking the time, @TLama just gave me the same solution.

    – user3464658
    Nov 6 '14 at 9:38











  • Ups, @TLama sorry having open the the question I just could see SW_HIDE, I did not realize the update of the comment. Maybe you should add it as answer and I'll delete mine.

    – bummi
    Nov 6 '14 at 9:41








  • 2





    STARTF_USESHOWWINDOW only works for GUI apps, and even then it is only a hint, there is no 100% guarantee the app will actually honor it, depending on how it implements its UI. Standard UIs using ShowWindow() will honor it, though. For a console app, you can use the CREATE_NO_WINDOW flag in the dwCreationFlags parameter of CreateProcess().

    – Remy Lebeau
    Nov 6 '14 at 18:11













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26775794%2fhide-process-window-with-createprocess%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









9














Use dwFlags with STARTF_USESHOWWINDOW to forced the usage of wShowWindow



StartInfo.wShowWindow := SW_HIDE;
StartInfo.dwFlags := STARTF_USESHOWWINDOW;





share|improve this answer


























  • Thank you for taking the time, @TLama just gave me the same solution.

    – user3464658
    Nov 6 '14 at 9:38











  • Ups, @TLama sorry having open the the question I just could see SW_HIDE, I did not realize the update of the comment. Maybe you should add it as answer and I'll delete mine.

    – bummi
    Nov 6 '14 at 9:41








  • 2





    STARTF_USESHOWWINDOW only works for GUI apps, and even then it is only a hint, there is no 100% guarantee the app will actually honor it, depending on how it implements its UI. Standard UIs using ShowWindow() will honor it, though. For a console app, you can use the CREATE_NO_WINDOW flag in the dwCreationFlags parameter of CreateProcess().

    – Remy Lebeau
    Nov 6 '14 at 18:11


















9














Use dwFlags with STARTF_USESHOWWINDOW to forced the usage of wShowWindow



StartInfo.wShowWindow := SW_HIDE;
StartInfo.dwFlags := STARTF_USESHOWWINDOW;





share|improve this answer


























  • Thank you for taking the time, @TLama just gave me the same solution.

    – user3464658
    Nov 6 '14 at 9:38











  • Ups, @TLama sorry having open the the question I just could see SW_HIDE, I did not realize the update of the comment. Maybe you should add it as answer and I'll delete mine.

    – bummi
    Nov 6 '14 at 9:41








  • 2





    STARTF_USESHOWWINDOW only works for GUI apps, and even then it is only a hint, there is no 100% guarantee the app will actually honor it, depending on how it implements its UI. Standard UIs using ShowWindow() will honor it, though. For a console app, you can use the CREATE_NO_WINDOW flag in the dwCreationFlags parameter of CreateProcess().

    – Remy Lebeau
    Nov 6 '14 at 18:11
















9












9








9







Use dwFlags with STARTF_USESHOWWINDOW to forced the usage of wShowWindow



StartInfo.wShowWindow := SW_HIDE;
StartInfo.dwFlags := STARTF_USESHOWWINDOW;





share|improve this answer















Use dwFlags with STARTF_USESHOWWINDOW to forced the usage of wShowWindow



StartInfo.wShowWindow := SW_HIDE;
StartInfo.dwFlags := STARTF_USESHOWWINDOW;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 6 '14 at 18:11









Remy Lebeau

337k18260455




337k18260455










answered Nov 6 '14 at 9:37









bummibummi

25.1k85289




25.1k85289













  • Thank you for taking the time, @TLama just gave me the same solution.

    – user3464658
    Nov 6 '14 at 9:38











  • Ups, @TLama sorry having open the the question I just could see SW_HIDE, I did not realize the update of the comment. Maybe you should add it as answer and I'll delete mine.

    – bummi
    Nov 6 '14 at 9:41








  • 2





    STARTF_USESHOWWINDOW only works for GUI apps, and even then it is only a hint, there is no 100% guarantee the app will actually honor it, depending on how it implements its UI. Standard UIs using ShowWindow() will honor it, though. For a console app, you can use the CREATE_NO_WINDOW flag in the dwCreationFlags parameter of CreateProcess().

    – Remy Lebeau
    Nov 6 '14 at 18:11





















  • Thank you for taking the time, @TLama just gave me the same solution.

    – user3464658
    Nov 6 '14 at 9:38











  • Ups, @TLama sorry having open the the question I just could see SW_HIDE, I did not realize the update of the comment. Maybe you should add it as answer and I'll delete mine.

    – bummi
    Nov 6 '14 at 9:41








  • 2





    STARTF_USESHOWWINDOW only works for GUI apps, and even then it is only a hint, there is no 100% guarantee the app will actually honor it, depending on how it implements its UI. Standard UIs using ShowWindow() will honor it, though. For a console app, you can use the CREATE_NO_WINDOW flag in the dwCreationFlags parameter of CreateProcess().

    – Remy Lebeau
    Nov 6 '14 at 18:11



















Thank you for taking the time, @TLama just gave me the same solution.

– user3464658
Nov 6 '14 at 9:38





Thank you for taking the time, @TLama just gave me the same solution.

– user3464658
Nov 6 '14 at 9:38













Ups, @TLama sorry having open the the question I just could see SW_HIDE, I did not realize the update of the comment. Maybe you should add it as answer and I'll delete mine.

– bummi
Nov 6 '14 at 9:41







Ups, @TLama sorry having open the the question I just could see SW_HIDE, I did not realize the update of the comment. Maybe you should add it as answer and I'll delete mine.

– bummi
Nov 6 '14 at 9:41






2




2





STARTF_USESHOWWINDOW only works for GUI apps, and even then it is only a hint, there is no 100% guarantee the app will actually honor it, depending on how it implements its UI. Standard UIs using ShowWindow() will honor it, though. For a console app, you can use the CREATE_NO_WINDOW flag in the dwCreationFlags parameter of CreateProcess().

– Remy Lebeau
Nov 6 '14 at 18:11







STARTF_USESHOWWINDOW only works for GUI apps, and even then it is only a hint, there is no 100% guarantee the app will actually honor it, depending on how it implements its UI. Standard UIs using ShowWindow() will honor it, though. For a console app, you can use the CREATE_NO_WINDOW flag in the dwCreationFlags parameter of CreateProcess().

– Remy Lebeau
Nov 6 '14 at 18:11






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26775794%2fhide-process-window-with-createprocess%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)