A little bit of the userdecls problem solved, but

Blitz3D Forums/Blitz3D Programming/A little bit of the userdecls problem solved, but

ChrML(Posted 2004) [#1]
Ok, I've solved how to get Delphi to return strings to BB. I did that this way, by returning the pointer to the first character in the string (ergo, null terminated):

library Project1;

uses
  SysUtils, Classes, Dialogs;

{$R *.res}

function Func_ShowMessage:pointer;
var
  temp: string;
begin
  temp := 'return string test';
  Func_Showmessage := addr(temp[1]);
end;

exports
  Func_ShowMessage;

begin
end.


As you now can see, returning the first char in the string works, because ordinary delphi strings are also terminated by null. The only difference is that the first byte in a string tells the length, and that's what we avoid when returning the pointer to the second. As we also know, the documentation tells us this:

Strings are passed and returned in 'C' format - ie: a pointer to a null-terminated sequence of
characters.


This also means that the strings passed to the Delphi DLL as a parameter also is a null-terminated string (pchar). I've tried all these solutions, but no luck:

library Project1;

uses
  SysUtils, Classes, Dialogs;

{$R *.res}

function Func_ShowMessage(s:pchar):pointer;
var
  temp: string;
begin
  showmessage(s);           //showmessage is compatible with pchars
  temp := 'return string test';
  Func_Showmessage := addr(temp[1]);
end;

exports
  Func_ShowMessage;

begin
end.


Also here I tried adding one by one char to the string, by reading the memory from the pointer BB passed:

library Project1;

uses
  SysUtils, Classes, Dialogs;

{$R *.res}

function Func_ShowMessage(s:pointer):pointer;
var
  temp: string;
  temp2: string;
begin
  temp2 := '';
  while s^ <> #0 do
  begin
    temp2 := temp2 + s^;
    inc(s);
  end;

  showmessage(temp2);
  temp := 'return string test';
  Func_Showmessage := addr(temp[1]);
end;

exports
  Func_ShowMessage;

begin
end.


Didn't work. Then I tried using Delphi's own pchar, and then convert it to string using Delphi's own "pointer to string" function:

library Project1;

uses
  SysUtils, Classes, Dialogs;

{$R *.res}

function Func_ShowMessage(s:pchar):pointer;
var
  temp: string;
  temp2: string;
begin
  temp2 := pchar(s);
  showmessage(temp2);
  temp := 'return string test';
  Func_Showmessage := addr(temp[1]);
end;

exports
  Func_ShowMessage;

begin
end.


Not working. It does show the message, but it shows just weird characters as if the pointer returned by BB is wrong. In all 3 ways to do it, it shows the same weird characters. Here's my userdecls:

.lib "Project1.dll"

Func_ShowMessage$(caption$)


Any suggestions, or experiences with this?