Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-19 Thread kinke via Digitalmars-d-learn

On Saturday, 19 May 2018 at 17:33:08 UTC, Robert M. Münch wrote:

On 2018-05-18 14:42:17 +, Adam D. Ruppe said:
A value struct return is actually done via a hidden pointer 
parameter (so the function can construct it in-place for the 
caller, a standard optimization), so it just shifted all the 
other arguments to the side, causing one of those 0's to be 
interpreted as the string.


[...]

Is this somehwere documented?


https://docs.microsoft.com/en-us/cpp/build/return-values-cpp:


Otherwise, the caller assumes the responsibility of allocating
memory and passing a pointer for the return value as the first
argument. Subsequent arguments are then shifted one argument
to the right.


Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-19 Thread Robert M. Münch via Digitalmars-d-learn

On 2018-05-18 14:42:17 +, Adam D. Ruppe said:


On Friday, 18 May 2018 at 14:06:11 UTC, Robert M. Münch wrote:
So, having a wrong return-type here, resulted in the const char *text 
parameter always being NULL. Not sure I understand the relation but 
looks strange to me... at least not very obvious.


A value struct return is actually done via a hidden pointer parameter 
(so the function can construct it in-place for the caller, a standard 
optimization), so it just shifted all the other arguments to the side, 
causing one of those 0's to be interpreted as the string.


Wow, thanks for the clear explanation. Without very deep internal 
knowhow I don't think anyone is able to ever guess this.


Is this somehwere documented?

--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-18 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 18 May 2018 at 14:06:11 UTC, Robert M. Münch wrote:
So, having a wrong return-type here, resulted in the const char 
*text parameter always being NULL. Not sure I understand the 
relation but looks strange to me... at least not very obvious.


A value struct return is actually done via a hidden pointer 
parameter (so the function can construct it in-place for the 
caller, a standard optimization), so it just shifted all the 
other arguments to the side, causing one of those 0's to be 
interpreted as the string.


Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-18 Thread Robert M. Münch via Digitalmars-d-learn

On 2018-05-16 17:46:59 +, Steven Schveighoffer said:


Well, for C see above on the D side:

extern(C) {
  result myfunc(double x, double y, const char *text, stuff 
*myStuff, bool measureOnly);

}


Shouldn't the result be a pointer?


Indeed. And you know what? That was causing the problem.

So, having a wrong return-type here, resulted in the const char *text 
parameter always being NULL. Not sure I understand the relation but 
looks strange to me... at least not very obvious.


--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster

Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-16 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/16/18 12:53 PM, Robert M. Münch wrote:

On 2018-05-16 15:09:41 +, rikki cattermole said:


On 17/05/2018 3:07 AM, Robert M. Münch wrote:

I have an extern(C) function in a DLL with this signature:

result* myfunc(double x, double y, const char *text, stuff *myStuff, 
bool flag);


I call it like this:

result = myfunc(0, 0, std.string.toStringz("1"), stuff, true);


The problem is, that on the DLL side *text is always NULL. I doesn't 
matter what I put as third argument. And even more strange, I have an 
other function with a const char* as first argument. There everything 
is working...


Any idea what cause this could be? This is really strange...


Please post the C function prototype.


Well, for C see above on the D side:

 extern(C) {
   result myfunc(double x, double y, const char *text, stuff 
*myStuff, bool measureOnly);

 }


Shouldn't the result be a pointer?

-Steve


Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-16 Thread Robert M. Münch via Digitalmars-d-learn

On 2018-05-16 15:09:41 +, rikki cattermole said:


On 17/05/2018 3:07 AM, Robert M. Münch wrote:

I have an extern(C) function in a DLL with this signature:

result* myfunc(double x, double y, const char *text, stuff *myStuff, 
bool flag);


I call it like this:

result = myfunc(0, 0, std.string.toStringz("1"), stuff, true);


The problem is, that on the DLL side *text is always NULL. I doesn't 
matter what I put as third argument. And even more strange, I have an 
other function with a const char* as first argument. There everything 
is working...


Any idea what cause this could be? This is really strange...


Please post the C function prototype.


Well, for C see above on the D side:

extern(C) {
  		result myfunc(double x, double y, const char *text, stuff 
*myStuff, bool measureOnly);

}

result is a:

   extern (C++, class) struct result {
 double w;
 double h;
   }



--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-16 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/16/18 11:07 AM, Robert M. Münch wrote:

I have an extern(C) function in a DLL with this signature:

result* myfunc(double x, double y, const char *text, stuff *myStuff, 
bool flag);


I call it like this:

result = myfunc(0, 0, std.string.toStringz("1"), stuff, true);


toStringz isn't necessary, string literals always are null terminated, 
and implicitly cast to const char *.


The problem is, that on the DLL side *text is always NULL. I doesn't 
matter what I put as third argument. And even more strange, I have an 
other function with a const char* as first argument. There everything is 
working...


Any idea what cause this could be? This is really strange...


It sounds very much like a mismatch of parameters on the prototype. 
Anything extern(C) has no mangling, and so you can specify the wrong 
parameters and it still compiles.


Double check the parameter types, and if possible, reduce to a simple 
compilable example that can be tested by others.


-Steve



Re: C API / const char *text / std.string.toStringz pointer is always NULL on C side

2018-05-16 Thread rikki cattermole via Digitalmars-d-learn

On 17/05/2018 3:07 AM, Robert M. Münch wrote:

I have an extern(C) function in a DLL with this signature:

result* myfunc(double x, double y, const char *text, stuff *myStuff, 
bool flag);


I call it like this:

result = myfunc(0, 0, std.string.toStringz("1"), stuff, true);


The problem is, that on the DLL side *text is always NULL. I doesn't 
matter what I put as third argument. And even more strange, I have an 
other function with a const char* as first argument. There everything is 
working...


Any idea what cause this could be? This is really strange...


Please post the C function prototype.