Search code examples
c++dllinno-setuppascalscript

How to return a string from a DLL to Inno Setup?


I need to return a string value to the calling Inno Setup script. Problem is I can't find a way to manage the allocated memory. If I allocate on the DLL side, I don't have anything to deallocate with on the script side. I can't use an output parameter, because there is no allocation function in the Pascal Script either. What should I do?


Solution

  • Here is a sample code of how to allocate a string that returns from a DLL:

    [Code]
    Function GetClassNameA(hWnd: Integer; lpClassName: PChar; nMaxCount: Integer): Integer; 
    External 'GetClassNameA@User32.dll StdCall';
    
    function GetClassName(hWnd: Integer): string;
    var
      ClassName: String;
      Ret: Integer;
    begin
      { allocate enough memory (pascal script will deallocate the string) }
      SetLength(ClassName, 256); 
      { the DLL returns the number of characters copied to the buffer }
      Ret := GetClassNameA(hWnd, PChar(ClassName), 256); 
      { adjust new size }
      Result := Copy(ClassName, 1 , Ret);
    end;