Re: Calling C functions that modify a string

2023-06-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/15/23 2:21 PM, bachmeier wrote: On Thursday, 15 June 2023 at 15:53:57 UTC, Steven Schveighoffer wrote: On 6/15/23 10:04 AM, Jonathan M Davis wrote: On Thursday, June 15, 2023 7:18:06 AM MDT Steven Schveighoffer via Digitalmars-d-learn wrote: But in general, if you want a mutable character

Re: Calling C functions that modify a string

2023-06-15 Thread bachmeier via Digitalmars-d-learn
On Thursday, 15 June 2023 at 15:53:57 UTC, Steven Schveighoffer wrote: On 6/15/23 10:04 AM, Jonathan M Davis wrote: On Thursday, June 15, 2023 7:18:06 AM MDT Steven Schveighoffer via Digitalmars-d-learn wrote: But in general, if you want a mutable character array that's zero terminated, you ne

Re: Calling C functions that modify a string

2023-06-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/15/23 10:04 AM, Jonathan M Davis wrote: On Thursday, June 15, 2023 7:18:06 AM MDT Steven Schveighoffer via Digitalmars-d-learn wrote: But in general, if you want a mutable character array that's zero terminated, you need to make a copy with a zero terminator, but type it as mutable. I'm sur

Re: Calling C functions that modify a string

2023-06-15 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, June 15, 2023 7:18:06 AM MDT Steven Schveighoffer via Digitalmars-d-learn wrote: > But in general, if you want a mutable character array that's zero > terminated, you need to make a copy with a zero terminator, but type it > as mutable. I'm surprised there isn't a way to do this easil

Re: Calling C functions that modify a string

2023-06-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/15/23 9:18 AM, Steven Schveighoffer wrote: So interestingly enough, toStringz is pure, and returns an unrelated type, so you shouldn't need to cast. However, for some reason, it does require a cast. That seems like a bug to me. Oh wait, a pure function can return immutable data that isn't

Re: Calling C functions that modify a string

2023-06-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/14/23 11:29 PM, Pat Maddox wrote: Hi there, I want to call a C function that upcases a string. I have something working, I just want to check in here to see if there's a better approach that I'm missing. I ask because `std.string.toStringZ()` returns an `immutable char *`. As far as I ca

Calling C functions that modify a string

2023-06-14 Thread Pat Maddox via Digitalmars-d-learn
Hi there, I want to call a C function that upcases a string. I have something working, I just want to check in here to see if there's a better approach that I'm missing. I ask because `std.string.toStringZ()` returns an `immutable char *`. As far as I can tell, I have two options: 1. Make the

Re: Calling C functions

2020-06-30 Thread Jacob Carlborg via Digitalmars-d-learn
On Tuesday, 30 June 2020 at 12:22:15 UTC, Steven Schveighoffer wrote: (i.e. one cannot use extern(D) functions for C callbacks). I don't think that's a big issue. Honestly, I don't think it's an issue at all. BTW, the order of arguments is not the only thing. Variadic functions in D and C

Re: Calling C functions

2020-06-30 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/30/20 3:00 AM, Kagamin wrote: On Monday, 29 June 2020 at 19:55:59 UTC, Steven Schveighoffer wrote: Yep, for sure. I'll file an issue. Anyone know why the calling convention would differ? It's easier to enforce left to right evaluation order this way: arguments are pushed to stack as they

Re: Calling C functions

2020-06-30 Thread Kagamin via Digitalmars-d-learn
On Monday, 29 June 2020 at 19:55:59 UTC, Steven Schveighoffer wrote: Yep, for sure. I'll file an issue. Anyone know why the calling convention would differ? It's easier to enforce left to right evaluation order this way: arguments are pushed to stack as they are evaluated, which is pascal cal

Re: Calling C functions

2020-06-29 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/29/20 1:50 PM, Jacob Carlborg wrote: On Monday, 29 June 2020 at 16:34:33 UTC, Steven Schveighoffer wrote: Are you sure? On the ABI page [1] , it says "The extern (C) and extern (D) calling convention matches the C calling convention used by the supported C compiler on the host system."

Re: Calling C functions

2020-06-29 Thread Jacob Carlborg via Digitalmars-d-learn
On Monday, 29 June 2020 at 16:34:33 UTC, Steven Schveighoffer wrote: Are you sure? On the ABI page [1] , it says "The extern (C) and extern (D) calling convention matches the C calling convention used by the supported C compiler on the host system." In that case the documentation is wrong. H

Re: Calling C functions

2020-06-29 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/26/20 4:15 AM, Jacob Carlborg wrote: On Friday, 26 June 2020 at 00:30:22 UTC, Denis wrote: I have a two questions about calling C functions from D. (1) When passing a D callback to a C function, is there a way to write the code without having to prefix the callback declaration with

Re: Calling C functions

2020-06-26 Thread Jacob Carlborg via Digitalmars-d-learn
On 2020-06-26 18:54, Denis wrote: OK, now this makes sense. I tested calling the same callback function directly from D: it compiled and worked correctly. So at least prefixing the callback function with `extern(C)` doesn't prevent the rest of the D program from calling it too. No, of cours

Re: Calling C functions

