What exactly gets returned with extern(C) export string func() ?

cc cc at nevernet.com
Wed Jun 16 02:46:36 UTC 2021


On Sunday, 13 June 2021 at 21:13:33 UTC, frame wrote:
> On Sunday, 13 June 2021 at 10:02:45 UTC, cc wrote:
>
>> it seems to work as expected with the same C# code.  Does D 
>> explicitly disallow slices as an extern(C) export parameter 
>> type?
>
> The spec says that there is no equivalent to type[]. You get a 
> type* instead.

I can't seem to get it to work as a return type, but 
interestingly it does work as an out/pass by ref parameter.

D:
```d
export void D_testString(out string ret) {
	ret = "hello".idup;
}
```

C#:
```c#
public struct DString {
	public ulong length;
	public IntPtr ptr;
	public string str {
		get {
			byte[] b = new byte[length];
			for (int i = 0; i < (int)length; i++) {
				b[i] = Marshal.ReadByte(ptr, i);
			}
			return Encoding.UTF8.GetString(b);
		}
	}
}
[DllImport("test.dll")]
private static extern void D_testString(out DString dd);
public static string testString() {
	DString d;
	D_testString(out d);
	return d.str;
}
```


More information about the Digitalmars-d-learn mailing list