Quote
The reason why I dont want to pass the size as an argument is because I
want to limit the hassle to developer; you see, I am constructing a
Registry Component. So it's a matter of asthetics.
By not providing a way for the developer to indicate to your function the
number of bytes in the array you are actually doing the opposite. A hassle
would be the suggestions I proposed last time about prepending or appending
some sort of length indicator.
Quote
I was hoping there was some sort of function out there that could count
bytes for me.
There would be no way for the code to know how many bytes are in the array.
Even if you could access the memory manager to know how many bytes were
reserved for the array this might not be the number the developer intends to
write to the registry.
TRegIniFilePlus* rifp = new TRegIniFilePlus;
BYTE buffer[1024];
memcpy(buffer, "0123456789ABCDEF", 16);
rifp->WriteREG_BINARY("foo", buffer);
Even though 'buffer' can legally hold 1024 bytes it only has 16. How should
your code behave? When you work with a fundamental data type you need to
let the developer have control over that.
In these situations it is best to provide several overloaded versions of
your method. You can support NULL terminated C strings, TMemoryStream, or
some other custom structure type, plus your generic method which is called
by the other overloaded methods:
long __fastcall TRegIniFilePlus::WriteREG_BINARY(AnsiString Ident,
BYTE* Buffer, unsigned Length)
{
return RegSetValueEx(CurrentKey, Ident.c_str(), 0, REG_BINARY,
Buffer, Length);
}
long __fastcall TRegIniFilePlus::WriteREG_BINARY(AnsiString Ident,
char* Value)
{
WriteREG_BINARY(Ident, (BYTE*) Value, strlen(Value));
}
long __fastcall TRegIniFilePlus::WriteREG_BINARY(AnsiString Ident,
TMemoryStream* Stream)
{
WriteREG_BINARY(Ident, (BYTE*) Stream->Memory, Stream->Size);
}
long __fastcall TRegIniFilePlus::WriteREG_BINARY(AnsiString Ident,
TStream* Stream)
{
// This method is a little less efficient than the previous method
// but allows your code to work with any TStream descendant
BYTE* buffer = new BYTE[Stream->Size];
Stream->Position = 0;
Stream->Read(buffer, Stream->Size);
WriteREG_BINARY(Ident,buffer, Stream->Size);
delete[] buffer;
}
... etc ...
- Clayton