title: Excel 2013 (64bit) VBA String + dll
body: <p>I've got a 64bit Delphi (XE4) dll. I call it from Excel VBA. </p>
  
  <p>I use the following trick : <a href="http://www.devx.com/tips/Tip/37587" rel="nofollow">http://www.devx.com/tips/Tip/37587</a></p>
  
  <p>It works for 32bit and 64bit excel-2010, but not with excel-2013</p>
  
  <pre><code>StrDataSizePtr^:=Length(tmpStr);//Access Violation here
  </code></pre>
  
  <p>What can be the problem? Does excel-2013 vba has new String format?    </p>
  
  <p>Thank You!</p>
  
  <p>EDIT:</p>
  
  <p>Delphi</p>
  
  <pre><code>{$IFDEF WIN64}
  TPtrLong = UInt64;
  {$ELSE}
  TPtrLong = Longword;
  {$ENDIF}
  
  procedure StrToVBAStr(Str : String;VAR VBAStr : PAnsiChar);
  VAR
    VBAStrPtr : TPtrLong absolute VBAStr;
    ResStrSizePtr : PLongword;
  begin
    if Length(Str)&gt;Integer(StrLen(VBAStr))
    then raise Exception.Create('StrToVBAStr :     '+IntToStr(Length(Str))+'&gt;'+IntToStr(StrLen(VBAStr)));
  
    ResStrSizePtr:=Ptr(VBAStrPtr-4);//this points to VBA String size
    VBAStr:=StrPLCopy(VBAStr,Str,Length(Str));//copy to VBAStr-be
    ResStrSizePtr^:=Length(Str);//set VBAStr length
  end;
  
  function GetLastError(VAR Error : PAnsiChar) : Longint; stdcall;
  VAR
    sError : String;
  begin
    TRY
      Result := _GetLastError(sError);
      StrToVBAStr(sError, Error);
    EXCEPT
      Result := -1;
    END;
  end;
  </code></pre>
  
  <p>VBA</p>
  
  <pre><code>Private Declare PtrSafe Function XLDLL_GetLastErrorA Lib "XL.dll" Alias "GetLastError" ( _
  ByRef Result As String) As Long
  
  Public Sub XLDLL_Error(Optional ByVal Source As String = "")
    Dim XLErr As String
  
    XLErr = Space(1001)
    If XLDLL_GetLastErrorA(XLErr) &lt;&gt; -1 Then
      XL_LastError = XLErr
      If XL_LastError &lt;&gt; "" Then
        Err.Raise vbObjectError + 1000, Source, XL_LastError
      End If
    Else
      Err.Raise vbObjectError + 1000, "XLDLL_Hiba", "XLDLL_GetLastErrorA hiba"
    End If
  End Sub
  </code></pre>