Building Win32 application via dub
Mike Parker
aldacron at gmail.com
Wed Apr 29 10:12:29 UTC 2020
On Wednesday, 29 April 2020 at 09:43:53 UTC, Sam E. wrote:
> Though the program built with dub is now crashing at runtime
> when calling `writeln` within the `WinMain` block.
>
> The exception error is:
>
>> Exception has occurred: W32/0xc0000096
>> Unhandled exception at 0x00007FF643C5AFE4 in test-win32.exe:
>> 0xC0000096: Privileged instruction.
>
> So it feels that something else is missing or wrong.
>
> Any pointer would be helpful :)
>
> Screenshot of the call site: https://postimg.cc/5YtY9PRQ
> Screenshot of the expection: https://postimg.cc/K3vKz0pg
Most likely because you're calling writeln before initializing
the runtime.
Also, when using WinMain, you aren't going to see any output from
writeln because you won't have a console window. The linker will
create a "Windows subsystem" app rather than a "Console
subsystem".
Really, there's no reason at all to use WinMain. Just create a
standard main function. Then you don't need to worry about
manually initializing the runtime and you'll have a console
window by default. You can always turn it off in anything you
want to ship without the console by adding the appropriate dflags
to your dub file:
-L/SUBSYSTEM:WINDOWS -L/ENTRY:mainCRTStartup
Conversely, you can get the console window in a WinMain app with:
-L/SUBSYSTEM:CONSOLE -L/ENTRY:WinMainCRTStartup
Though, again, there's really no reason to use WinMain.
The /SUBSYSTEM flag works with the default OPTLINK linker and
Microsoft's link.exe. You don't need the /ENTRY flag with
optlink. It will do the right thing based on the /SUBSYSTEM flag.
https://docs.microsoft.com/en-us/cpp/build/reference/entry-entry-point-symbol?view=vs-2019
https://docs.microsoft.com/en-us/cpp/build/reference/subsystem-specify-subsystem?view=vs-2019
More information about the Digitalmars-d-learn
mailing list