Re: How to call a function from a dll created with d ?
On Saturday, 2 July 2022 at 20:43:41 UTC, Vinod KC wrote: On Saturday, 2 July 2022 at 14:32:11 UTC, apz28 wrote: dmd -of=dimedll.dll dimedll.d dimedll.def dmd dime.d dimedll.di Thanks for the reply. Well, I am sorry to say that your suggestions resulted in failure. First of all, when I used this command -- ` dmd -of=dimedll.dll dimedll.d dimedll.def` I got this error message- `Error: unrecognized file extension dll`. So I avoided the `-of=dimedll.dll` part. Then I compiled it with this command - `dmd -H dimedll.d dimedll.def` And I got some warnings. Here are they. ```d dimedll.def(2) : warning LNK4017: EXETYPE statement not supported for the target platform; ignored dimedll.def(3) : warning LNK4017: SUBSYSTEM statement not supported for the target platform; ignored dimedll.def(4) : warning LNK4017: CODE statement not supported for the target platform; ignored dimedll.def(4) : warning LNK4017: DATA statement not supported for the target platform; ignored Creating library dimedll.lib and object dimedll.exp ``` I know all of them are from my `def` file. Anyways, I stepped forward and tried to run the main file with this dll & lib. So I ran this command. - `dmd dime.d dimedll.di`. But I got this error message. ```d dime.obj : error LNK2001: unresolved external symbol __D7dimedll12__ModuleInfoZ dime.exe : fatal error LNK1120: 1 unresolved externals Error: linker exited with status 1120 ``` First issue - you're using dmd on windows. Dmd gives me errors about the ModuleInfo, while LDC doesn't. [This is the LDC download link.](https://github.com/ldc-developers/ldc/releases/download/v1.29.0/ldc2-1.29.0-windows-multilib.exe) Next, change dimedll.d to the following: ```d module dimedll; export void testFunc() { import std.stdio; writeln("Lets build our own ime."); } ``` and dime.d to the following: ```d import dimedll; //In case you ever write more functions for dimedll.d; pragma(lib, "dimedll.lib"); void main() { testFunc(); } ``` Then run ```ldc2 -shared dimedll.d``` and right after that ```ldc2 dime.d```. If I didn't make a mistake in writing this (I tested it on my own system), it should output a working program that prints the expected output when ran.
Re: Background thread, async and GUI (dlangui)
On 7/6/22 16:17, Ali Çehreli wrote: > I would consider std.parallelism And it looks more natural with a std.parallelism.Task: struct Progress { size_t percent_; void set(size_t downloaded, size_t total) { if (total != 0) { import core.atomic: atomicStore; const value = cast(size_t)(float(downloaded) / float(total) * 100); atomicStore(percent_, value); } } size_t get() const { import core.atomic: atomicLoad; return atomicLoad(percent_); } } struct Request { string url; string result; Progress progress; } void download(Request * request) { import std.net.curl: HTTP; auto http = HTTP(request.url); http.onProgress((size_t dl, size_t dln, size_t ul, size_t uln) { if (dl != 0) { request.progress.set(dln, dl); } return 0; }); http.onReceive((ubyte[] data) { request.result ~= (cast(char[])data); return data.length; }); http.perform(); } void main() { import std.parallelism : task; import std.stdio: writefln; auto request = Request("dlang.org"); auto downloadTask = task!download(); downloadTask.executeInNewThread; foreach (i; 0 .. 10) { writefln!"Doing work on the side (%s)"(i); writefln!"Checking download progress: %s%%"(request.progress.get()); import core.thread; Thread.sleep(100.msecs); } // Now we need the result before continuing: downloadTask.yieldForce(); writefln!"Downloaded %s bytes:\n%s"(request.result.length, request.result); } Ali
Re: How to call a function from a dll created with d ?
On Sunday, 3 July 2022 at 09:43:20 UTC, frame wrote: app.d: ```d module app; import dimedll; import std.stdio; import std.stdio : log = writeln; pragma(lib, "dimedll.lib"); void main() { log("Lets build our own ime"); testFunc(); } ``` You should be able to change contents in the DLL and run the executable wihtout re-compiling (the library file should be round ~2kB). PS: ddemangle just waits for your input. You copy in the mangled symbol like `__D7dimedll12__ModuleInfoZ` and press enter ;-) Does importing dimedll into app.d properly NOT link in the functions that are exported to the DLL? When I tried something similar with dmd, I had to create a .di file containing just stubs, otherwise it looked like it was ignoring the DLL and compiling in an additional copy of each fuction.