Re: Building Win32 application via dub

2020-04-29 Thread Sam E. via Digitalmars-d-learn

On Wednesday, 29 April 2020 at 12:26:02 UTC, Mike Parker wrote:

On Wednesday, 29 April 2020 at 11:55:54 UTC, Sam E. wrote:



I cannot find a D example using Win32 and the normal main 
function, and while it is working for simple message boxes, as 
soon as I want to do something slightly more complex (using a 
window), an hInstance has to be provided (as far as I 
understand, I'm not that knowledgeable with Win32).


FYI, a quick google turned up this example:

https://gist.github.com/caiorss/e8967d4d3dad522c82aab18ccd8f8304

It's C++ and not D, but the Win32 calls are the same. You 
should be able to adapt that to D fairly easily.


Thank you a lot Mike, you were really helpful!


Re: Building Win32 application via dub

2020-04-29 Thread Sam E. via Digitalmars-d-learn

On Wednesday, 29 April 2020 at 10:46:30 UTC, Mike Parker wrote:

On Wednesday, 29 April 2020 at 10:44:48 UTC, Mike Parker wrote:


Yeah, it says "WinMain is needed", which has never been true.


THere's no need for the def file either.


What's the way to get the hInstance without the use of WinMain?

From Microsoft documentation:
hInstance is the handle to the application instance. Get this 
value from the hInstance parameter of wWinMain.


https://docs.microsoft.com/en-us/windows/win32/learnwin32/creating-a-window

I cannot find a D example using Win32 and the normal main 
function, and while it is working for simple message boxes, as 
soon as I want to do something slightly more complex (using a 
window), an hInstance has to be provided (as far as I understand, 
I'm not that knowledgeable with Win32).


Re: Building Win32 application via dub

2020-04-29 Thread Sam E. via Digitalmars-d-learn

On Wednesday, 29 April 2020 at 10:26:40 UTC, Sam E. wrote:
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.


I took the WinMain from https://wiki.dlang.org/D_for_Win32, 
should that documentation be updated to use a normal main 
function instead? Also the details regarding linker flags may 
be a good addition to that wiki page.


Just to confirm what Mike was saying: removing the WinMain 
completely and using a normal main function with calls to Win32 
functions builds and works perfectly fine, that's a way nicer 
approach.


Re: Building Win32 application via dub

2020-04-29 Thread Sam E. via Digitalmars-d-learn

On Wednesday, 29 April 2020 at 10:12:29 UTC, Mike Parker wrote:
Most likely because you're calling writeln before initializing 
the runtime.


Of course, that was it, thanks for the help Mike!

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".


Thanks again, you're right, I didn't realize that would be the 
case.


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.


I took the WinMain from https://wiki.dlang.org/D_for_Win32, 
should that documentation be updated to use a normal main 
function instead? Also the details regarding linker flags may be 
a good addition to that wiki page.


Re: Building Win32 application via dub

2020-04-29 Thread Sam E. via Digitalmars-d-learn

On Wednesday, 29 April 2020 at 10:19:39 UTC, Ahmet Sait wrote:
Though the program built with dub is now crashing at runtime 
when calling `writeln` within the `WinMain` block.


Back then when I was trying to use writeln (or any standard 
output function like printf)in a non-console app in Windows it 
used to crash, I don't know exact reason behind it but you 
might want to use AllocConsole to workaround it.


If the same code works fine with -m32 but not -m32mscoff (or 
-m64) then I have no idea.


To be honest, I haven't yet found the way to switch between -m32 
and -m64 (or other) via dub :)


Re: Building Win32 application via dub

2020-04-29 Thread Sam E. via Digitalmars-d-learn

On Tuesday, 28 April 2020 at 20:18:29 UTC, Adam D. Ruppe wrote:

On Tuesday, 28 April 2020 at 19:25:06 UTC, Sam E. wrote:
I'm a bit surprised to see a linking error given that building 
directly from `dmd` seems to work fine without any flag.


dmd directly uses -m32 whereas dub by default uses -m32mscoff 
to dmd.


The mscoff linker (also used for -m64 btw) doesn't add the 
user32 library by default, you must explicitly add it.


So with dub just add the user32 library to your config file and 
it should be ok. (I don't recall the syntax for that off the 
top of my head)


Thanks, that seems to link after adding `libs "user32"` to my 
`dub.sdl` file.


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/0xc096
Unhandled exception at 0x7FF643C5AFE4 in test-win32.exe: 
0xC096: 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




Building Win32 application via dub

2020-04-28 Thread Sam E. via Digitalmars-d-learn

Hi,

I'm fairly new to D, just playing around with Win32 bindings. I 
have a Win32 hello world that works when build via `dmd 
.\source\app test-win32.def`. I'm now trying to build the 
application via `dub`, but I cannot find what configuration I 
would need to do so.


By default, just running `dub`, I get the following error:

```
Performing "debug" build using C:\D\dmd2\windows\bin\dmd.exe for 
x86_64.

test-win32 ~master: building configuration "application"...
Linking...
test-win32.obj : error LNK2019: unresolved external symbol 
MessageBoxA referenced in function WinMain

.dub\build\application-debug-windows-x86_64-dmd_2091-347A0A76992D6DB80483AD860293313E\test-win32.exe
 : fatal error LNK1120: 1 unresolved externals
Error: linker exited with status 1120
C:\D\dmd2\windows\bin\dmd.exe failed with exit code 1.
```

The code is the one from https://wiki.dlang.org/D_for_Win32, with 
a simple extra `MessageBoxA` in the function `myWinMain`.


I'm a bit surprised to see a linking error given that building 
directly from `dmd` seems to work fine without any flag.


What am I missing?