DLL and string reference parameters

Help
Mia
2008-06-23
2013-05-23
  • Mia

    Mia - 2008-06-23

    Hi there

    I have a DLL that uses fastmm which will be called from Delphi, VB and VB.net applications.

    I pass some String parameters to the DLL as reference paramaters. I then read in some Strings from am a socket connection (using Indy components' readln). The idea is then to populate the reference parameters with the newly read-in values (by casting to PChar) before exiting the DLL.

    I get the expected results but I have seen some funny characters every now and again which makes me think that the "expected results" I am getting might just be lucky results :) Please shed some light on my misgivings.

    Is FastMM able to pass Strings/PChar back as reference parameters?
    And how will the calling application know what the length of the string is to avoid overflow problems?

     
    • photon wang

      photon wang - 2009-08-14

      TTest = record
         a:string;
         b:integer;// b = length(a);
      end;

       
    • Muetze1

      Muetze1 - 2009-08-14

      Keep in mind that temporary PChar() casts on local variable loose their validity on leaving the routine containing the local variable. The memory (reference) of the local variable is on the stack and the references string/memory has to be freed. This implies that the pointer result of the temporary PChar cast points to a freed memory block. If the block is not used after freeing you can be lucky to read the old data using the PChar.

       

Log in to post a comment.