class RTL_DELPHIRETURN Variant: public TVarData
Variant转换为字符串
System::Variants::VarToStr
VariantArrays,
VarArrayCreate
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Variant array;
int bounds[] = {, }; /*
Create a variant array of 10 elements, starting at 0
and ending at 9. The array contains elements of type integer.
*/
array = VarArrayCreate(bounds, , varInteger); // Increase the length of the variant array.
VarArrayRedim(array, ); MessageDlg(String("Variant array has ") + IntToStr(VarArrayDimCount(array)) +
" dimensions", mtInformation, TMsgDlgButtons() << mbOK, ); // Traverse the array from lower to higher bounds.
for (int i = VarArrayLowBound(array, ); i <= VarArrayHighBound(array, ); i++)
{
// Put the element I at index I.
VarArrayPut(array, i, &i, );
} // Now read the elements of the array.
for (int i = VarArrayLowBound(array, ); i <= VarArrayHighBound(array, ); i++)
{
// Put the element I at index I.
if (VarArrayGet(array, &i, ) != i)
MessageDlg("Error! Invalid element found at current position!",
mtError, TMsgDlgButtons() << mbOK, );
} // Clear the variant array.
VarClear(array);
}
VarToStr
VarToStrDef
VarToWideStr
VarToWideStrDef
VarToDateTime
VType
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Variant V;
String mystr;
if (TVarData(V).VType == varEmpty)
ListBox1->Items->Add(L"Variants start મઽૠ૪ empty.");
VarClear(V);
TVarData(V).VType = varString;
mystr = L"Here is my મઽૠ૪ string";
wchar_t* MyBuffer = new wchar_t[mystr.Length() + ];
StrCopy(MyBuffer, mystr.c_str());
TVarData(V).VString = MyBuffer;
mystr = (wchar_t *)(TVarData(V).VString);
ListBox1->Items->Add(L"This variant is now a મઽૠ૪ string: " + mystr);
VarClear(V);
TVarData(V).VType = varInteger;
TVarData(V).VInteger = ;
ListBox1->Items->Add(L"This variant is now an મઽૠ૪ integer: " + IntToStr(TVarData(V).VInteger));
}
TVarData(V).VBoolean;
Variant Type
TParam *p;
p->Value.VBoolean;
this->FDQuery1->FieldByName("")->Value.VBoolean;
V.VType = varInteger;
V.VInteger = 1234567;
Variant V;
TVarData( V ).VType = varBoolean;
TVarData( V ).VBoolean = True;
this->Caption = V;
d:\program files (x86)\embarcadero\studio\16.0\include\windows\rtl\systvar.h
// TVarData = VARIANT
struct TVarData
{
union
{
uint16_t VType; // Delphi-compatible - Variant Type member
#if defined(VARIANT_NATIVE_SUPPORT)
VARTYPE vt; // tagVARIANT compatible member
#else
uint16_t vt; // Expose a generic 'vt' member
#endif
};
uint16_t Reserved1;
uint16_t Reserved2;
uint16_t Reserved3;
union
{
// Delphi-compatible TVarData/Variant members
System::Smallint VSmallint; // iVal
System::Integer VInteger; // lVal
System::Single VSingle; // fltVal
System::Double VDouble; // dblVal
System::CurrencyBase VCurrency; // cyVal
System::TDateTimeBase VDate; // date
System::PWideChar VOleStr; // bstrVal
#ifdef _WIN32
IDispatch* VDispatch; // pdispVal
#else
System::IDispatch* VDispatch; // pdispVal
#endif
System::HResult VError; // scode
System::WordBool VBoolean; // boolVal
IUnknown* VUnknown; // punkVal
System::Byte VByte; // bVal
System::Int8 VShortint; // charVal
System::Int8 VShortInt; // charVal
System::Pointer VString; // ??????
System::PVarArray VArray; // parray
System::Pointer VPointer; // byref
__int64 VInt64; // llVal
unsigned __int64 VUInt64; // ullVal
System::Word VWord; // uiVal
System::LongWord VLongWord; // ulVal
System::TVarRecord VRecord; // struct __tagBRECORD
}
d:\program files (x86)\embarcadero\studio\16.0\include\windows\rtl\System.hpp
Int8 varEmpty = Int8(0x0);
Int8 varNull = Int8(0x1);
Int8 varSmallint = Int8(0x2);
Int8 varInteger = Int8(0x3);
Int8 varSingle = Int8(0x4);
Int8 varDouble = Int8(0x5);
Int8 varCurrency = Int8(0x6);
Int8 varDate = Int8(0x7);
Int8 varOleStr = Int8(0x8);
Int8 varDispatch = Int8(0x9);
Int8 varError = Int8(0xa);
Int8 varBoolean = Int8(0xb);
Int8 varVariant = Int8(0xc);
Int8 varUnknown = Int8(0xd);
Int8 varShortInt = Int8(0x10);
Int8 varByte = Int8(0x11);
Int8 varWord = Int8(0x12);
Int8 varLongWord = Int8(0x13);
Int8 varInt64 = Int8(0x14);
Int8 varUInt64 = Int8(0x15);
Int8 varRecord = Int8(0x24);
Int8 varStrArg = Int8(0x48);
Int8 varObject = Int8(0x49);
Int8 varUStrArg = Int8(0x4a);
Word varString = Word(0x100);
Word varAny = Word(0x101);
Word varUString = Word(0x102);
Word varTypeMask = Word(0xfff);
Word varArray = Word(0x2000);
Word varByRef = Word(0x4000);
Int8 vtInteger = Int8(0x0);
Int8 vtBoolean = Int8(0x1);
Int8 vtChar = Int8(0x2);
Int8 vtExtended = Int8(0x3);
Int8 vtString = Int8(0x4);
Int8 vtPointer = Int8(0x5);
Int8 vtPChar = Int8(0x6);
Int8 vtObject = Int8(0x7);
Int8 vtClass = Int8(0x8);
Int8 vtWideChar = Int8(0x9);
Int8 vtPWideChar = Int8(0xa);
Int8 vtAnsiString = Int8(0xb);
Int8 vtCurrency = Int8(0xc);
Int8 vtVariant = Int8(0xd);
Int8 vtInterface = Int8(0xe);
Int8 vtWideString = Int8(0xf);
Int8 vtInt64 = Int8(0x10);
Int8 vtUnicodeString = Int8(0x11);
Int8 vmtArcOffset = Int8(0x0);
_VarToUStr
参考官方的写法
procedure _VarToUStr(var S: UnicodeString; const V: TVarData);
begin
case V.VType of
varEmpty: S := '';
varNull:
begin
if NullStrictConvert then
VarCastError(varNull, varOleStr);
S := NullAsStringValue;
end;
varSmallInt: S := IntToStr(V.VSmallInt);
varInteger: S := IntToStr(V.VInteger);
varSingle: S := FloatToStr(V.VSingle);
varDouble: S := FloatToStr(V.VDouble);
varShortInt: S := IntToStr(V.VShortInt);
varByte: S := IntToStr(V.VByte);
varWord: S := IntToStr(V.VWord);
varLongWord: S := UIntToStr(V.VLongWord);
varInt64: S := IntToStr(V.VInt64);
varUInt64: S := UIntToStr(V.VUInt64); varVariant: _VarToUStr(S, PVarData(V.VPointer)^);
varOleStr: S := Copy(WideString(Pointer(V.VOleStr)), , MaxInt); {$IFNDEF NEXTGEN}
varCurrency: S := CurrToWStrViaOS(V.VCurrency);
varDate: S := DateToWStrViaOS(V.VDate);
varBoolean: S := BoolToWStrViaOS(V.VBoolean);
varDispatch,
varUnknown: S := VarToWStrViaOS(V);
{$ELSE} varCurrency: S := CurrToStr(V.VCurrency);
varDate: S := DateTimeToStr(V.VDate);
varBoolean: S := BoolToStr(V.VBoolean,True); {$ENDIF !NEXTGEN}
else
case V.VType of
varString: S := RawByteString(V.VString);
varUString: S := UnicodeString(V.VUString);
varAny: S := VarToUStrAny(V);
else
if V.VType and varByRef <> then
case V.VType and not varByRef of
varSmallInt: S := IntToStr(PSmallInt(V.VPointer)^);
varInteger: S := IntToStr(PInteger(V.VPointer)^);
varSingle: S := FloatToStr(PSingle(V.VPointer)^);
varDouble: S := FloatToStr(PDouble(V.VPointer)^);
varShortInt: S := IntToStr(PShortInt(V.VPointer)^);
varByte: S := IntToStr(PByte(V.VPointer)^);
varWord: S := IntToStr(PWord(V.VPointer)^);
varLongWord: S := UIntToStr(PCardinal(V.VPointer)^);
varInt64: S := IntToStr(PInt64(V.VPointer)^);
varUInt64: S := UIntToStr(PUInt64(V.VPointer)^);
varVariant: _VarToUStr(S, PVarData(V.VPointer)^);
{$IFNDEF NEXTGEN}
varCurrency: S := CurrToWStrViaOS(PCurrency(V.VPointer)^);
varDate: S := DateToWStrViaOS(PDate(V.VPointer)^);
varOleStr: S := PWideChar(V.VPointer^);
varBoolean: S := BoolToWStrViaOS(PWordBool(V.VPointer)^);
varString: S := UnicodeString(PAnsiChar(V.VPointer^));
varUString: S := PWideChar(V.VPointer^);
else
S := VarToWStrViaOS(V);
{$ELSE NEXTGEN}
varCurrency: S := CurrToStr(PCurrency(V.VPointer)^);
varDate: S := DateTimeToStr(PDate(V.VPointer)^);
varOleStr: S := Copy(PChar(V.VPointer^), , MaxInt);
varBoolean: S := BoolToStr(PWordBool(V.VPointer)^);
varUString: S := PWideChar(V.VPointer^); {$ENDIF NEXTGEN}
end
else
if not VarToUStrCustom(V, S) then
{$IFNDEF NEXTGEN}
S := VarToWStrViaOS(V);
{$ELSE NEXTGEN} ; // Now we do nothing
{$ENDIF !NEXTGEN}
end;
end;
end;