2020-06-26 Thread Denis via Digitalmars-d-learn
On Friday, 26 June 2020 at 08:15:27 UTC, Jacob Carlborg wrote: On Friday, 26 June 2020 at 00:30:22 UTC, Denis wrote: extern(C) void cfunc(void function(int)); extern(C) void dcallback(int x) {...} <-- Why extern(C)? cfunc(&dcallback); Can this be rewritten, dropping the prefix f

Re: Calling C functions

2020-06-26 Thread Jacob Carlborg via Digitalmars-d-learn
On Friday, 26 June 2020 at 00:30:22 UTC, Denis wrote: I have a two questions about calling C functions from D. (1) When passing a D callback to a C function, is there a way to write the code without having to prefix the callback declaration with "extern(C)"? It's not a big

Calling C functions

2020-06-25 Thread Denis via Digitalmars-d-learn
I have a two questions about calling C functions from D. (1) When passing a D callback to a C function, is there a way to write the code without having to prefix the callback declaration with "extern(C)"? It's not a big deal adding the prefix to the D function declaration. It

Re: Calling C functions

2010-12-09 Thread CrypticMetaphor
On 12/9/2010 10:04 PM, Jesse Phillips wrote: CrypticMetaphor Wrote: It is a linker bug, so Oplink is at fault. What is the bug number you submitted? bug number: 5337 http://d.puremagic.com/issues/show_bug.cgi?id=5337

Re: Calling C functions

2010-12-09 Thread Adam Ruppe
I almost forgot I wrote that dtips page! Thanks for bringing it up. I just updated it to include a brief note on __gshared. The rest of it should still work, though. Calling C functions is pretty easy.

Re: Calling C functions

2010-12-09 Thread Jesse Phillips
CrypticMetaphor Wrote: > On 12/9/2010 5:28 PM, Steven Schveighoffer wrote: > > > Yes please, and be sure to specify that it correctly does not compile on > > linux. http://d.puremagic.com/issues/enter_bug.cgi > > > -Steve > > Alright then! > > I submitted a my first bug report and added __gshare

Re: Calling C functions

2010-12-09 Thread Steven Schveighoffer
On Thu, 09 Dec 2010 12:24:05 -0500, Andrej Mitrovic wrote: On 12/9/10, Steven Schveighoffer wrote: __gshared is unprotected sharing, and the type system is not aware that it is shared. Can you remember what specifically you were doing with the variable? My dev PC is in repairs right no

Re: Calling C functions

2010-12-09 Thread Andrej Mitrovic
On 12/9/10, Steven Schveighoffer wrote: > __gshared is unprotected sharing, and the type system is not aware that it > is shared. Can you remember what specifically you were doing with the > variable? My dev PC is in repairs right now so I don't have the code atm. It's a project that works with

Re: Calling C functions

2010-12-09 Thread CrypticMetaphor
On 12/9/2010 5:28 PM, Steven Schveighoffer wrote: On Thu, 09 Dec 2010 10:15:59 -0500, CrypticMetaphor wrote: On 12/9/2010 3:57 PM, Steven Schveighoffer wrote: On Thu, 09 Dec 2010 09:37:03 -0500, CrypticMetaphor wrote: I found this page that describes how to call c functions from D. I foun

Re: Calling C functions

2010-12-09 Thread Steven Schveighoffer
On Thu, 09 Dec 2010 11:38:00 -0500, Andrej Mitrovic wrote: I know I prefer using shared() when interfacing with C. I've tried using __gshared once when interfacing with C code. But I had crashes all the time, using shared instead made my app stable again. It might be related to the way the C

Re: Calling C functions

2010-12-09 Thread Andrej Mitrovic
I know I prefer using shared() when interfacing with C. I've tried using __gshared once when interfacing with C code. But I had crashes all the time, using shared instead made my app stable again. It might be related to the way the C code worked, since multiple threads were involved. Anyway.. that

Re: Calling C functions

2010-12-09 Thread Steven Schveighoffer
On Thu, 09 Dec 2010 10:15:59 -0500, CrypticMetaphor wrote: On 12/9/2010 3:57 PM, Steven Schveighoffer wrote: On Thu, 09 Dec 2010 09:37:03 -0500, CrypticMetaphor wrote: I found this page that describes how to call c functions from D. I found this page that describes how: http://arsdnet.ne

Re: Calling C functions

2010-12-09 Thread CrypticMetaphor
On 12/9/2010 3:57 PM, Steven Schveighoffer wrote: On Thu, 09 Dec 2010 09:37:03 -0500, CrypticMetaphor wrote: I found this page that describes how to call c functions from D. I found this page that describes how: http://arsdnet.net/dtips/#cfunc on that page he uses gcc, and I use dmc, but I g

Re: Calling C functions

2010-12-09 Thread Steven Schveighoffer
On Thu, 09 Dec 2010 09:37:03 -0500, CrypticMetaphor wrote: I found this page that describes how to call c functions from D. I found this page that describes how: http://arsdnet.net/dtips/#cfunc on that page he uses gcc, and I use dmc, but I get different results. This is what I did // c

Calling C functions

2010-12-09 Thread CrypticMetaphor
I found this page that describes how to call c functions from D. I found this page that describes how: http://arsdnet.net/dtips/#cfunc on that page he uses gcc, and I use dmc, but I get different results. This is what I did // cfile.c file extern int globalFromD; void functionFromC(int a) {