Delphi将数组变体转换为String

时间:2012-04-25 11:34:41

标签: delphi

在我的delphi程序中,我与web服务进行通信。

在WSDL导入中,我创建了以下函数 -

    function  call(const sessionId: WideString; const resourcePath: WideString;
 const args: Variant): Variant; stdcall;

现在当我在我的代码中调用它时,我想看看返回值是什么

var
p:WideString;
v:variant;
VarArray:variant;
begin

   //varArray := VarArrayCreate([0,50],varVariant);

   v := (httprio1 as  Mtest).call(a,'giftgit.gift', '28184');


   //p := VarArrayget(V,[1] ) ;
   p := vartostr(v[1]);

我在最后一行收到此错误

无法将类型(数组变体)的变体转换为类型(字符串)

我尝试使用varArray但问题相同。 我怎么解决这个问题?

谢谢

1 个答案:

答案 0 :(得分:6)

错误表明V [1]是变体数组。您可以检查确实是VarIsArray(V[1])

我有几种方法可以帮助您显示它:

  TPsVarDataType = (
    vdtDefault, // varEmpty
    vdtNull,
    vdtSmallint,
    vdtInteger,
    vdtSingle,
    vdtDouble,
    vdtCurrency,
    vdtDate,
    vdtOleStr,  // varOleStr, varString, varUString
    vdtDispatch,
    vdtError,
    vdtBoolean,
    vdtVariant,
    vdtUnknown,
    vdtDecimal,
    vdtUndefined,
    vdtShortint,
    vdtByte,
    vdtWord,
    vdtLongWord,
    vdtInt64,
  {$IFDEF D2009_OR_LATER}
    vdtUInt64,
  {$ENDIF}
    vdtUnsupported
    {Usupported types
    VT_UI8  = 21,
    VT_INT  = 22,
    VT_UINT = 23,
    VT_VOID = 24,
    VT_HRESULT  = 25,
    VT_PTR  = 26,
    VT_SAFEARRAY    = 27,
    VT_CARRAY   = 28,
    VT_USERDEFINED  = 29,
    VT_LPSTR    = 30,
    VT_LPWSTR   = 31,
    VT_RECORD   = 36,
    VT_FILETIME = 64,
    VT_BLOB = 65,
    VT_STREAM   = 66,
    VT_STORAGE  = 67,
    VT_STREAMED_OBJECT  = 68,
    VT_STORED_OBJECT    = 69,
    VT_BLOB_OBJECT  = 70,
    VT_CF   = 71,
    VT_CLSID    = 72,
    VT_VERSIONED_STREAM = 73,
    VT_BSTR_BLOB    = 0xfff,
    VT_VECTOR   = 0x1000,
    VT_ARRAY    = 0x2000,
    VT_BYREF    = 0x4000,
    VT_RESERVED = 0x8000,
    VT_ILLEGAL  = 0xffff,
    VT_ILLEGALMASKED    = 0xfff,
    VT_TYPEMASK = 0xfff
    }
  );

const
  SPsVarDataTypes: array [TPsVarDataType] of string  = (
    'Default',
    'Null',
    'Smallint',
    'Integer',
    'Single',
    'Double',
    'Currency',
    'Date',
    'OleStr',
    'Dispatch',
    'Error',
    'Boolean',
    'Variant',
    'Unknown',
    'Decimal',
    'Undefined',
    'Shortint',
    'Byte',
    'Word',
    'LongWord',
    'Int64',
  {$IFDEF D2009_OR_LATER}
    'UInt64',
  {$ENDIF}
    'Unsupported'
  );

const
  SPsVarDataTypes: array [TPsVarDataType] of string  = (
    'Default',
    'Null',
    'Smallint',
    'Integer',
    'Single',
    'Double',
    'Currency',
    'Date',
    'OleStr',
    'Dispatch',
    'Error',
    'Boolean',
    'Variant',
    'Unknown',
    'Decimal',
    'Undefined',
    'Shortint',
    'Byte',
    'Word',
    'LongWord',
    'Int64',
  {$IFDEF D2009_OR_LATER}
    'UInt64',
  {$ENDIF}
    'Unsupported'
  );

  SPsVarArray = 'Array of %s';
  SPsVarByRef = '%s (Reference)';

