delphi programming forums mysql charset mget recursive synonimos
free ventrilo servers hosting cs javascript delay python find in list
Back Forum New
abstract:

how would I return a string in a friendly,cross-language way to my main .Exe application?


I have a String array in my dll library and I also have a function which returns a String value based on the index of that array:
Code:
  1. function getValue(index: Integer){
  2. return myArray[index];
  3. }
Copy Code
how would I return a string in a friendly,cross-language way to my main .Exe application?

TOP

You need to change it from a function into a procedure.
Typically the procedure should have two parameters
a Pchar (pointer to some memory that the EXECUTABLE has allocated!) and an integer indicating the size of the memory allocated.
Your procedure should copy the string to the pChar ensuring that the size of the string,
including the terminating null, is no greater than the "size" value passed in.
It should be the responsibility of the executable to allocate and dispose of the memory for its copy of the string.
Clive

TOP

what am I doing wrong  
Code:
  1. [b]EXE[/b]
  2. procedure TForm1.Button1Click(Sender: TObject);
  3. var
  4. Result : PChar;
  5. begin
  6.   getURL(46, Result);
  7.   ShowMessage(String(Result));
  8. end;
  9. [b]DLL[/b]
  10. procedure getURL(const index: Integer; Result: PChar);
  11. begin
  12.     result := AllocMem(Length(myArray[index]));
  13.     StrCopy(result,Addr(myArray[index]));
  14. end;
Copy Code
it returns me some weird characters: ⹡˥{Ljʺ

TOP

First, I said you needed to allocate and free the memory in the EXE not in the DLL.
Second, I would suggest you do not use Result as the name of your parameter as it has a specific meaning in Delphi
as the return value of a function.
This is untested but should be nearer to what you want:
Code:
  1. [b]EXE[/b]
  2. procedure TForm1.Button1Click(Sender: TObject);
  3. var
  4. RetVal : PChar;
  5. arrayIndexReq: Integer;
  6. begin
  7. retVal := AllocMem(46);
  8. arrayIndexReq := 2;  // Or whatever
  9. try
  10.   getURL(46,arrayIndexReq, retVal);
  11.   ShowMessage(String(retVal));
  12. finally
  13.     FreeMem(retVal);
  14. end;
  15. end;
  16. [b]DLL[/b]
  17. procedure getURL(const bufSize, index: Integer; msg: PChar);
  18. begin
  19.     StrPLCopy(msg,myArray[index],bufSize);
  20. end;
Copy Code
Of course, in the real world you will need a lot of additional code
to make sure you do not get AV's or buffer overflows or "index out of bounds".
Also a way of knowing how large the string was.
One way is to make the procedure a function again
But the function returns the length of the string.
Just like many Windows API's you could add code in the DLL function so that if you pass in Nil for msg the it simply returns
the size of the string at array[index] after which you can
set everything up for the second call with msg allocated.
WARNING: Always make sure you allocate enough memory for the terminating null!
Clive

TOP

abstract:

how would I return a string in a friendly,cross-language way to my main .Exe application?


im confused  
why did you add extra parameter?
getURL(46,arrayIndexReq, retVal);
oh and :
Code:
  1. StrPLCopy(msg,myArray[index],bufSize);
Copy Code
since the function and myarray are in the same dll file, I know exactly how much memory i need to allocate
Code:
  1. StrPLCopy(msg,myArray[index],[b]13 characters[/b]);
Copy Code
^ no?

TOP


Originally Posted by Athlon1600
im confused  
why did you add extra parameter?
Remember, I don't have access to your project!
I am assuming you need to pass in three things:
1. The size of the buffer you have allocated.
2. A pointer to the buffer you allocated.
3. The index of the array that you want returned.
oh and :
Code:
  1. StrPLCopy(msg,myArray[index],bufSize);
Copy Code
since the function and myarray are in the same dll file, I know exactly how much memory i need to allocate
Code:
  1. StrPLCopy(msg,myArray[index],[b]13 characters[/b]);
Copy Code
^ no?
NO!!!
Read what I keep telling you again
The memory is allocated IN THE EXE!!!
You have to tell the DLL how big that is so that the DLL does not overflow the allocated buffer.
Clive

TOP

aah I got it:
Code:
  1. [b]EXE[/b]
  2. var
  3.   RetVal : PChar;
  4.   arrayIndex: Integer;
  5. begin
  6.   retVal := AllocMem(1);
  7.   arrayIndex := 2;
  8.   try
  9.     getWorldURL(sizeof(retVal), retVal, arrayIndex);
  10.     ShowMessage(String(retVal));
  11.   finally
  12.     FreeMem(retVal);
  13.   end;
  14. [b]DLL[/b]
  15.   procedure getWorldURL(const bufsize: Integer; msg: PChar; const index: Integer);
  16.   begin
  17.     StrPLCopy(msg,WorldList[index],bufsize);
  18.   end;
Copy Code
works good. all my String in an array are 12-15 characters long but AllocMem(15) is not enough, I tried 20-25 still nothing. It's asking for 36 or the application just closes out without warning? So can I know how much memory I would need?
by the way, how is this strategy called? I want to look it up on google and learn more

TOP


  Thank you for your help
by the way, how is this strategy called? I want to look it up on google and learn more
Glad it helped.
I don't know a name for this; but if you give Google some keywords like these you ought to get some information:
passing strings EXE DLL

TOP

ok got it
one more problem though, I just edited  

TOP

abstract:

how would I return a string in a friendly,cross-language way to my main .Exe application?



  all my String in an array are 12-15 characters long but AllocMem(15) is not enough, I tried 20-25 still nothing. It's asking for 36 or the application just closes out without warning? So can I know how much memory I would need?
No way to know without seeing your actual code and string text.
Are you using Unicode characters?
Also sizeOf(retVal) is wrong.
That will always return 4 (in 32 bit windows) which is the size of
a pChar. You need to pass in the amount of memory you allocated in bytes.
Also:
So can I know how much memory I would need?
I mentioned how to do that earlier.
Re-read where I suggested passing in "nil".
Clive

TOP

Back Forum