Search code examples
inno-setuppascalscript

How can I retrieve the computer name with my Inno pascal script?


I've been having some difficulty with my inno pascal script. I familiarized myself a bit (just a bit) with Pascal, but its becoming more clear to me now that pascal script may be very different.

The following code compiles fine with freepascal:

program test;

uses windows;

function GetPCName: string;
var
  buffer: array[0..MAX_COMPUTERNAME_LENGTH + 1] of Char;
  Size: Cardinal;
begin
  Size := MAX_COMPUTERNAME_LENGTH + 1;
  Windows.GetComputerName(@buffer, Size);
  Result := StrPas(buffer);
end;

begin
   writeln('ComputerName: ' + (GetPCName));
   readln;
end.

How can I do something like this in my inno [code] section? I tried the above, but it doesn't seem to like "uses windows;"


Solution

  • First off, you can use the builtin GetComputerNameString support function.


    You can declare a function in the code section to be able to call library functions. Something like the below should work:

    [Code]
    function GetComputerName(lpBuffer: AnsiString; var nSize: DWORD): BOOL;
    external 'GetComputerNameA@kernel32.dll';
    
    function GetPCName: string;
    var
      Size: Cardinal;
      buffer: AnsiString;
    begin
      Size := MAX_COMPUTERNAME_LENGTH + 1;
      SetLength(buffer, Size);
      GetComputerName(buffer, Size);
      Result := buffer;
    end;
    


    See 'codedll.iss' in the examples folder of inno setup.