win64 DLL stdout printing after main process completes
Marcone
marcone at email.com
Tue Apr 20 11:36:00 UTC 2021
On Monday, 19 April 2021 at 14:55:03 UTC, cc wrote:
> I'm not sure if this is something unique to D or not, but I've
> having a minor issue where stdout output from a DLL (either via
> printf or phobos std.stdio write) is not displayed until after
> the main process has completed. I'm making a project based
> around the example at https://wiki.dlang.org/Win32_DLLs_in_D
> which I've heard is a little out of date but I've gotten it
> working nonetheless. I have the following project files:
>
> ```d
> // mydll.d
> module mydll;
> import core.runtime;
> import core.stdc.stdio;
> import core.stdc.stdlib;
> import core.sys.windows.windows;
> extern(Windows) BOOL DllMain(HINSTANCE hInstance, ULONG
> ulReason, LPVOID pvReserved) {
> switch (ulReason) {
> case DLL_PROCESS_ATTACH:
> printf("[dll] DLL_PROCESS_ATTACH\n");
> Runtime.initialize();
> break;
> case DLL_PROCESS_DETACH:
> printf("[dll] DLL_PROCESS_DETACH\n");
> Runtime.terminate();
> break;
> case DLL_THREAD_ATTACH:
> printf("[dll] DLL_THREAD_ATTACH\n");
> return false;
> case DLL_THREAD_DETACH:
> printf("[dll] DLL_THREAD_DETACH\n");
> return false;
> default:
> }
> return true;
> }
> export int MyDLL_Test() {
> printf("[dll] MyDLL_Test\n");
> return 5;
> }
> static this() {
> printf("[dll] static this for mydll\n");
> }
> static ~this() {
> printf("[dll] static ~this for mydll\n");
> }
> ```
>
> ```d
> // mydll.di
> module mydll;
> export int MyDLL_Test();
> ```
>
> ```d
> // main.d
> import mydll;
> pragma(lib, "mydll.lib");
> import core.sys.windows.windows;
> void main() {
> printf("[Main] Start\n");
> scope(exit) printf("[Main] END\n");
> int x = MyDLL_Test();
> printf("[Main] x: %d\n", x);
> printf("[Main] Finished\n");
> }
> ```
>
> DLL compilation command line: `dmd -m64 -ofmydll.dll -L/DLL
> mydll.d mydll.def`
> Main command line: `rdmd -m64 main.d`
>
> And upon running, the output I receive is:
> ```
> [Main] Start
> [Main] x: 5
> [Main] Finished
> [Main] END
> [dll] DLL_PROCESS_ATTACH
> [dll] static this for mydll
> [dll] MyDLL_Test
> [dll] DLL_PROCESS_DETACH
> [dll] static ~this for mydll
> ```
>
> I would expect the first three lines of dll output to precede
> the "[Main] x:" line at least. Is there something I'm doing
> wrong? Do I need to somehow pass a reference to the main stdio
> to the DLL's D runtime similar to how the GC can be shared?
Hi, I use this function:
// Function echo()
void echo(T)(T text) nothrow {try {spawnShell("echo%s
%s".format(text.to!string.strip() != "" ? "" : ".",
text.to!string.replace(">","^>").replace("&","^&").replace("\n","
&&echo."))); } catch(Throwable){} }
echo("This is not will wait the end to print.");
More information about the Digitalmars-d-learn
mailing list