title: Delphi - Accessing data from dynamic array that is populated from an untyped Pointer
body: <p>I'm using <strong>Delphi 2009</strong> not that it has a large affect on what I'm doing. I think I would run into the same if I was still on <strong>2007</strong>.</p>
  
  <p>I have a scsi call that outputs data to a pointer (wrong way of looking at it but i have trouble explaining that).</p>
  
  <p>Originally I used <strong>Move</strong> to populate a <strong>Static Array of Byte</strong> with the data that came back, but I'd like to switch to a <strong>Dynamic Array</strong> to which the length of is known at the time of the call.  I've tried several things with varied results some get the data but have mad access violations others have no errors but get invalid data.</p>
  
  <p>Adding <strong>setlength</strong> to the array and then using <strong>move</strong>, causes first to have an empty array of set length and then second not be able to access the data via like <strong>OutputData[0]</strong> like I did when it was static, in the debugger after the move everything shows as innaccesable value or whatever.</p>
  
  <p>Below is something I tried after reading an article that did the oposit took a dynamic array and gave a pointer that address.  It mentioned making mistakes like orphaning data.</p>
  
  <pre><code>var
    Output: Pointer;
    OutputData: Array of byte;
    I: Integer;
  begin
  GetMem(Output, OutputLength.Value);
  if SendPSPQuery(Char(DriveLetter[1]), cbxQuery.Items.IndexOf(cbxQuery.Text), Output, OutputLength.Value) = 0 then
    begin
      OutputData := @Output;
      for I := 0 to OutputLength.Value - 1 do
      begin
        edtString.Text := edtString.Text + Char(OutputData[I]);
      end;
  </code></pre>
  
  <p>There is various otherstuff that th eoutput data is used for it gets put out in string and hex and things.</p>
  
  <p>Anyway, how can I Take a Pointer put that data into a dynamic array and then grab that data the way you would address an array.</p>
  
  <p>Thanks.</p>