title: Is COM broken in XE2, and how might I work around it?
body: <p><strong>Update:</strong> XE2 Update 2 fixes the bug described below.</p>
  
  <p>The program below, cutdown from the real program, fails with an exception in XE2. This is a regression from 2010. I don't have XE to test on but I'd expect that the program works fine on XE (thanks to Primož for confirming that the code runs fine on XE).</p>
  
  <pre><code>program COMbug;
  
  {$APPTYPE CONSOLE}
  
  uses
    SysUtils, Variants, Windows, Excel2000;
  
  var
    Excel: TExcelApplication;
    Book: ExcelWorkbook;
    Sheet: ExcelWorksheet;
    UsedRange: ExcelRange;
    Row, Col: Integer;
    v: Variant;
  
  begin
    Excel := TExcelApplication.Create(nil);
    try
      Excel.Visible[LOCALE_USER_DEFAULT] := True;
      Book := Excel.Workbooks.Add(EmptyParam, LOCALE_USER_DEFAULT) as ExcelWorkbook;
      Sheet := Book.Worksheets.Add(EmptyParam, EmptyParam, 1, EmptyParam, LOCALE_USER_DEFAULT) as ExcelWorksheet;
  
      Sheet.Cells.Item[1,1].Value := 1.0;
      Sheet.Cells.Item[2,2].Value := 1.0;
      UsedRange := Sheet.UsedRange[LOCALE_USER_DEFAULT] as ExcelRange;
      for Row := 1 to UsedRange.Rows.Count do begin
        for Col := 1 to UsedRange.Columns.Count do begin
          v := UsedRange.Item[Row, Col].Value;
        end;
      end;
    finally
      Excel.Free;
    end;
  end.
  </code></pre>
  
  <p>In XE2 32 bit the error is:</p>
  
  <blockquote>
    <p>Project COMbug.exe raised exception class $C000001D with message 'system exception (code 0xc000001d) at 0x00dd6f3e'.</p>
  </blockquote>
  
  <p>The error occurs on the second execution of <code>UsedRange.Columns</code>.</p>
  
  <p>In XE2 64 bit the error is:</p>
  
  <blockquote>
    <p>Project COMbug.exe raised exception class $C0000005 with message 'c0000005 ACCESS_VIOLATION'</p>
  </blockquote>
  
  <p>Again, I think that the error occurs on the second execution of <code>UsedRange.Columns</code>, but the 64 bit debugger steps through the code in a slightly weird way so I'm not 100% sure of that.</p>
  
  <p>I have submitted a <a href="http://qc.embarcadero.com/wc/qcmain.aspx?d=100414" rel="noreferrer">QC report</a> for the issue.</p>
  
  <p>I looks very much to me as though something in the Delphi COM/automation/interface stack is comprehensively broken. This is a complete show-stopper for my XE2 adoption.</p>
  
  <p>Does anyone have any experience of this problem? Does anyone have any tips and advice as to how I might attempt to work around the problem? Debugging what's really going on here is outside my area of expertise.</p>