<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>I've fallen into various traps with array of const before. I
would always recommend that:</p>
<p>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.</p>
<p>2. Values such as VAnsiString are best dealt with as PChars as
otherwise you can quickly get into problems with reference counts.</p>
<p>For example:<br>
</p>
<p>program test_args;<br>
<br>
procedure test(name: string; args: array of const);<br>
begin<br>
writeln(name);</p>
<p> case args[0].vType of</p>
<p> vtAnsiString:<br>
writeln(strpas(PAnsiChar(args[0].VAnsiString)));<br>
else<br>
raise Exception.Create('Unexpected vtype');<br>
end;<br>
</p>
<p>end;<br>
<br>
begin<br>
test('name', ['arg']);<br>
end.</p>
<p>Use of "strpas" is probably unnecessary in the above, but it does
illustrate the point.<br>
</p>
<br>
<div class="moz-cite-prefix">On 28/10/17 23:59, Darius Blaszyk
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:d2ab2072dabb0740a9e40f0cdb6a6f4c@zeelandnet.nl">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Consider the application below. When I run it I do get the
following output:<br>
<br>
name<br>
rg��������name�F&{00000000-0000-0000-C000-000000000046}<br>
<br>
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?<br>
<br>
Rgds, Darius<br>
<br>
program test_args;<br>
<br>
procedure test(name: string; args: array of const);<br>
begin<br>
writeln(name);<br>
writeln(args[0].VString^);<br>
end;<br>
<br>
begin<br>
test('name', ['arg']);<br>
end.<br>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
fpc-pascal maillist - <a class="moz-txt-link-abbreviated" href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a>
<a class="moz-txt-link-freetext" href="http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal</a></pre>
</blockquote>
<br>
</body>
</html>