C++ 如何使用tmemoryStream写入和读取组件
我一直在尝试使用C++ 如何使用tmemoryStream写入和读取组件,c++,C++,我一直在尝试使用writecomponent方法在TMemoryStream对象上编写一个从TComponent派生的任意类的对象,并使用readcomponent方法再次检索该对象。虽然我认为这是一项容易的任务,但我无法使它正常工作。实际上没有编译错误,但是对象的属性没有正确加载。请帮我找出我做错了什么。这是我的代码片段 #include <vcl.h> #pragma hdrstop #include <tchar.h> #include <memory>
writecomponent
方法在TMemoryStream
对象上编写一个从TComponent
派生的任意类的对象,并使用readcomponent
方法再次检索该对象。虽然我认为这是一项容易的任务,但我无法使它正常工作。实际上没有编译错误,但是对象的属性没有正确加载。请帮我找出我做错了什么。这是我的代码片段
#include <vcl.h>
#pragma hdrstop
#include <tchar.h>
#include <memory>
#include <iostream>
#include <conio.h>
#pragma argsused
using namespace std;
class Woman : public TComponent
{
private:
int Age;
public:
UnicodeString Name;
Woman(TComponent* _Owner, int InAge, UnicodeString InName)
: TComponent(_Owner)
{
Age = InAge;
Name = InName;
}
int GetAge()
{
return Age;
}
};
void RegisterClassesWithStreamingSystem(void)
{
#pragma startup RegisterClassesWithStreamingSystem
Classes::RegisterClass(__classid(Woman));
}
int _tmain(int argc, _TCHAR* argv[])
{
Woman* FirstWoman = new Woman(NULL, 25, "Anjelina");
UnicodeString as;
auto_ptr<TMemoryStream> MStr(new TMemoryStream);
auto_ptr<TStringStream> SStr(new TStringStream(as));
MStr->WriteComponent(FirstWoman);
MStr->Seek(0, soFromBeginning);
ObjectBinaryToText(MStr.get(), SStr.get());
SStr->Seek(0, soFromBeginning);
as = SStr->DataString;
auto_ptr<TMemoryStream> pms(new TMemoryStream);
auto_ptr<TStringStream> pss(new TStringStream(as));
TComponent *pc;
ObjectTextToBinary(pss.get(), pms.get());
pms->Seek(0, soFromBeginning);
pc = pms->ReadComponent(NULL);
Woman* AWoman;
AWoman = dynamic_cast<Woman*>(pc);
cout << AWoman->GetAge() << endl;
cout << AWoman->Name.c_str() << endl;
FirstWoman->Free();
pc->Free();
getch();
return 0;
}
#包括
#布拉格语hdrstop
#包括
#包括
#包括
#包括
#布拉格语
使用名称空间std;
女学员:公共t组件
{
私人:
智力年龄;
公众:
独角兽名称;
女性(t组件*_所有者,内部名称,内部名称)
:t组件(\u所有者)
{
年龄=年龄;
Name=InName;
}
int GetAge()
{
回归年龄;
}
};
无效注册表类与StreamingSystem(无效)
{
#pragma启动寄存器类SwithStreamingSystem
类:RegisterClass(uu classid(Woman));
}
int _tmain(int argc,_TCHAR*argv[]
{
女人*第一个女人=新女人(空,25,“安吉丽娜”);
破坏as;
自动ptr MStr(新TMemoryStream);
自动ptr SStr(新TStringStream(as));
MStr->WriteComponent(FirstWoman);
MStr->Seek(0,从开始);
ObjectBinaryToText(MStr.get(),SStr.get());
SStr->Seek(0,从开始);
as=SStr->DataString;
自动ptr pms(新的TMemoryStream);
自动ptr pss(新TStringStream(as));
t组件*pc;
ObjectTextToBinary(pss.get(),pms.get());
pms->Seek(0,从开始);
pc=pms->ReadComponent(空);
女人*女人;
AWoman=动态投影(pc);
cout GetAge()Free();
getch();
返回0;
}
它不起作用,因为
Woman
类没有任何\u已发布的读/写属性。WriteComponent()
和ReadComponent()
方法是DFM流媒体系统的一部分,DFM依靠发布属性的RTTI来完成工作
class Woman : public TComponent
{
private:
int fAge;
UnicodeString fName;
public:
__fastcall Woman(TComponent* Owner)
: TComponent(Owner)
{
}
__fastcall Woman(TComponent* Owner, int InAge, const UnicodeString &InName)
: TComponent(Owner)
{
fAge = InAge;
fName = InName;
}
__published:
__property int Age = {read=fAge, write=fAge};
__property UnicodeString Name = {read=fName, write=fName};
};
然后你可以这样做:
int _tmain(int argc, _TCHAR* argv[])
{
auto_ptr<Woman> FirstWoman(new Woman(NULL, 25, L"Anjelina"));
auto_ptr<TMemoryStream> MStr(new TMemoryStream);
auto_ptr<TStringStream> SStr(new TStringStream(L""));
MStr->WriteComponent(FirstWoman.get());
MStr->Position = 0;
ObjectBinaryToText(MStr.get(), SStr.get());
SStr->Position = 0;
UnicodeString as = SStr->DataString;
auto_ptr<TStringStream> pss(new TStringStream(as));
auto_ptr<TMemoryStream> pms(new TMemoryStream);
ObjectTextToBinary(pss.get(), pms.get());
pms->Position = 0;
auto_ptr<TComponent> pc(pms->ReadComponent(NULL));
Woman* AWoman = static_cast<Woman*>(pc.get());
cout << AWoman->Age << endl;
cout << AWoman->Name.c_str() << endl;
FirstWoman.reset();
pc.reset();
getch();
return 0;
}
int-tmain(int-argc,_-TCHAR*argv[]
{
auto_ptr FirstWoman(新女性(空,25,L“Angelina”);
自动ptr MStr(新TMemoryStream);
自动ptr SStr(新TStringStream(L“);
MStr->WriteComponent(FirstWoman.get());
MStr->Position=0;
ObjectBinaryToText(MStr.get(),SStr.get());
SStr->位置=0;
UnicodeString as=SStr->DataString;
自动ptr pss(新TStringStream(as));
自动ptr pms(新的TMemoryStream);
ObjectTextToBinary(pss.get(),pms.get());
pms->Position=0;
自动ptr pc(pms->ReadComponent(NULL));
女人*女人=静态演员(pc.get());
cout年龄位置=0;
ObjectBinaryToText(MStr.get(),SStr.get());
SStr->位置=0;
UnicodeString as=SStr->DataString;
自动ptr pss(新TStringStream(as));
自动ptr pms(新的TMemoryStream);
ObjectTextToBinary(pss.get(),pms.get());
pms->Position=0;
自动ptr第二名女性(新女性(空));
pms->ReadComponent(SecondWoman.get());
它不起作用,因为
您的Woman
类没有任何发布的\u
读/写属性。WriteComponent()
和ReadComponent()
方法是DFM流媒体系统的一部分,DFMs依赖发布属性的RTTI来完成其工作
DFM无法构造类的实例,因为它不是兼容的构造函数,您使用的是DFM无法调用的自定义构造函数
您需要相应地更新您的类,例如:
class Woman : public TComponent
{
private:
int fAge;
UnicodeString fName;
public:
__fastcall Woman(TComponent* Owner)
: TComponent(Owner)
{
}
__fastcall Woman(TComponent* Owner, int InAge, const UnicodeString &InName)
: TComponent(Owner)
{
fAge = InAge;
fName = InName;
}
__published:
__property int Age = {read=fAge, write=fAge};
__property UnicodeString Name = {read=fName, write=fName};
};
然后你可以这样做:
int _tmain(int argc, _TCHAR* argv[])
{
auto_ptr<Woman> FirstWoman(new Woman(NULL, 25, L"Anjelina"));
auto_ptr<TMemoryStream> MStr(new TMemoryStream);
auto_ptr<TStringStream> SStr(new TStringStream(L""));
MStr->WriteComponent(FirstWoman.get());
MStr->Position = 0;
ObjectBinaryToText(MStr.get(), SStr.get());
SStr->Position = 0;
UnicodeString as = SStr->DataString;
auto_ptr<TStringStream> pss(new TStringStream(as));
auto_ptr<TMemoryStream> pms(new TMemoryStream);
ObjectTextToBinary(pss.get(), pms.get());
pms->Position = 0;
auto_ptr<TComponent> pc(pms->ReadComponent(NULL));
Woman* AWoman = static_cast<Woman*>(pc.get());
cout << AWoman->Age << endl;
cout << AWoman->Name.c_str() << endl;
FirstWoman.reset();
pc.reset();
getch();
return 0;
}
int-tmain(int-argc,_-TCHAR*argv[]
{
auto_ptr FirstWoman(新女性(空,25,L“Angelina”);
自动ptr MStr(新TMemoryStream);
自动ptr SStr(新TStringStream(L“);
MStr->WriteComponent(FirstWoman.get());
MStr->Position=0;
ObjectBinaryToText(MStr.get(),SStr.get());
SStr->位置=0;
UnicodeString as=SStr->DataString;
自动ptr pss(新TStringStream(as));
自动ptr pms(新的TMemoryStream);
ObjectTextToBinary(pss.get(),pms.get());
pms->Position=0;
自动ptr pc(pms->ReadComponent(NULL));
女人*女人=静态演员(pc.get());
cout年龄位置=0;
ObjectBinaryToText(MStr.get(),SStr.get());
SStr->位置=0;
UnicodeString as=SStr->DataString;
自动ptr pss(新TStringStream(as));
自动ptr pms(新的TMemoryStream);
ObjectTextToBinary(pss.get(),pms.get());
pms->Position=0;
自动ptr第二名女性(新女性(空));
pms->ReadComponent(SecondWoman.get());
亲爱的雷米,谢谢你。我爱你,伙计。它奏效了。你是个天才。亲爱的雷米,我爱你,伙计。它奏效了。你是个天才