Adventures in calling Nim DLLs from Delphi again... If I use the following code
I can get the string on the Delphi side using PAnsiChar as expected:
proc GetString*(): cstring {.cdecl, exportc, dynlib.} =
var cs: cstring
cs = "String1"
return cs
Run
Generated C code looks like:
N_LIB_EXPORT N_CDECL(NCSTRING, GetStringList)(void) {
NCSTRING result;
NCSTRING cs;
{ result = (NCSTRING)0;
cs = (NCSTRING)0;
cs = "String1";
result = cs;
goto BeforeRet_;
}BeforeRet_: ;
return result;
Run
However if I use the following I get an exception:
proc GetStringList*(): cstring {.cdecl, exportc, dynlib.} =
var s: string
s = "String1"
return cstring(s)
Run
Not surprisingly the C code is different:
STRING_LITERAL(TM_OCIjdUBZYh7z3CKkm9az9a8Q_2, "String1", 7);
N_LIB_EXPORT N_CDECL(NCSTRING, GetStringList)(void) {
NCSTRING result;
NimStringDesc* s;
{ result = (NCSTRING)0;
s = (NimStringDesc*)0;
s = copyString(((NimStringDesc*) &TM_OCIjdUBZYh7z3CKkm9az9a8Q_2));
result = nimToCStringConv(s);
goto BeforeRet_;
}BeforeRet_: ;
return result;
}
Run
Anyone got any idea why these two cstrings appear to have a different
structure? I'm using 0.19 and am aware of the possible need to use GC_ref
(although that's not the issue here). Thanks.