I've fallen into various traps with array of const before. I would always recommend that:

1. Test the vType before accessing the argument value and raise an exception if it is not what is expected. You are dealing with a free union and the compiler may not always do what you expect it to.

2. Values such as VAnsiString are best dealt with as PChars as otherwise you can quickly get into problems with reference counts.

For example:

program test_args;

procedure test(name: string; args: array of const);
begin
  writeln(name);

  case args[0].vType of

  vtAnsiString:
    writeln(strpas(PAnsiChar(args[0].VAnsiString)));
  else
    raise Exception.Create('Unexpected vtype');
  end;

end;

begin
  test('name', ['arg']);
end.

Use of "strpas" is probably unnecessary in the above, but it does illustrate the point.


On 28/10/17 23:59, Darius Blaszyk wrote:
Consider the application below. When I run it I do get the following output:

name
rg��������name�F&{00000000-0000-0000-C000-000000000046}

In other words I lose the first character (a) from the arguments supplied and the string returns with a lot of garbage. What am I doing wrong here?

Rgds, Darius

program test_args;

procedure test(name: string; args: array of const);
begin
  writeln(name);
  writeln(args[0].VString^);
end;

begin
  test('name', ['arg']);
end.


_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to