regarding spawnProcess and parameters in Windows

Mike Parker aldacron at gmail.com
Mon Dec 16 01:59:52 PST 2013


On 12/16/2013 6:33 PM, Hugo Florentino wrote:
> On Mon, 16 Dec 2013 02:04:10 +0100, Danny Arends wrote:

>
> However, something is bothering me: when running the launcher, it opens
> a console temporarily before launching the other process (even when I am
> not using std.stdio) and I cannot get rid of this behavior.
>
> This is the code I am now using:
>
> import std.file: exists, getcwd;
> import std.path: buildPath, dirName;
> import std.string: format, toStringz;
> import core.sys.windows.windows;
>
> immutable static auto appname = "myapp";
>
> extern(Windows) HANDLE ShellExecuteA(HWND, LPCSTR, LPCSTR, LPCSTR,
> LPCSTR, int);
>
> int main(string[] args) {
>    auto appath = getcwd();
>    auto appexe = buildPath(appath, appname ~ ".exe");
>    if (exists(appexe)) {
>      auto param = format(`/ini="%s"`, buildPath(appath, appname ~ ".ini"));
>      ShellExecuteA(null, "", toStringz(appexe), toStringz(param), "",
> SW_SHOWMAXIMIZED);
>      scope(failure) return -1;
>    }
>    return 0;
> }
>
> Why does the console window appear and how can I prevent this?

This is how Windows works. There are a couple of ways to eliminate the 
console. One is to use WinMain instead of main. That requires some 
boilerplate, though, as it bypasses the main function in DRuntime, so 
you have to initialize the runtime manually. It has the benefit of being 
portable across compilers. Another way, which is what I always do, is to 
add this to the command line (assuming DMD with OPTLINK):

-L/SUBSYSTEM:WINDOWS:5.01

Every linker on Windows has some form of this flag. It may even be the 
same when using the VC toolchain. Also, you can drop the version number 
at the end. It depends on the minimum version of Windows you target and 
the target architecture (32-bit vs. 64-bit). Google it if you want to be 
sure of the details.



More information about the Digitalmars-d-learn mailing list