www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Program exited with code 1

reply Andrey <saasecondbox yandex.ru> writes:
Hello,
I wrote a small test code with WinApi:

----------------------------------------------------
import core.runtime;
import std.utf;

import core.sys.windows.windows;
import core.sys.windows.wingdi;

class Test
{
     public this() nothrow
     {

     }
}

extern(Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int 
nCmdShow)
{
     new Test(); // error is here
     return 0;
}
----------------------------------------------------

When I run it, there is an error: Program exited with code 1.

If I comment "new Test();" - no error happens. Why? How to create 
instances of classes?

DMD v2.077.1, Windows 10.
Apr 19 2018
parent reply user1234 <user1234 12.nj> writes:
On Thursday, 19 April 2018 at 08:13:00 UTC, Andrey wrote:
 Hello,
 I wrote a small test code with WinApi:

 ----------------------------------------------------
 import core.runtime;
 import std.utf;

 import core.sys.windows.windows;
 import core.sys.windows.wingdi;

 class Test
 {
     public this() nothrow
     {

     }
 }

 extern(Windows)
 int WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, 
 int nCmdShow)
 {
     new Test(); // error is here
     return 0;
 }
 ----------------------------------------------------

 When I run it, there is an error: Program exited with code 1.

 If I comment "new Test();" - no error happens. Why?
The run-time is not already initialized but the "new" operator relies on it.
Apr 19 2018
parent reply Andrey <saasecondbox yandex.ru> writes:
On Thursday, 19 April 2018 at 08:24:55 UTC, user1234 wrote:
 The run-time is not already initialized but the "new" operator 
 relies on it.
What will be a solution?
Apr 19 2018
parent reply Andrey <saasecondbox yandex.ru> writes:
On Thursday, 19 April 2018 at 08:37:19 UTC, Andrey wrote:
 What will be a solution?
It seems to me that I found a solution - just replace WinMain() with main().
Apr 19 2018
parent Mike Parker <aldacron gmail.com> writes:
On Thursday, 19 April 2018 at 11:21:52 UTC, Andrey wrote:
 On Thursday, 19 April 2018 at 08:37:19 UTC, Andrey wrote:
 What will be a solution?
It seems to me that I found a solution - just replace WinMain() with main().
That's fine when you want a console app, but it leaves you with a console window automatically popping up if you create a windowed app. When using WinMain, you have to manually setup and tear down DRuntime as described in the wiki[1]. Alternatively, you can get rid of the console window with a main function via linker flags -- when using OPTLINK (the default): -L/SUBSYSTEM:windows should be enough. With -m32mscoff or -m64, which uses the MS linker, you'll both that and this: -L/ENTRY:mainCRTStartup [1] https://wiki.dlang.org/D_for_Win32
Apr 19 2018