function VariantToStr(V: Variant; IncludeType: Boolean = False): string;
begin
  if VarIsArray(V) then
    Result := VarArrayToStr(V)
  else
    case VarType(V) of
      varError:
        Result := Format('Error($%x)', [TVarData(v).VError]);
      varNull:
        Result := '#NULL';
      varEmpty:
        Result := '#EMPTY';
      varDate:
        Result := FormatDateTime(ShortDateFormat + ' ' + LongTimeFormat + '.zzz', V)
    else
      Result := VarToStr(V);
    end;
  if IncludeType then
    Result := Format('%s{%s}', [Result, VarTypeToString(VarType(V))]);
end;


function VarArrayToStr(v: Variant; Delimiter: Char = #0; LineDelimiter: Char =
    #13): string;
var
  i,j,d: Integer;
  line: string;
begin
  if VarIsArray(v) then
  begin
    if Delimiter = #0 then
      Delimiter := ListSeparator;
    if LineDelimiter = #0 then
      LineDelimiter := ListSeparator;
    d := VarArrayDimCount(v);
    // The elements
    case d of
      1:
      begin
        Result := '';
        for i := VarArrayLowBound(v,1) to VarArrayHighBound(v,1) do
          Result := Result + VariantToStr(v[i]) + Delimiter;
        if Length(Result) > 0 then
          SetLength(Result, Length(Result)-1);
      end;
      2:
      begin
        Result := '';
        if (VarArrayLowBound(v,1) <= VarArrayHighBound(v,1)) and
          (VarArrayLowBound(v,2) <= VarArrayHighBound(v,2)) then
        begin
          for i := VarArrayLowBound(v,1) to VarArrayHighBound(v,1) do
          begin
            line := '';
            for j := VarArrayLowBound(v,2) to VarArrayHighBound(v,2) do
              line := line + VariantToStr(v[i,j]) + Delimiter;
            if Length(Result) > 0 then
              SetLength(line, Length(line)-1);
            Result := Result + LineDelimiter + Format('[%s]', [line]);
          end;
        end;
      end // 2
    else
      Result := 'Array Dim=' + IntToStr(d);
    end;
    Result := Format('[%s]', [Result]);
  end
  else
    Result := VarToStr(v);
end;


function VarTypeToString(varType: Integer): string;
var
  T: TPsVarDataType;
begin
  if varType = varString then
    Result := 'string'
  else
  begin
    T := VarTypeToPsVarDataType(varType);
    if T = vdtUnSupported then
      Result := IntToStr(varType and varTypeMask)
    else
      Result := SPsVarDataTypes[T];
  end;
  if (varType and varArray) <> 0 then
    Result := Format(SPsVarArray,[Result]);

  if (varType and varByRef) <> 0  then
    Result := Format(SPsVarByRef,[Result]);
end;


function VarTypeToPsVarDataType(Value: Word): TPsVarDataType;
var
  v: Word;
begin
  debugEnter('VarTypeToPsVarDataType');
  v := Value and varTypeMask;
  debugInt('Value', v);
  if v < Word(vdtUnsupported) then
    result := TPsVarDataType(v)
  else if v = varString then
    result := vdtOleStr
  {$IFDEF D2009_OR_LATER}
  else if v = varUString then
    result := vdtOleStr
  {$ENDIF}
  else
    result := vdtUnsupported;
  debugLeave;
end;

function PsVarDataTypeToVarType(Value: TPsVarDataType; IsArray: Boolean =
    False): Word;
begin
  if Value = vdtUnsupported then
    Result := varEmpty
  else
  begin
    Result := Word(Value);
    if IsArray then
      Result := Result or varArray;
  end;
end;

更新2012-09-20:好像我错过了字符串常量。添加。 更新2012-11-02:TPsVarDataType,VarTypeToPsVarDataType()&amp; VarTypeToPsVarDataType();