C++ Builder 将 TMemoryStream 缓冲区读取为无效指针
C++ Builder reading TMemoryStream buffer to void pointer
我正在尝试将 TMemoryStream 缓冲区的内容复制到 void 指针,下面您可以看到不起作用的示例代码 - void 缓冲区为空,但文本框包含有效大小 - 我用 winrar 大小检查了它。以下代码创建 exe 文件,但它的大小为 0.
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMemoryStream* ms = new TMemoryStream();
Download->Get("http://rarlab.com/rar/wrar521pl.exe",ms);
this->Edit1->Text = ms->Size;
void* buffer = malloc(ms->Size );
ms->Read(buffer,ms->Size);
FILE* pFile = fopen("test.exe","wb");
fwrite(buffer,4,ms->Size,pFile);
fclose(pFile);
delete ms;
}
此外,我知道我可以轻松地将 TMemoryStream 缓冲区的内容保存到文件中,但我需要它的内容作为 void 指针,所以我写了下面的代码来测试它。
您从流中读取的代码几乎是正确的。您没有在读取之前将流的 Position
属性 重置为 0,因此您实际上是从流的末尾读取,那里没有数据可读。
试试这个:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMemoryStream* ms = new TMemoryStream();
try
{
Download->Get("http://rarlab.com/rar/wrar521pl.exe", ms);
ms->Position = 0; // <-- ADD THIS
Edit1->Text = ms->Size;
void* buffer = malloc(ms->Size);
if (buffer)
{
ms->ReadBuffer(buffer, ms->Size);
// ms->SaveToFile("test.exe");
FILE* pFile = fopen("test.exe", "wb");
if (pFile)
{
fwrite(buffer, 1, ms->Size, pFile);
fclose(pFile);
}
free(buffer);
}
}
__finally
{
delete ms;
}
}
或者,根本不分配单独的缓冲区,而是直接使用流自己的缓冲区:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMemoryStream* ms = new TMemoryStream();
try
{
Download->Get("http://rarlab.com/rar/wrar521pl.exe", ms);
Edit1->Text = ms->Size;
void* buffer = ms->Memory;
// ms->SaveToFile("test.exe");
FILE* pFile = fopen("test.exe", "wb");
if (pFile)
{
fwrite(buffer, 1, ms->Size, pFile);
fclose(pFile);
}
}
__finally
{
delete ms;
}
}
我正在尝试将 TMemoryStream 缓冲区的内容复制到 void 指针,下面您可以看到不起作用的示例代码 - void 缓冲区为空,但文本框包含有效大小 - 我用 winrar 大小检查了它。以下代码创建 exe 文件,但它的大小为 0.
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMemoryStream* ms = new TMemoryStream();
Download->Get("http://rarlab.com/rar/wrar521pl.exe",ms);
this->Edit1->Text = ms->Size;
void* buffer = malloc(ms->Size );
ms->Read(buffer,ms->Size);
FILE* pFile = fopen("test.exe","wb");
fwrite(buffer,4,ms->Size,pFile);
fclose(pFile);
delete ms;
}
此外,我知道我可以轻松地将 TMemoryStream 缓冲区的内容保存到文件中,但我需要它的内容作为 void 指针,所以我写了下面的代码来测试它。
您从流中读取的代码几乎是正确的。您没有在读取之前将流的 Position
属性 重置为 0,因此您实际上是从流的末尾读取,那里没有数据可读。
试试这个:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMemoryStream* ms = new TMemoryStream();
try
{
Download->Get("http://rarlab.com/rar/wrar521pl.exe", ms);
ms->Position = 0; // <-- ADD THIS
Edit1->Text = ms->Size;
void* buffer = malloc(ms->Size);
if (buffer)
{
ms->ReadBuffer(buffer, ms->Size);
// ms->SaveToFile("test.exe");
FILE* pFile = fopen("test.exe", "wb");
if (pFile)
{
fwrite(buffer, 1, ms->Size, pFile);
fclose(pFile);
}
free(buffer);
}
}
__finally
{
delete ms;
}
}
或者,根本不分配单独的缓冲区,而是直接使用流自己的缓冲区:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMemoryStream* ms = new TMemoryStream();
try
{
Download->Get("http://rarlab.com/rar/wrar521pl.exe", ms);
Edit1->Text = ms->Size;
void* buffer = ms->Memory;
// ms->SaveToFile("test.exe");
FILE* pFile = fopen("test.exe", "wb");
if (pFile)
{
fwrite(buffer, 1, ms->Size, pFile);
fclose(pFile);
}
}
__finally
{
delete ms;
}
}